-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
254 lines (240 loc) · 7.68 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: "VPC Traffic Mirroring Serverless App to automate VPC/Subnet/Tags as a traffic mirroring source.
The application can perform 3 tasks based on input parameters - set up mirroring on existing instances,
new instance launches and based on GuardDuty findings"
Parameters:
EnableForExistingInstances:
Type: String
Default: true
AllowedValues:
- true
- false
Description: Choose whether to enable traffic mirroring for existing instances.
EnableForInstanceLaunch:
Type: String
Default: true
AllowedValues:
- true
- false
Description: Chose whether to enable traffic mirroring for new instance launches.
EnableForGuardDuty:
Type: String
Default: false
AllowedValues:
- true
- false
Description: Chose whether to enable traffic mirroring for GuardDuty Findings.
Conditions:
EnableBackfillExistingInstances:
Fn::Equals:
- Ref: EnableForExistingInstances
- 'true'
EnableInstanceLaunchEvents:
Fn::Equals:
- Ref: EnableForInstanceLaunch
- 'true'
EnableGuardDutyFindingEvents:
Fn::Equals:
- Ref: EnableForGuardDuty
- 'true'
EnableCloudWatchEvents:
Fn::Or:
- Condition: EnableInstanceLaunchEvents
- Condition: EnableGuardDutyFindingEvents
Resources:
InstanceLaunchEventRule:
Condition: EnableInstanceLaunchEvents
Type: AWS::Events::Rule
Properties:
Description: "Instance launch EventRule"
EventPattern:
source:
- "aws.ec2"
detail-type:
- "EC2 Instance State-change Notification"
detail:
state:
- "running"
State: "ENABLED"
Targets:
-
Arn:
Fn::GetAtt:
- "CloudWatchEventsHandlerLambdaFunction"
- "Arn"
Id: "CloudWatchEventsHandlerLambdaFunction"
GuardDutyEventRule:
Condition: EnableGuardDutyFindingEvents
Type: AWS::Events::Rule
Properties:
Description: "GuardDuty Findings EventRule"
EventPattern:
source:
- "aws.guardduty"
detail-type:
- "GuardDuty Finding"
State: "ENABLED"
Targets:
-
Arn:
Fn::GetAtt:
- "CloudWatchEventsHandlerLambdaFunction"
- "Arn"
Id: "CloudWatchEventsHandlerLambdaFunction"
InstanceLaunchLambaInvocationPermission:
Condition: EnableInstanceLaunchEvents
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: "CloudWatchEventsHandlerLambdaFunction"
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn:
Fn::GetAtt:
- "InstanceLaunchEventRule"
- "Arn"
GuardDutyLambaInvocationPermission:
Condition: EnableGuardDutyFindingEvents
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: "CloudWatchEventsHandlerLambdaFunction"
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn:
Fn::GetAtt:
- "GuardDutyEventRule"
- "Arn"
CloudWatchEventsHandlerLambdaFunction:
Condition: EnableCloudWatchEvents
Type: AWS::Serverless::Function
Properties:
CodeUri: traffic_mirroring/
Handler: cloudwatch_event_handler.lambda_handler
Runtime: python3.7
Timeout: 300
Role:
Fn::GetAtt: [CloudWatchEventsHandlerLambdaExecutionRole, Arn]
CloudWatchEventsHandlerLambdaExecutionRole:
Condition: EnableCloudWatchEvents
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [lambda.amazonaws.com]
Version: '2012-10-17'
Policies:
- PolicyDocument:
Statement:
- Action: ['cloudwatch:*', 'logs:*']
Effect: Allow
Resource: '*'
- Action: ['ec2:CreateTrafficMirrorTarget', 'ec2:DescribeTrafficMirrorTargets', 'ec2:CreateTrafficMirrorSession']
Effect: Allow
Resource: '*'
- Action: ['ec2:CreateTags', 'ec2:DeleteTags', 'ec2:RunInstances', 'ec2:DescribeInstances', 'ec2:TerminateInstances']
Effect: Allow
Resource: '*'
Version: '2012-10-17'
PolicyName: CloudWatchEventsHandlerLambdaExecutionPolicy
TrafficMirroringBackfillLambdaFunction:
Condition: EnableBackfillExistingInstances
Type: AWS::Serverless::Function
Properties:
CodeUri: traffic_mirroring/
Handler: backfill_handler.lambda_handler
Runtime: python3.7
Timeout: 900
Role:
Fn::GetAtt: [TrafficMirroringBackfillLambdaExecutionRole, Arn]
TrafficMirroringBackfillLambdaExecutionRole:
Condition: EnableBackfillExistingInstances
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [lambda.amazonaws.com]
Version: '2012-10-17'
Policies:
- PolicyDocument:
Statement:
- Action: ['cloudwatch:*', 'logs:*']
Effect: Allow
Resource: '*'
- Action: ['ec2:CreateTrafficMirrorTarget', 'ec2:DescribeTrafficMirrorTargets', 'ec2:CreateTrafficMirrorSession']
Effect: Allow
Resource: '*'
- Action: ['ec2:CreateTags', 'ec2:DeleteTags', 'ec2:RunInstances', 'ec2:DescribeInstances', 'ec2:TerminateInstances']
Effect: Allow
Resource: '*'
- Action: ['SNS:Publish']
Effect: Allow
Resource: '*'
Version: '2012-10-17'
PolicyName: TrafficMirroringBackfillLambdaExecutionPolicy
SNSLambdaInvocationPermission:
Condition: EnableBackfillExistingInstances
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: "TrafficMirroringBackfillLambdaFunction"
Action: "lambda:InvokeFunction"
Principal: "sns.amazonaws.com"
SourceArn:
Ref: "TrafficMirroringBackfillSNSTopic"
TrafficMirroringBackfillSNSTopic:
Condition: EnableBackfillExistingInstances
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint:
Fn::GetAtt:
- "TrafficMirroringBackfillLambdaFunction"
- "Arn"
Protocol: "lambda"
BackfillInitiatorLambdaFunction:
Condition: EnableBackfillExistingInstances
Type: AWS::Serverless::Function
Properties:
CodeUri: traffic_mirroring/
Handler: backfill_initiator_handler.lambda_handler
Runtime: python3.7
Timeout: 60
Role:
Fn::GetAtt: [BackfillInitiatorLambdaExecutionRole, Arn]
# Lambda Execution Role
BackfillInitiatorLambdaExecutionRole:
Condition: EnableBackfillExistingInstances
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [lambda.amazonaws.com]
Version: '2012-10-17'
Policies:
- PolicyDocument:
Statement:
- Action: ['cloudwatch:*', 'logs:*']
Effect: Allow
Resource: '*'
- Action: ['SNS:Publish']
Effect: Allow
Resource: '*'
Version: '2012-10-17'
PolicyName: BackfillInitiatorLambdaExecutionPolicy
BackfillTriggerCustomResource:
Condition: EnableBackfillExistingInstances
Type: Custom::AppConfiguration
Properties:
ServiceToken: !GetAtt BackfillInitiatorLambdaFunction.Arn
SNSTopicArn: {Ref: TrafficMirroringBackfillSNSTopic}