Amazon Lambda Node.js script for transcoding media files in S3 using Elastic Transcoder
This setup mainly focusess on automatic transcoding audio files when they get uploaded to a specific folder in a S3 bucket. The script could however also be used for other media files and formats supported by AWS Elastic Transcoder.
It uses an "ingest" folder for newly added files. When the Lambda Function is triggered, an Elastic Transcoder job is created. The Lambda Function will check and wait until the transcoding is done. When successful the transcoded file is moved to its own folder. And the ingested file is moved to an "original" folder. All from one Amazon S3 bucket!
The creator of this tutorial takes no reliability for damages, material or inmaterial, to any property, while using the information given in this tutorial or script.
Almost everything you do on Amazon Web Services costs real money. Know the costs of the services you and this tutorial are using before proceeding!
Create a new bucket if you haven't already. Inside the root of this bucket create 3 folders:
ingest
original
vbr128
In Elastic Transcoder create a new Pipeline. Select your bucket and use the default Elastic Transcoder role. Select your bucket again for the Transcoded Files, Playlists and Thumbnails.
Create a new IAM Role in the IAM Console for a AWS Service and select Lambda. Give it the following rights:
Click on 'Create Function' in the Lambda main menu and start from scratch. Give it a name and use 'Node.js 6.10' as the Runtime engine. At the Permissions select the Existing Role you created earlier.
Insert the contents of index.js
from this repository into the main runtime file of the function.
You must set the following Environment variables:
BUCKET_REGION
:your_bucket_region
BUCKET_NAME
:your_bucket_name
PREFIX_TRIGGER
:ingest/
PREFIX_INPUT
:original/
PREFIX_OUTPUT
:vbr128/
PIPELINE_ID
:your_pipeline_id
TRANSCODE_PRESET
:your_preset_id
(this tutorial uses 'System preset: Audio MP3 - 128k')TRANSCODE_EXTENSION
:.mp3
Under Basic Settings, set the timeout to 5 minutes or more. The Lambda function will wait until the transcoding is done for each file before moving the ingested original.
Add a S3 Trigger for All Create Events in your selected bucket. Use the Prefix ingest/
for your ingest folder. You can also use a suffix, if you only want to trigger on a specific file type.