Automating MediaConvert Jobs with AWS Lambda

Automating MediaConvert Jobs with AWS Lambda

In this Demo, we will use Amazon S3 and Lambda to trigger AWS Elemental MediaConvert jobs automatically.

Prerequisites

You must have an Amazon S3 source bucket (for example, ip-tutorial-bucket-1) with videos to be transcoded already stored in it. And Output Bucket (for example,video-op-bucket-2)

image.png

Depending on the environment, customer requirements, and marketplace we will use AWS Serverless to deploy Lambda Functions. Currently, MediaConvert has implemented a mechanism that will be invoked when a user uploads a video in the Input Bucket. The Lambda function will then query the MediaConvert service to produce the required video formats and distribution configurations.

  • An Apple HLS adaptive bitrate stream for playout on multiple-sized devices and varying bandwidths.
  • An MP4 stream
  • Thumbnails for use in websites to show a video preview at a point in time.

Step 1: Create an S3 bucket for the output media file

In this step, you'll create an S3 bucket to store the transcoded media files so they can be viewed on any device, then configure CORS rules to allow devices that do not meet the CORS settings to access the files.

  1. Create a bucket for the output media files
  2. Sign in to the AWS Management Console and open the Amazon S3 console at console.aws.amazon.com/s3.

  3. In the left navigation pane, choose Buckets.

  4. Choose Create bucket.

  5. For Bucket name, enter a name for your bucket (for example, video-op-bucket-2).

  6. For Region, choose the AWS Region where you want the bucket to reside.

  7. To ensure public access to your output media files, in Block Public Access settings for this bucket, clear Block all public access.

  8. Select the check box next to I acknowledge that the current settings might result in this bucket and the objects within becoming public.

  9. Keep the remaining settings set to the defaults.

  10. Choose Create bucket.

Add a CORS configuration to the S3 output bucket

  1. In the Buckets list, choose the name of the bucket that you created earlier (for example,video-op-bucket-2).

  2. Choose the Permissions tab.

  3. In the Cross-origin resource sharing (CORS) section, choose Edit.

  4. In the CORS configuration text box, copy and paste the following CORS configuration.

  5. The CORS configuration must be in JSON format. In this example, the AllowedOrigins attribute uses the wildcard character (*) to specify all origins.

    {
        "AllowedOrigins": [
            "*"
         ],
         "AllowedMethods": [
             "GET"
         ],
         "AllowedHeaders": [
             "*"
         ],
         "ExposeHeaders": []

     }
 ]

Choose Save changes.

Step 2: Create an IAM role for MediaConvert

In order for you to use the MediaConvert console to transcode videos in your S3 bucket, you need IAM service roles to grant MediaConvert the ability to read and write video files from and to your source and destination buckets. Related entities, including metadata objects, automatically use these roles, allowing MediaConvert to automatically conform to your S3 access control rules.

To create an IAM role for MediaConvert

Sign in to the AWS Management Console and open the IAM console at console.aws.amazon.com/iam.

In the navigation pane of the IAM console, choose Roles, and then choose Create role.

Choose the AWS service role type, and then choose the MediaConvert service.

Choose the MediaConvert use case for your service. Then choose Next: Permissions. The service has already defined the permissions used by the role. These permissions grant MediaConvert the following permissions:

  • Full access to your Amazon S3 resources

  • API Gateway invoke full access

Choose Next: Review.

For the Role name, enter a name that describes the purpose of the role. If you use the name MediaConvert_Default_Role, then the MediaConvert console will use this role by default when you run jobs.

On the Summary page, copy the Role ARN (which starts with arn:aws:iam::), and save the ARN for use later.

Review the role, and then choose to Create a role.

image.png

Step 3: Create an IAM role for your Lambda function

To batch-transcode videos with MediaConvertand S3, you must create an IAM role in order to connect the roles to MediaConvert and S3 Batch. The correct step is to practice IAM role that grants the Lambda function permissions to access MediaConvert and S3 Batch Operations

  1. Create an IAM role for your Lambda function
  2. Sign in to the AWS Management Console and open the IAM console at console.aws.amazon.com/iam.

  3. In the left navigation pane, choose Roles, and then choose Create role.

  4. Choose the AWS service role type, and then under Common use cases, choose Lambda.

  5. Choose Next: Permissions.

  6. On the Attach permissions policies page, enter AWSLambdaBasicExecutionRole in the Filter policies box. To attach the managed policy AWSLambdaBasicExecutionRole to this role to grant write permissions to Amazon CloudWatch Logs, select the check box next to AWSLambdaBasicExecutionRole.

  7. Choose Next: Tags.

  8. (Optional) Add tags to the managed policy.

  9. Choose Next: Review.

  10. For Role name, enter tutorial-lambda-transcode-role.

  11. Choose Create role.

  12. Type the role name into the filter box on the Roles page which you created

