aws-lambda-r icon indicating copy to clipboard operation
aws-lambda-r copied to clipboard

return images (PNG) not only JSON

Open MikeBadescu opened this issue 6 years ago • 2 comments

MikeBadescu avatar Mar 26 '18 12:03 MikeBadescu

We can solve this problem by making the R script return a base64 encoded image which will be passed to the lambda handler. In order for the API to be able to respond to a GET request with a decoded image, binary payloads should be activated at the integration response level of the API. E.g aws apigateway update-integration-response \ --profile davemaple \ --rest-api-id [api_id] \ --resource-id [resource_id] \ --http-method GET \ --status-code 200 \ --patch-operations '[{"op" : "replace", "path" : "/contentHandling", "value" : "CONVERT_TO_BINARY"}]'

AnaTofan avatar Mar 26 '18 16:03 AnaTofan

The main problem still remains the fact that a plot generated by R should be saved in the file system and then encoded as base64 (using base64enc package). When R generates a plot, it automatically looks for a file called Rplots.pdf in order to write it, and this file cannot be found when the script is run in a Lambda environment. (causing the failure of the Lambda function) A possible solution would be to deactivate implicit writing in Rplots.pdf file when a plot is created.

AnaTofan avatar Mar 26 '18 16:03 AnaTofan