-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathtemplate.yaml
97 lines (88 loc) · 2.71 KB
/
template.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: S3-to-EventBridge Integration 2 - Existing Buckets
Parameters:
ExistingBucketName:
Type: String
Default: textract-testing-jbesw
LoggingBucketName:
Type: String
Default: patterns-s3-eventbridge-ct-logs-2
Resources:
# Bucket for CloudTrail Logs
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 arn:aws:s3:::${ExistingBucketName}/
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 ExistingBucketName
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