On the Permissions tab, click on the Add Inline Policy link and choose the JSON tab. Copy and paste the following JSON in the Policy Document Box. You will need to edit this policy in the next step to fill in the resources for your application.

    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "*",
            "Effect": "Allow",
            "Sid": "Logging"
        },
        {
            "Action": [
                "iam:PassRole"
            ],
            "Resource": [
                "<ARN for MediaConvert_Default_Role>"
            ],
            "Effect": "Allow",
            "Sid": "PassRole"
        },
        {
            "Action": [
                "mediaconvert:*"
            ],
            "Resource": [
                "*"
            ],
            "Effect": "Allow",
            "Sid": "MediaConvertService"
        }
    ]
}
  • Choose Review Policy.

  • For Name, enter tutorial-lambda-policy.

  • Choose Create Policy.

image.png

Step 4: Create a Lambda function for converting videos

AWS Lambda will run your code in response to events such as a putObject into S3 or an HTTP request. In this step you'll build the core function that will process videos using the MediaConvert python SDK. The lambda function will respond to putObject events in your S3 source bucket. Whenever a video file is added, the lambda will start a MediaConvert job.

Make sure to configure your function to use the tutorial-lambda-transcode-role IAM role you created in the previous section.

Step-by-step instructions

  1. Choose Services then select Lambda in the Compute section.

  2. Choose Create function.

  3. Choose the Author from scratch button.

  4. On the Author from Scratch panel, enter tutorial-lambda-convert in the Function name field.

  5. Select Python 3.8 for the Runtime.

  6. Expand the Choose or create an execution role.

  7. Select Use an existing role.

  8. Select tutorial-lambda-transcode-role from the Existing Role dropdown.

  9. click on Create function.

image.png

  1. On the Configuration tab of the tutorial-lambda-convert page, in the function code panel:

  2. Select Upload a file from Amazon S3 for the Code entry type Enter the following for the URL: rodeolabz-us-west-2.s3-us-west-2.amazonaws....

  3. Note that this zip file is simply the convert.py script and the job JSON file provided in this repo that you could zip up yourself if desired.

  4. Enter convert.handler for the Handler field.

image.png

  • On the Environment Variables panel of the tutorial-lambda-convert page, enter the following keys and values:

DestinationBucket = video-op-bucket-2 (or whatever you named your bucket )

MediaConvertRole = arn:aws:iam::ACCOUNT NUMBER:role/MediaConvert_Default_Role

Application = Batch-Transcoding

  • On the Basic Settings panel, set Timeout to 2 minutes.

  • Scroll back to the top of the page and click on the Save button.

Step 5: Create a S3 Put Event Trigger for your Convert lambda

Step-by-step instructions

  1. In the Configuration->Designer panel of the tutorial-lambda-convert function, click on Add trigger button.

  2. Select S3 from the Trigger configuration dropdown.

  3. Select ip-tutorial-bucket-1 or the name you used for the Source bucket you created earlier.

  4. Select PUT for the Event type.

  5. Leave the rest of the settings as the default and click the Add button.

image.png

Final Step: Test the MediaConvert(ip-tutorial-bucket-1) automation

You can use your own video or use the test.mp4 video included in this folder to test the workflow.

  1. Open the S3 console overview page for the ip-tutorial-bucket-1 S3 bucket you created earlier.

  2. Select Upload and then choose the file test.mp4 from your computer.

  3. Note the time that the upload was completed.

  4. Open the MediaConvert jobs page and find a job for the input 'test.mp4' that was started near the time your upload was completed.

image.png

image.png

Navigate to the MP4 or HLS output and play the test video by clicking on the test.mp4 output http resource.

image.png

Summary

In this post, I have shown you how to convert any video file to HLS or MP4 format using MediaConvert and Lambda.

Did you find this article valuable?

Support Ramu Chelloju by becoming a sponsor. Any amount is appreciated!