aws-lambda-r
aws-lambda-r copied to clipboard
return images (PNG) not only JSON
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"}]'
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.