Skip to main content

Implementation: EventBridge & Lambda

Getting the result of the MediaConvert job

Since creating the job command doesn't tell us anything about the result of the job, we need to use AWS EventBridge to notify us about the job results, and we need an AWS Lambda function to receive the notification and do something with it. First we'll create the Lambda function, then create the EventBridge rule.

Create a Lambda function to receive your EventBridge notifications

In the Lambda console, go to Functions and create one. You can use whatever runtime you like; for example purposes I'm using Node.js 20.x here. You can use your existing Lambda execution role for this function if you have one, or create a new one using the form.

For the function itself, here's a very basic bit of code we can enter, just to verify that the EventBridge notifications are being received successfully:

export const handler = async (event) => {
  // TODO implement
  console.log("Received event from EventBridge: %o", event);
  const response = {
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda!'),
  };
  return response;
};

With our Lambda function ready, we can create our EventBridge rule.

Create an EventBridge rule to monitor MediaConvert jobs

In the EventBridge console, go to Rules and create one. Select Rule with an event pattern and enter a name for the rule. The Event Source should be AWS events or EventBridge partner events; it's worth selecting one of the sample events just to have it there for reference later. Search for "MediaConvert Job State Change" to find it.

image.png

For the Creation Method, select Use pattern from, and for the Event Pattern, select AWS Services -> MediaConvert -> MediaConvert Job State Change, then add the state changes you want to listen for (here, COMPLETE and ERROR state changes have been added).

image.png

Now we need to specify the target these event notifications should be sent to. Select AWS Service as the target type, then Lambda function. You should be able to find the Lambda function you created earlier in the list (if you can't, it may be that you created your Lambda function in the wrong region; your EventBridge rule and Lambda function need to be in the same region).

image.png

That's everything; click next until the rule is created.

Testing the integration

To test that your setup is working, run your Node.js code (not the Lambda function, the code to create a MediaConvert job command), or create a job manually in the MediaConvert console. Once the job completes, or returns an error, you can check your Lambda function's logs in CloudWatch to see if it received the EventBridge notification successfully.

If your Lambda function is getting the notifications, you can now change your Lambda function code to do whatever you want with the notification. For instance, you could update your database with the URL to the newly transcoded video file, or set a flag for that video's record in your database signifying a transcoding error.