This CDK template deploys a SNS topic that is triggered by an EventBridge rule. The SNS topic policy provides the permission for EventBridge to invoke the SNS topic.
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/eventbridge-sns-cdk
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 for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
- Create an AWS account 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.
- AWS CLI installed and configured
- Git Installed
- AWS Cloud Development Kit (AWS CDK >= 1.124.0) Installed
- Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/aws-samples/serverless-patterns
- Change directory to the pattern directory:
cd eventbridge-sns-cdk
- Create a virtual environment for python:
python3 -m venv .venv
- Activate the virtual environment:
source .venv/bin/activate
- Install python modules:
python3 -m pip install -r requirements.txt
- From the command line, use CDK to synthesize the CloudFormation template and check for errors:
cdk synth
- From the command line, use CDK to deploy the stack:
cdk deploy
- Note the outputs from the CDK deployment process. These contain the resource names and/or ARNs which are used for testing.
The CDK stack deploys the resources and the IAM permissions required to run the application.
The EventBridge Rule specified in app.py which filters the events based upon the criteria in the rule.add_event_pattern method. When matching events are sent to the custom EventBridge Bus that trigger the Rule, they are published to target SNS topic.
Note - Account Number and SigningCertURL are masked in the example paylod.
{
"Type" : "Notification",
"MessageId" : "ecc4f3ed-47eb-5374-b5a4-aa16ec96b0a1",
"TopicArn" : "arn:aws:sns:us-east-1:<*AccountNumber*>:EventbridgeSnsCdkStack-MySnsTopicCB85459E-1VN8RJK7ZDOSB",
"Message" : "{\"version\":\"0\",\"id\":\"3a91138a-dc4b-8fff-0325-5e18f7247415\",\"detail-type\":\"message\",\"source\":\"my-application\",\"account\":\"<*AccountNumber*>\",\"time\":\"2021-11-02T21:10:21Z\",\"region\":\"us-east-1\",\"resources\":[],\"detail\":{\"message\":\"Hello from EventBridge to SNS Topic!\"}}",
"Timestamp" : "2021-11-02T21:10:21.802Z",
"SignatureVersion" : "1",
"Signature" : "OQBlVrpA8e4sFu211RkLjn2SPWVv1cThCXfG7YBaFkoulyNM+8SvlApbHLR/t+BtLyzmU8DIh7MOb8zyR4rpBhKCkjEvqNpX1AO+Da294onD+NHGrqjnRMkMyjcDzxe7poTKSEwwYBp16jjDNBBXRZmM6YX5XiL1zY41NrfJiUXro76xJr1nqr7BoxNJ1a3ACzh2cv6lyoGS0oxTgXztevUvUmfvxqtbfiFAapEjxde6IWPT6pellzy6HGfls8z7UI1NndBqfCAo4z8dCAxrczDM4Yozc+KvoquSbttKPcMkApe184teOWp0ZOT5dhO6mOV4hgCPoe9hUipoFq4Acw==",
"SigningCertURL" : "******",
"UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:<AccountNumber>:EventbridgeSnsCdkStack-MySnsTopicCB85459E-1VN8RJK7ZDOSB:175aa034-1d2d-4a83-a0e4-dbd830fce265"
}
Use the AWS CLI to send a test event to EventBridge:
-
Subscribe your email address to the SNS topic:
aws sns subscribe --topic-arn ENTER_YOUR_TOPIC_ARN --protocol email-json --notification-endpoint ENTER_YOUR_EMAIL_ADDRESS
-
Click the confirmation link(SubscribeURL) delivered to your email to verify the endpoint.
-
Send an event to EventBridge:
aws events put-events --entries file://event.json
-
The event is delivered to your email address.
- Run the given command to delete the resources that were created. It might take some time for the CloudFormation stack to get deleted.
cdk destroy
- Deactivate the virtual environment:
deactivate
Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0