AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Streams DynamoDB table changes to EventBridge Resources: #DynamoDB Streams specification MyDynamoStream: Type: 'AWS::DynamoDB::Table' Properties: AttributeDefinitions: - AttributeName: name AttributeType: S KeySchema: - AttributeName: name KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 StreamSpecification: StreamViewType: NEW_AND_OLD_IMAGES # Define the publisher Lambda function with permissions EventProcessingFunction: Type: 'AWS::Serverless::Function' Properties: Handler: app.handler Runtime: nodejs14.x CodeUri: publish/ Description: A Lambda function that forward changes on DynamoDB table to EventBridge bus. MemorySize: 128 Timeout: 3 Events: TableRecordChange: Type: DynamoDB Properties: StartingPosition: LATEST Stream: !GetAtt MyDynamoStream.StreamArn Policies: - Statement: - Effect: Allow Resource: '*' Action: - events:PutEvents #Define the Lambda subscribers for INSERT and REMOVE events from table SubscribeInsertFunction: Type: AWS::Serverless::Function Properties: CodeUri: subscribe/ Handler: app.handler Runtime: nodejs14.x Description: A Lambda function that receive Insert events from the table. MemorySize: 128 Timeout: 3 SubscribeRemoveFunction: Type: AWS::Serverless::Function Properties: CodeUri: subscribe/ Handler: app.handler Runtime: nodejs14.x Description: A Lambda function that receive Deletion events from the table. MemorySize: 128 Timeout: 3 #EventBridge specification MyEventBus: Type: AWS::Events::EventBus Properties: Name: MyEventBus #EventBridge rules and permissions EventRuleInsert: Type: AWS::Events::Rule Properties: Description: "Insert transactions" EventBusName: !GetAtt MyEventBus.Arn EventPattern: source: - "MyDynamoStream" detail-type: - transaction detail: eventName: - "INSERT" State: "ENABLED" Targets: - Arn: !GetAtt SubscribeInsertFunction.Arn Id: "eventRuleInsert" PermissionForInsertEventsToInvokeLambda: Type: AWS::Lambda::Permission Properties: FunctionName: !Ref SubscribeInsertFunction Action: "lambda:InvokeFunction" Principal: "events.amazonaws.com" SourceArn: !GetAtt EventRuleInsert.Arn EventRuleRemove: Type: AWS::Events::Rule Properties: Description: "Remove transactions" EventBusName: !GetAtt MyEventBus.Arn EventPattern: source: - "MyDynamoStream" detail-type: - transaction detail: eventName: - "REMOVE" State: "ENABLED" Targets: - Arn: !GetAtt SubscribeRemoveFunction.Arn Id: "eventRuleRemove" PermissionForRemoveEventsToInvokeLambda: Type: AWS::Lambda::Permission Properties: FunctionName: !Ref SubscribeRemoveFunction Action: "lambda:InvokeFunction" Principal: "events.amazonaws.com" SourceArn: !GetAtt EventRuleRemove.Arn