-
Notifications
You must be signed in to change notification settings - Fork 9
/
cloudformation.template
163 lines (159 loc) · 4.51 KB
/
cloudformation.template
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
AWSTemplateFormatVersion: 2010-09-09
Description: Launch Compliance Enforcer
Parameters:
LambdaScriptBucket:
Type: String
Description: s3 bucket name of the lambda script to execute
Default: lambda-scripts-region-accountid
LambdaScriptBucketKey:
Type: String
Description: s3 key of the lambda script to execute
Default: compliance-enforcer/main.zip
Resources:
LambdaExecutionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
-
PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- logs:CreateLogStream
- logs:CreateLogGroup
- logs:DescribeLogStreams
- logs:PutLogEvents
Resource:
- "arn:aws:logs:*:*:log-group:/aws/lambda/compliance-enforcer"
- "arn:aws:logs:*:*:log-group:/aws/lambda/compliance-enforcer:log-stream:*"
-
Effect: "Allow"
Action:
- ec2:RevokeSecurityGroupIngress
Resource: "*"
-
Effect: "Allow"
Action:
- s3:GetObject
Resource:
- !Sub '${ComplianceEnforcerBucket.Arn}/*'
ComplianceEnforcerBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Private
BucketName:
Fn::Join:
- ""
- - 'compliance-enforcer-'
- !Ref AWS::Region
- '-'
- !Ref AWS::AccountId
ComplianceEnforcerBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref ComplianceEnforcerBucket
PolicyDocument:
Statement:
-
Action:
- "s3:GetBucketAcl"
Effect: "Allow"
Resource:
- !Sub '${ComplianceEnforcerBucket.Arn}'
Principal:
Service: cloudtrail.amazonaws.com
-
Action:
- "s3:PutObject"
Effect: "Allow"
Resource:
- !Sub '${ComplianceEnforcerBucket.Arn}/AWSLogs/*'
Principal:
Service: cloudtrail.amazonaws.com
Condition:
StringEquals:
s3:x-amz-acl: bucket-owner-full-control
ComplianceEnforcerSNSTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: ComplianceEnforcer
TopicName: ComplianceEnforcer
ComplianceEnforcerSNSTopicPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: cloudtrail.amazonaws.com
Action: SNS:Publish
Resource: !Ref ComplianceEnforcerSNSTopic
Topics:
- !Ref ComplianceEnforcerSNSTopic
ComplianceEnforcerTrail:
Type: AWS::CloudTrail::Trail
Properties:
EnableLogFileValidation: False
IsLogging: True
S3BucketName:
Fn::Join:
- ""
- - 'compliance-enforcer-'
- !Ref AWS::Region
- '-'
- !Ref AWS::AccountId
SnsTopicName:
Fn::GetAtt:
- "ComplianceEnforcerSNSTopic"
- "TopicName"
TrailName: ComplianceEnforcer
DependsOn:
- ComplianceEnforcerBucketPolicy
- LambdaInvokePermission
LambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
FunctionName: compliance-enforcer
Handler: "main"
Role:
Fn::GetAtt:
- "LambdaExecutionRole"
- "Arn"
Code:
S3Bucket: !Ref LambdaScriptBucket
S3Key: !Ref LambdaScriptBucketKey
Runtime: "go1.x"
Timeout: 30
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt
- LambdaFunction
- Arn
Action: 'lambda:InvokeFunction'
Principal: sns.amazonaws.com
SourceArn: !Ref ComplianceEnforcerSNSTopic
LambdaSNSSubscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint: !GetAtt
- LambdaFunction
- Arn
Protocol: lambda
TopicArn: !Ref ComplianceEnforcerSNSTopic
DependsOn:
- LambdaInvokePermission