Skip to content

Latest commit

 

History

History
 
 

eventbridge-sns-cdk

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Amazon Eventbridge to Amazon SNS

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.

Requirements

Deployment Instructions

  1. 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
  2. Change directory to the pattern directory:
    cd eventbridge-sns-cdk
  3. Create a virtual environment for python:
    python3 -m venv .venv
  4. Activate the virtual environment:
    source .venv/bin/activate
  5. Install python modules:
    python3 -m pip install -r requirements.txt
  6. From the command line, use CDK to synthesize the CloudFormation template and check for errors:
    cdk synth
  7. From the command line, use CDK to deploy the stack:
    cdk deploy
  8. Note the outputs from the CDK deployment process. These contain the resource names and/or ARNs which are used for testing.

How it works

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.

Example event payload from EventBridge to SNS

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"
}

Testing

Use the AWS CLI to send a test event to EventBridge:

  1. 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
  2. Click the confirmation link(SubscribeURL) delivered to your email to verify the endpoint.

  3. Send an event to EventBridge:

    aws events put-events --entries file://event.json
  4. The event is delivered to your email address.

Cleanup

  1. 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
  2. Deactivate the virtual environment:
    deactivate

Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0