AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::Serverless-2016-10-31 Description: S3-to-EventBridge Integration 1 - Simple Invoke Parameters: SourceBucketName: Type: String Default: patterns-s3-eventbridge-src-bucket LoggingBucketName: Type: String Default: patterns-s3-eventbridge-ct-logs Resources: # S3 Bucket - A source bucket and a logging bucket SourceBucket: Type: AWS::S3::Bucket Properties: BucketName: !Ref SourceBucketName LoggingBucket: Type: AWS::S3::Bucket Properties: BucketName: !Ref LoggingBucketName # Bucket policy enables CloudTrail to write to the logging bucket BucketPolicy: Type: AWS::S3::BucketPolicy Properties: Bucket: !Ref LoggingBucket PolicyDocument: Version: 2012-10-17 Statement: - Sid: AWSCloudTrailAclCheck Effect: Allow Principal: Service: cloudtrail.amazonaws.com Action: s3:GetBucketAcl Resource: !GetAtt LoggingBucket.Arn - Sid: AWSCloudTrailWrite Effect: Allow Principal: Service: cloudtrail.amazonaws.com Action: s3:PutObject Resource: !Sub ${LoggingBucket.Arn}/AWSLogs/${AWS::AccountId}/* Condition: StringEquals: s3:x-amz-acl: bucket-owner-full-control # The CloudTrail trail - uses the LoggingBucketName as the trail name myTrail: Type: AWS::CloudTrail::Trail DependsOn: BucketPolicy Properties: TrailName: !Ref LoggingBucketName S3BucketName: !Ref LoggingBucket IsLogging: true IsMultiRegionTrail: false EventSelectors: - IncludeManagementEvents: false DataResources: - Type: AWS::S3::Object Values: - !Sub ${SourceBucket.Arn}/ IncludeGlobalServiceEvents: false ### This section configures the consuming Lambda function ### # Lambda function EventConsumerFunction: Type: AWS::Serverless::Function Properties: CodeUri: eventConsumer/ Handler: app.handler Runtime: nodejs14.x # EventBridge rule - invokes EventConsumerFunction EventRule: Type: AWS::Events::Rule Properties: Description: EventRule State: ENABLED EventPattern: source: - aws.s3 detail: eventName: - PutObject requestParameters: bucketName: - !Ref SourceBucket Targets: - Arn: !GetAtt EventConsumerFunction.Arn Id: EventConsumerFunctionTarget PermissionForEventsToInvokeLambda: Type: AWS::Lambda::Permission Properties: FunctionName: !Ref EventConsumerFunction Action: lambda:InvokeFunction Principal: events.amazonaws.com SourceArn: !GetAtt EventRule.Arn