This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrubrik-aws-ec2-snap-replicate.yml
461 lines (459 loc) · 16.8 KB
/
rubrik-aws-ec2-snap-replicate.yml
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
Description: Deploys pre-requisites for copying snapshots created by Rubrik Cloud
Native Protection
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Lambda Function Source Parameters
Parameters:
- lambdaBucketName
- rubrikIamUser
- Label:
default: Snapshot Destination Information
Parameters:
# - destAccountNumber
- destRegion
- destEncryptSnaps
- destKmsKey
Parameters:
lambdaBucketName:
Description: "Name of the bucket containing the lambda zip file, must be in the region you are creating this stack in"
Type: String
AllowedPattern: ".+"
rubrikIamUser:
Description: Name of the IAM user that Rubrik Cloud Native Protection is configured
to use.
Type: String
AllowedPattern: .+
# destAccountNumber:
# Description: AWS Account Number where the snapshots will be replicated to.
# Type: String
# Default: !Ref 'AWS::AccountId'
# AllowedPattern: .+
destRegion:
Description: Name of the bucket containing the lambda zip file, must be in the
region you are creating this stack in.
Type: String
AllowedValues:
- ap-northeast-1
- ap-northeast-2
- ap-northeast-3
- ap-south-1
- ap-southeast-1
- ap-southeast-2
- ca-central-1
- cn-north-1
- cn-northwest-1
- eu-central-1
- eu-north-1
- eu-west-1
- eu-west-2
- eu-west-3
- sa-east-1
- us-east-1
- us-east-2
- us-west-1
- us-west-2
- us-gov-east-1
- us-gov-west-1
AllowedPattern: .+
destEncryptSnaps:
Description: Encrypt snapshots on the destination.
Type: String
AllowedValues:
- 'True'
- 'False'
AllowedPattern: .+
destKmsKey:
Description: >-
KMS Key in the destination region if snapshots are to be encrypted. If no key
is specified the default key will be used. Encrypted source snapshots will be
encrypted with the specified key or the default key in the region if none is
specified.
Type: String
Default: 'Default'
AllowedPattern: Default|^[a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{12}$
Resources:
rubrikEc2SnapReplicatePolicy:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: "GlobalPermissions"
Effect: Allow
Action:
- ec2:CopyImage
- ec2:CopySnapshot
- ec2:DeregisterImage
- ec2:DescribeImages
- ec2:DescribeSnapshots
- kms:ListKeys
- kms:ListAliases
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: '*'
- Sid: "EC2Permissions"
Effect: Allow
Action: ec2:DeleteSnapshot
Resource: !Join [':', ['arn:aws:ec2', Ref: 'destRegion', ':snapshot/*']]
- Sid: "RolePermissions"
Effect: Allow
Action: sts:AssumeRole
Resource: !Join [':', ['arn:aws:iam:', Ref: 'AWS::AccountId', 'role/rubrikEc2SnapReplicateRole']]
- Sid: "StateMachinePermission"
Effect: Allow
Action: states:StartExecution
Resource: !Join [':', ['arn:aws:states',Ref: 'AWS::Region', Ref: 'AWS::AccountId', 'stateMachine:rk_CopySnap']]
- Sid: "LambdaPermissions"
Effect: Allow
Action: lambda:InvokeFunction
Resource:
- !Join [':', ['arn:aws:lambda', Ref: 'AWS::Region', Ref: 'AWS::AccountId', 'function:rk_CopyAmi']]
- !Join [':', ['arn:aws:lambda', Ref: 'AWS::Region', Ref: 'AWS::AccountId', 'function:rk_DeleteReplica']]
- !Join [':', ['arn:aws:lambda', Ref: 'AWS::Region', Ref: 'AWS::AccountId', 'function:rk_GetAmiStatus']]
- !Join [':', ['arn:aws:lambda', Ref: 'AWS::Region', Ref: 'AWS::AccountId', 'function:rk_GetDestAmiStatus']]
- !Join [':', ['arn:aws:lambda', Ref: 'AWS::Region', Ref: 'AWS::AccountId', 'function:rk_TagSnaps']]
- Sid: "TagPermissions"
Effect: Allow
Action: ec2:CreateTags
Resource:
- !Join [':', ['arn:aws:ec2', Ref: 'destRegion',':image/*']]
- !Join [':', ['arn:aws:ec2', Ref: 'destRegion',':snapshot/*']]
- Sid: "SourceKMSPermissions"
Effect: Allow
Action:
- kms:DescribeKey
- kms:CreateGrant
- kms:Decrypt
Resource:
- !Join [':', ['arn:aws:kms', Ref: 'AWS::Region', Ref: 'AWS::AccountId', 'key/*']]
- Sid: "TargetKMSPermissions"
Effect: Allow
Action:
- kms:CreateGrant
- kms:Encrypt
- kms:Decrypt
- kms:DescribeKey
- kms:GenerateDataKeyWithoutPlaintext
Resource:
- !Join [':', ['arn:aws:kms', Ref: 'destRegion', Ref: 'AWS::AccountId', 'key/*']]
PolicyName: rubrikEc2SnapReplicatePolicy
Roles:
- !Ref 'rubrikEc2SnapReplicateRole'
rubrikEc2SnapReplicateRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- events.amazonaws.com
- lambda.amazonaws.com
- states.amazonaws.com
Action: sts:AssumeRole
Policies: []
RoleName: rubrikEc2SnapReplicateRole
rubrikEc2CopyAmiFunction:
Type: AWS::Lambda::Function
DependsOn: rubrikEc2SnapReplicatePolicy
Properties:
Handler: rk_CopyAmi.lambda_handler
Code:
S3Bucket: !Ref 'lambdaBucketName'
S3Key: "rubrik-aws-ec2-snap-replicate.zip"
Description: Lambda function used by Rubrik AWS Snap Replicate to copy EC2 Native
snapshots
FunctionName: rk_CopyAmi
Role: !GetAtt 'rubrikEc2SnapReplicateRole.Arn'
Runtime: python3.6
Timeout: '180'
rubrikEc2DeleteReplicaFunction:
Type: AWS::Lambda::Function
DependsOn: rubrikEc2SnapReplicatePolicy
Properties:
Handler: rk_DeleteReplica.lambda_handler
Code:
S3Bucket: !Ref 'lambdaBucketName'
S3Key: rubrik-aws-ec2-snap-replicate.zip
Description: Lambda function used by Rubrik AWS Snap Replicate to delete replicated
EC2 Native snapshots
FunctionName: rk_DeleteReplica
Role: !GetAtt 'rubrikEc2SnapReplicateRole.Arn'
Runtime: python3.6
Timeout: '180'
rubrikEc2GetAmiStatusFunction:
Type: AWS::Lambda::Function
DependsOn: rubrikEc2SnapReplicatePolicy
Properties:
Handler: rk_GetAmiStatus.lambda_handler
Code:
S3Bucket: !Ref 'lambdaBucketName'
S3Key: rubrik-aws-ec2-snap-replicate.zip
Description: Lambda function used by Rubrik AWS Snap Replicate to get copy status
of EC2 Native snapshots
FunctionName: rk_GetAmiStatus
Role: !GetAtt 'rubrikEc2SnapReplicateRole.Arn'
Runtime: python3.6
Timeout: '180'
rubrikEc2GetDestAmiStatusFunction:
Type: AWS::Lambda::Function
DependsOn: rubrikEc2SnapReplicatePolicy
Properties:
Handler: rk_GetDestAmiStatus.lambda_handler
Code:
S3Bucket: !Ref 'lambdaBucketName'
S3Key: rubrik-aws-ec2-snap-replicate.zip
Description: Lambda function used by Rubrik AWS Snap Replicate to get destination AMI copy status
of EC2 Native snapshots
FunctionName: rk_GetDestAmiStatus
Role: !GetAtt 'rubrikEc2SnapReplicateRole.Arn'
Runtime: python3.6
Timeout: '180'
rubrikEc2TagSnapsFunction:
Type: AWS::Lambda::Function
DependsOn: rubrikEc2SnapReplicatePolicy
Properties:
Handler: rk_TagSnaps.lambda_handler
Code:
S3Bucket: !Ref 'lambdaBucketName'
S3Key: rubrik-aws-ec2-snap-replicate.zip
Description: Lambda function used by Rubrik AWS Snap Replicate to tag snapshots of destination AMI
of EC2 Native snapshots
FunctionName: rk_TagSnaps
Role: !GetAtt 'rubrikEc2SnapReplicateRole.Arn'
Runtime: python3.6
Timeout: '180'
lambdaInvokePermissionCopyAmi:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt 'rubrikEc2CopyAmiFunction.Arn'
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
lambdaInvokePermissionDeleteReplica:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt 'rubrikEc2DeleteReplicaFunction.Arn'
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
lambdaInvokePermissionGetAmiStatus:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt 'rubrikEc2GetAmiStatusFunction.Arn'
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
lambdaInvokePermissionGetDestAmiStatus:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt 'rubrikEc2GetDestAmiStatusFunction.Arn'
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
lambdaInvokePermissionTagSnaps:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt 'rubrikEc2TagSnapsFunction.Arn'
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
cloudWatchEventRuleReplicateSnaps:
Type: AWS::Events::Rule
DependsOn: stepFunctionCopySnap
Properties:
Description: Replicates snapshots created by Rubrik Cloud Native Protection.
EventPattern:
source:
- aws.ec2
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- ec2.amazonaws.com
eventName:
- CreateTags
requestParameters:
tagSet:
items:
key:
- rk_cloud_native_feature_type
value:
- CREATE_CLOUD_NATIVE_VM_SNAPSHOT
State: ENABLED
Targets:
- Arn: !Ref 'stepFunctionCopySnap'
Id: rk_stepFunctionCopySnap
InputTransformer:
InputPathsMap:
resourceId: $.detail.requestParameters.resourcesSet.items[0].resourceId
source_region: $.region
InputTemplate:
Fn::Sub: |
{ "resource_id" : <resourceId>, "source_region" : <source_region>, "destination_region" : "${destRegion}", "kmsEnabled" : "${destEncryptSnaps}", "kmsKey" : "${destKmsKey}", "debug" : "false" }
RoleArn: !GetAtt 'rubrikEc2SnapReplicateRole.Arn'
cloudWatchEventRuleDeleteSnap:
Type: AWS::Events::Rule
Properties:
Description: Delete expired replicated snapshots created by Rubrik Cloud Native
Protection.
EventPattern:
Fn::Sub: |
{
"detail-type": [
"AWS API Call via CloudTrail"
],
"source": [
"aws.ec2"
],
"detail": {
"responseElements": {
"_return": [
true
]
},
"eventSource": [
"ec2.amazonaws.com"
],
"eventName": [
"DeregisterImage"
],
"userIdentity": {
"userName": [
"${rubrikIamUser}"
]
}
}
}
State: ENABLED
Targets:
- Arn: !GetAtt 'rubrikEc2DeleteReplicaFunction.Arn'
Id: rk_DeleteReplica
InputTransformer:
InputPathsMap:
resourceId: $.detail.requestParameters.imageId
source_region: $.region
InputTemplate:
Fn::Sub: |
{ "resource_id" : <resourceId>, "source_region" : <source_region>, "destination_region" : "${destRegion}", "debug" : "false" }
stepFunctionCopySnap:
Type: AWS::StepFunctions::StateMachine
DependsOn: rubrikEc2CopyAmiFunction
Properties:
StateMachineName: rk_CopySnap
DefinitionString:
Fn::Sub: |
{
"Comment": "Copy Rubrik created AMIs and snapshots to a remote region once the AMI becomes available",
"StartAt": "rk_GetAmiStatus",
"States": {
"rk_StatusChoice": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.rkstatus",
"StringEquals": "available",
"Next": "rk_CopyAmi"
},
{
"Variable": "$.rkstatus",
"StringEquals": "pending",
"Next": "rk_Wait"
},
{
"Variable": "$.rkstatus",
"StringEquals": "failed",
"Next": "rk_Fail"
},
{
"Variable": "$.rkstatus",
"StringEquals": "noami",
"Next": "rk_NoAmi"
}
]
},
"rk_DestStatusChoice": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.rkstatus",
"StringEquals": "failed",
"Next": "rk_Fail"
},
{
"Variable": "$.rkstatus",
"StringEquals": "dest_pending",
"Next": "rk_DestWait"
},
{
"Variable": "$.rkstatus",
"StringEquals": "dest_available",
"Next": "rk_TagSnaps"
}
]
},
"rk_CopyAmi": {
"Type": "Task",
"Resource": "${rubrikEc2CopyAmiFunction.Arn}",
"Next": "rk_DestWait"
},
"rk_GetAmiStatus": {
"Type": "Task",
"Resource": "${rubrikEc2GetAmiStatusFunction.Arn}",
"Next": "rk_StatusChoice"
},
"rk_GetDestAmiStatus": {
"Type": "Task",
"Resource": "${rubrikEc2GetDestAmiStatusFunction.Arn}",
"Next": "rk_DestStatusChoice"
},
"rk_TagSnaps": {
"Type": "Task",
"Resource": "${rubrikEc2TagSnapsFunction.Arn}",
"End": true
},
"rk_Wait": {
"Type" : "Wait",
"Seconds" : 60,
"Next": "rk_GetAmiStatus"
},
"rk_DestWait": {
"Type" : "Wait",
"Seconds" : 60,
"Next": "rk_GetDestAmiStatus"
},
"rk_Fail": {
"Type": "Fail",
"Cause": "AMI in failed state",
"Error": "failedami"
},
"rk_NoAmi": {
"Type": "Succeed"
}
}
}
RoleArn: !GetAtt 'rubrikEc2SnapReplicateRole.Arn'
Outputs:
rubrikEc2SnapReplicateRoleARN:
Description: ARN of IAM role used by Lambda
Value: !GetAtt 'rubrikEc2SnapReplicateRole.Arn'
rubrikEc2CopyAmiFunctionARN:
Description: ARN of the Lambda function used to copy AMIs for EC2 native protection
Value: !GetAtt 'rubrikEc2CopyAmiFunction.Arn'
rubrikEc2TagSnapsFunctionARN:
Description: ARN of the Lambda function used to copy AMIs for EC2 native protection
Value: !GetAtt 'rubrikEc2TagSnapsFunction.Arn'
rubrikEc2DeleteReplicaFunctionARN:
Description: ARN of the Lambda function used to delete AMIs for EC2 native protection
Value: !GetAtt 'rubrikEc2DeleteReplicaFunction.Arn'
rubrikEc2GetAmiStatusFunctionARN:
Description: ARN of the Lambda function used to get status of AMI copy jobs for EC2 native protection
Value: !GetAtt 'rubrikEc2GetAmiStatusFunction.Arn'
rubrikEc2GetDestAmiStatusFunctionARN:
Description: ARN of the Lambda function used to get status of AMI copy jobs for EC2 native protection
Value: !GetAtt 'rubrikEc2GetDestAmiStatusFunction.Arn'
cloudWatchEventRuleReplicateSnapsARN:
Description: ARN of the CloudWatch Rule used to trigger the replication of AMIs for EC2 native protection
Value: !GetAtt 'cloudWatchEventRuleReplicateSnaps.Arn'
cloudWatchEventRuleDeleteSnapARN:
Description: ARN of the CloudWatch Rule used to trigger the deletion of AMIs for EC2 native protection
Value: !GetAtt 'cloudWatchEventRuleDeleteSnap.Arn'