-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoFetchDomainContainerStack.cfn.yaml
317 lines (311 loc) · 9.15 KB
/
AutoFetchDomainContainerStack.cfn.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
Parameters:
ParamFirewallDomainListName:
Type: String
Description: Please enter the Firewall Domain List Name <String> to create and update
ParamS3DomainListBucketName:
Type: String
Description: Please enter the bucketname <String> for the s3 bucket that will be used to store the domains that will be fetched
ParamScheduleRate:
Type: String
Description: Please enter the rate in minutes <Number> at which the domains will be fetched
ParamLambdaImageURI:
Type: String
Description: Please enter the name of the image URI <string> that will be used
Resources:
LambdaRoleForFetchLambda:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Description: Lambda-Role
RoleName: LambdaRoleForFetchLambda
DomainListBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Private
BucketName:
Ref: ParamS3DomainListBucketName
NotificationConfiguration:
EventBridgeConfiguration:
EventBridgeEnabled: true
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
FetchLambda:
Type: AWS::Lambda::Function
Properties:
Code:
ImageUri:
Ref: ParamLambdaImageURI
Role:
Fn::GetAtt:
- LambdaRoleForFetchLambda
- Arn
Environment:
Variables:
s3Prefix:
Ref: ParamS3DomainListBucketName
region:
Ref: AWS::Region
FunctionName: FetchLambda
PackageType: Image
Timeout: 60
Route53ResolverCustomDomainList:
Type: AWS::Route53Resolver::FirewallDomainList
Properties:
Name:
Ref: ParamFirewallDomainListName
LambdaLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /aws/lambda/FetchLambda
RetentionInDays: 30
AutoFetchSchedule:
Type: AWS::Events::Rule
Properties:
Description: Scheduled rules to trigger FetchLambda to update dnsfirewall
Name: AutoFetchSchedule
ScheduleExpression:
Fn::Join:
- ""
- - rate(
- Ref: ParamScheduleRate
- " minutes)"
State: ENABLED
Targets:
- Arn:
Fn::GetAtt:
- FetchLambda
- Arn
Id: FetchLambda
Input: '{"eventDetail":"Scheduled event"}'
LambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName:
Fn::GetAtt:
- FetchLambda
- Arn
Principal: events.amazonaws.com
SourceArn:
Fn::GetAtt:
- AutoFetchSchedule
- Arn
UpdateDNSFWStackLambdaFWRoleForLambdaUpdateDnsFwEDFC1518:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Description: Lambda-Role
RoleName: LambdaFWRoleForLambdaUpdateDnsFw
UpdateDNSFWStackLambdaAutoUpdateDnsFwDAA31F9E:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |-
// Package
const AWS = require('aws-sdk');
// Const
const FirewallDomainListId = process.env.domain_list_id
const region = process.env.region
// AWS Object
const route53Resolver = new AWS.Route53Resolver({ "region": region });
async function update_route53_domains(filekey, bucketname) {
let route53Resolver_params = {
DomainFileUrl: "s3://"+bucketname+"/" + filekey,
FirewallDomainListId: FirewallDomainListId,
Operation: "REPLACE"
}
await route53Resolver.importFirewallDomains(route53Resolver_params).promise()
console.log("[INFO] DNS Firewall of id : " + FirewallDomainListId + " updated with file " + filekey)
}
exports.handler = async (event) => {
let filekey = event.detail.object.key
let bucketname = event.detail.bucket.name
await update_route53_domains(filekey, bucketname)
};
Role:
Fn::GetAtt:
- UpdateDNSFWStackLambdaFWRoleForLambdaUpdateDnsFwEDFC1518
- Arn
Environment:
Variables:
domain_list_id:
Fn::GetAtt:
- Route53ResolverCustomDomainList
- Id
region:
Ref: AWS::Region
FunctionName: LambdaAutoUpdateDnsFw
Handler: index.handler
Runtime: nodejs14.x
UpdateDNSFWStackLambdaLogGroupB21E4462:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /aws/lambda/LambdaAutoUpdateDnsFw
RetentionInDays: 30
UpdateDNSFWStackUpdateDnsFWRuleC7786E69:
Type: AWS::Events::Rule
Properties:
Description: ""
EventPattern:
source:
- aws.s3
account:
- Ref: AWS::AccountId
detail-type:
- Object Created
detail:
bucket:
name:
- Ref: ParamS3DomainListBucketName
Name: UpdateDnsFWRule
State: ENABLED
Targets:
- Arn:
Fn::GetAtt:
- UpdateDNSFWStackLambdaAutoUpdateDnsFwDAA31F9E
- Arn
Id: LambdaUpdateDnsFw
UpdateDNSFWStackLambdaPermission6E98C382:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName:
Fn::GetAtt:
- UpdateDNSFWStackLambdaAutoUpdateDnsFwDAA31F9E
- Arn
Principal: events.amazonaws.com
SourceArn:
Fn::GetAtt:
- UpdateDNSFWStackUpdateDnsFWRuleC7786E69
- Arn
UpdateDNSFWStackPolicylambdaFW73FAD557:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
Fn::Join:
- ""
- - "arn:aws:logs:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- :*
- Effect: Allow
Action:
- s3:GetObject
Resource:
Fn::Join:
- ""
- - Fn::GetAtt:
- DomainListBucket
- Arn
- /*
- Effect: Allow
Action:
- route53resolver:ImportFirewallDomains
Resource:
Fn::Join:
- ""
- - "arn:aws:route53resolver:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- :firewall-domain-list/
- Fn::GetAtt:
- Route53ResolverCustomDomainList
- Id
PolicyName: PolicylambdaFW
Roles:
- Ref: UpdateDNSFWStackLambdaFWRoleForLambdaUpdateDnsFwEDFC1518
DomainListBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: ParamS3DomainListBucketName
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: Bucketpolicy-1
Effect: Allow
Principal:
AWS:
Fn::GetAtt:
- LambdaRoleForFetchLambda
- Arn
Action:
- s3:PutObject
Resource:
Fn::Join:
- ""
- - "arn:aws:s3:::"
- Ref: ParamS3DomainListBucketName
- /*
- Sid: Bucketpolicy-2
Effect: Allow
Principal:
AWS:
Fn::GetAtt:
- UpdateDNSFWStackLambdaFWRoleForLambdaUpdateDnsFwEDFC1518
- Arn
Action:
- s3:GetObject
Resource:
Fn::Join:
- ""
- - "arn:aws:s3:::"
- Ref: ParamS3DomainListBucketName
- /*
Policylambda:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
Fn::Join:
- ""
- - "arn:aws:logs:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- :*
- Effect: Allow
Action:
- s3:PutObject
Resource:
Fn::Join:
- ""
- - Fn::GetAtt:
- DomainListBucket
- Arn
- /*
PolicyName: Sample-PolicyForAutomation
Roles:
- Ref: LambdaRoleForFetchLambda