|
| 1 | +# Delayed EventBridge events with EventBridge Scheduler |
| 2 | + |
| 3 | +This pattern listens for EventBridge events, processes them and creates schedules for every user. 24 hours after user has been created a schedule is run that publishes events directly into EventBridge. The pattern is deployed using Terraform. |
| 4 | + |
| 5 | +Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/delayed-eventbridge-events-terraform. |
| 6 | + |
| 7 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 12 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 13 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 14 | +* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed |
| 15 | + |
| 16 | +## Deployment Instructions |
| 17 | + |
| 18 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 19 | + ``` |
| 20 | + git clone https://github.com/aws-samples/serverless-patterns |
| 21 | + ``` |
| 22 | +1. Change directory to the pattern directory: |
| 23 | + ``` |
| 24 | + cd delayed-eventbridge-events-terraform |
| 25 | + ``` |
| 26 | +1. From the command line, initialize Terraform: |
| 27 | + ``` |
| 28 | + terraform init |
| 29 | + ``` |
| 30 | +1. From the commend line, apply the configuration in the main.tf file and follow the prompts: |
| 31 | + ``` |
| 32 | + terraform apply |
| 33 | + ``` |
| 34 | +
|
| 35 | +## How it works |
| 36 | +
|
| 37 | +We assume we have a business requirement to email a customer 24 hours after they have signed up. |
| 38 | +
|
| 39 | +1. `UserCreated` event is triggered. In this example we assume a `UserCreated` event is triggered into our event bus. |
| 40 | +2. EventBridge Rule setup to listen to the `UserCreated` event. |
| 41 | +3. Lambda function listens to `UserCreated` event and creates an Amazon EventBridge Schedule for 24 hours in the future (2 mins for development mode). |
| 42 | +4. 24 hours pass, and schedule is triggered and raises `UserCreated24HoursAgo` event directly into EventBridge.. |
| 43 | +5. Consumers listen for event and process it. An example would be email a welcome message to customers or an offer etc. |
| 44 | +
|
| 45 | +## Testing |
| 46 | +
|
| 47 | +Use the [AWS CLI](https://aws.amazon.com/cli/) to send a test event to EventBridge: |
| 48 | +
|
| 49 | +1. Send an event to EventBridge: |
| 50 | + ```bash |
| 51 | + aws events put-events --entries "Source='myapp.users',DetailType='UserCreated',Detail='{\"id\": \"test-customer-id\", \"firstName\": \"FirstName\", \"lastName\": \"LastName\"}',EventBusName='<custom-event-bus-ARN>'" |
| 52 | + ``` |
| 53 | +
|
| 54 | +2. Check the CloudWatch metrics of `UserCreatedRule` and `UserCreated24HoursAgoRule` EventBridge rule and `SchedulesForUsers24HoursAfterCreation` EventBridge Scheduler Group. Also, check the CloudWatch Logs of Lambda Functions which are targets of corresponding `UserCreatedRule` and `UserCreated24HoursAgoRule` EventBridge rules within CloudWatch Logs Console or via `aws logs start-live-tail --log-group-identifiers <Lambda Function LogGroup ARN>` CLI command. |
| 55 | +
|
| 56 | +## Cleanup |
| 57 | + |
| 58 | +1. Delete all created resources and follow prompts: |
| 59 | + ``` |
| 60 | + terraform destroy |
| 61 | + ``` |
| 62 | +---- |
| 63 | +Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 64 | +
|
| 65 | +SPDX-License-Identifier: MIT-0 |
0 commit comments