Skip to content

Instantly share code, notes, and snippets.

@nickste
Created November 30, 2021 17:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickste/eccc646bff0fad522f5b5b95a8cf0068 to your computer and use it in GitHub Desktop.
Save nickste/eccc646bff0fad522f5b5b95a8cf0068 to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: '2010-09-09'
Description: >
S3 Event Notifications to EventBridge
Resources:
EventBucket:
Type: AWS::S3::Bucket
Properties:
NotificationConfiguration:
# New configuration to enable EventBridge for S3 Event Notifications
EventBridgeConfiguration:
EventBridgeEnabled: true
# EventBridge rule to match against S3 events and route them to CloudWatch Logs
EventBridgeRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
source:
- aws.s3
detail-type:
- Object Created
RoleArn: !GetAtt WriteToCwlRole.Arn
Targets:
- Id: SendEventToSecurityAnalysisRule
Arn: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:${EventLog}"
# CloudWatch Log group
EventLog:
Type: AWS::Logs::LogGroup
# IAM role assumed by EventBridge to make calls to CloudWatch Logs
WriteToCwlRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- events.amazonaws.com
Action:
- "sts:AssumeRole"
Path: /
Policies:
- PolicyName: EventBridgeWriteToCWLogs
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStreams"
- "logs:PutLogEvents"
Resource:
- !GetAtt EventLog.Arn
# Resource policy to allow EventBridge to call CloudWatch logs.
CWLogsResourcePolicy:
Type: AWS::Logs::ResourcePolicy
Properties:
PolicyName: "EventBridgeToCWLogs"
PolicyDocument: !Sub
- >
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EventBridgetoCWLogsPolicy",
"Effect": "Allow",
"Principal": {
"Service": [
"delivery.logs.amazonaws.com",
"events.amazonaws.com"
]
},
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
"${logArn}"
]
}
]
}
- logArn: !GetAtt EventLog.Arn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment