AWS Amplify EventBridge Custom CloudFormation Stack

Here at StratusGrid we wanted to leverage the power of AWS EventBridge in a server-less Amplify application that we are building. Amplify doesn’t provide out-of-the-box support for EventBridge via the CLI, but it does allow you to add your own custom CloudFormation stacks manually. Our end goal was to have EventBridge event buses and rules provisioned uniquely for each Amplify environment that we currently have, or that we will create in the future. We wanted to achieve this without ever touching the AWS console.

Follow these steps to add custom EventBridge Rules to your Amplify CloudFormation Stack. This example shows how to build a rule that will invoke a Lambda function (referred to as mytriggerlambda) that has already been created via the Amplify CLI. The tutorial also requires a second Lambda function that will send/fire the event to your environment-specific event bus (referred to as mysenderlambda).

  1. Modify amplify/backend/backend-config.json in your project by adding:

2. Under amplify/backend folder, make a folder structure like the following:

3. To have a unique event bus provisioned for each of your Amplify environments, modify your new amplify/backend/eventbus/mycustomeventbus/template.json file and add the following json:

4. Modify your new amplify/backend/eventbridge/mycustomrule/parameters.json file and add a parameter for your rule’s name:

5. Modify your new amplify/backend/eventbridge/mycustomrule/template.json file and define your new EventBridge Rule resource and include required parameters:

Send a custom event to EventBridge event bus via a NodeJS Lambda

This is assuming that you already have created a function via the Amplify CLI that will send/fire the desired event to match the rule you defined above.

  1. Return to your amplify/backend/backend-config.json file and find the entry for the Lambda function that will be firing an event on your new event bus. Add the following dependency to your Lambda function resource so that your function’s CloudFormation template will receive a parameter holding the name of the event bus:

2. Modify the cloudformation template for the calling/sending lambda function in your amplify/backend/function area of your solution (ex: lambdaresourcename-cloudformation-template.json). Add a parameter reference in the Parameters section of the cloudformation template to inject the name of your event bus:

3. In the same lambda function template file, add a new environment variable for you to access in your lambda function code:

4. In the same lambda function template file, add the something like the following to the amplify-lambda-execution-policy

this will give your lambda function the permissions to call putEvents() on the configured event bus.

5. Modify the src/index.js or src/app.js file (depending on if the function is an express js lambda) of your lambda function and add the following code:

Thank you for taking the time to review this quick tutorial. For more information, contact us today!