Oct 17, 2016

Automatically convert files in S3 buckets using AWS Lambda

AWS Lambda is an event-driven compute service which runs your code (Lambda functions) in response to events, such as changes to data in an Amazon S3 bucket. The CloudConvert API can be used to automatically convert all files, added to a specific S3 bucket. Typical use cases are converting all office documents to PDF, creating thumbnails or encoding videos.

We have created a GitHub repository with an example Lambda function. To get it running, follow these instructions:

  • Download / Fork the repository
  • Run npm install
  • Adjust convert.js according to your needs:
  • Set CLUDCONVERT_API_KEY to your personal API key.
  • Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY for downloading and uploading from/to your S3 bucket.
  • Adjust the conversion paramaters, such as outputformat according to your need. All possible options can be identified using the API Console.
  • Create a ZIP file which contains convert.js and the node_modules folder.
  • In the AWS Console create a new Lambda function. Select Blank Function blueprint.
  • Select S3 trigger and your bucket. Set Event Type to PUT. Adjust Prefix and Suffix to trigger for specific files only.
  • Choose a name for your Lambda function and use Node.js 4.3  as Runtime. Upload your previously created ZIP file as Lambda function. The Handler name needs to be convert.handler. The lambda function does not need any specific permissions: you can create a new empty role.
  • A timeout value of 10 seconds should be sufficient. Please note that the lambda function only triggers the actual conversion and terminates as soon as the conversion was started. Therefore the timeout does not affect the time needed for the actual conversion.
  • Done! You can test your Lambda function by adding a new file to your S3 bucket.