-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDMSCDC_CloudTemplate_Reusable.yaml
594 lines (567 loc) · 22.3 KB
/
DMSCDC_CloudTemplate_Reusable.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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
AWSTemplateFormatVersion: 2010-09-09
Parameters:
SecurityGroup:
Description: Sec Group for Replication Instance with rules to allow it to connect to your source DB.
Type: AWS::EC2::SecurityGroup::Id
Subnet1:
Description: Subnet-1 for Replication Instance with network path to source DB.
Type: AWS::EC2::Subnet::Id
Subnet2:
Description: Subnet-2 for Replication Instance with network path to source DB.
Type: AWS::EC2::Subnet::Id
Resources:
LambdaRoleToInitDMS:
Type: AWS::IAM::Role
Properties :
AssumeRolePolicyDocument:
Version : 2012-10-17
Statement :
-
Effect : Allow
Principal :
Service :
- lambda.amazonaws.com
Action :
- sts:AssumeRole
Path : /
Policies:
-
PolicyName: LambdaCloudFormationPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action:
- s3:*
Resource:
- !Sub "arn:aws:s3:::cloudformation-custom-resource-response-${AWS::Region}"
- !Sub "arn:aws:s3:::cloudformation-waitcondition-${AWS::Region}"
- !Sub "arn:aws:s3:::cloudformation-custom-resource-response-${AWS::Region}/*"
- !Sub "arn:aws:s3:::cloudformation-waitcondition-${AWS::Region}/*"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/CloudWatchLogsFullAccess
- arn:aws:iam::aws:policy/AmazonRDSDataFullAccess
- arn:aws:iam::aws:policy/IAMFullAccess
- arn:aws:iam::aws:policy/AmazonRedshiftFullAccess
LambdaFunctionDMSRoles:
Type: "AWS::Lambda::Function"
Properties:
Timeout: 30
Code:
ZipFile: |
import json
import boto3
import cfnresponse
import logging
import time
client = boto3.client('iam')
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def handler(event, context):
logger.info(json.dumps(event))
if event['RequestType'] == 'Delete':
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'Data': 'Delete complete'})
else:
try:
response = client.get_role(RoleName='dms-cloudwatch-logs-role')
except:
try:
role_policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"dms.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
client.create_role(
RoleName='dms-cloudwatch-logs-role',
AssumeRolePolicyDocument=json.dumps(role_policy_document)
)
client.attach_role_policy(
RoleName='dms-cloudwatch-logs-role',
PolicyArn='arn:aws:iam::aws:policy/service-role/AmazonDMSCloudWatchLogsRole'
)
except Exception as e:
logger.error(e)
cfnresponse.send(event, context, cfnresponse.FAILED, {'Data': 'Create failed'})
try:
response = client.get_role(RoleName='dms-vpc-role')
except:
try:
role_policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"dms.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
client.create_role(
RoleName='dms-vpc-role',
AssumeRolePolicyDocument=json.dumps(role_policy_document)
)
client.attach_role_policy(
RoleName='dms-vpc-role',
PolicyArn='arn:aws:iam::aws:policy/service-role/AmazonDMSVPCManagementRole'
)
time.sleep(30)
except Exception as e:
logger.error(e)
cfnresponse.send(event, context, cfnresponse.FAILED, {'Data': 'Create failed'})
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'Data': 'Create complete'})
Handler: index.handler
Role:
Fn::GetAtt: [LambdaRoleToInitDMS, Arn]
Runtime: python3.7
InitDMSRoles:
Type: Custom::InitDMSRoles
DependsOn:
- LambdaFunctionDMSRoles
Properties:
ServiceToken: !GetAtt 'LambdaFunctionDMSRoles.Arn'
DMSSubnetGroup:
Type: AWS::DMS::ReplicationSubnetGroup
DependsOn:
- InitDMSRoles
Properties:
ReplicationSubnetGroupDescription: Subnet group for DMSCDC pipeline.
SubnetIds:
- !Ref Subnet1
- !Ref Subnet2
DMSReplicationInstance:
Type: AWS::DMS::ReplicationInstance
DependsOn:
- InitDMSRoles
Properties:
ReplicationInstanceIdentifier: DMSCDC-ReplicationInstance
ReplicationInstanceClass: dms.c4.large
ReplicationSubnetGroupIdentifier: !Ref DMSSubnetGroup
VpcSecurityGroupIds:
- !Ref SecurityGroup
ExecutionRole:
Type: AWS::IAM::Role
Properties :
RoleName: DMSCDC_Execution_Role
AssumeRolePolicyDocument:
Version : 2012-10-17
Statement :
-
Effect : Allow
Principal :
Service :
- lambda.amazonaws.com
- glue.amazonaws.com
- dms.amazonaws.com
Action :
- sts:AssumeRole
Path : /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
Policies :
-
PolicyName: DMSCDCExecutionPolicy
PolicyDocument :
Version: 2012-10-17
Statement:
-
Effect: Allow
Action:
- lambda:InvokeFunction
- dynamodb:PutItem
- dynamodb:CreateTable
- dynamodb:UpdateItem
- dynamodb:UpdateTable
- dynamodb:GetItem
- dynamodb:DescribeTable
- iam:GetRole
- iam:PassRole
- dms:StartReplicationTask
- dms:TestConnection
- dms:StopReplicationTask
Resource:
- !Sub 'arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/DMSCDC_Controller'
- !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:DMSCDC_InitController'
- !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:DMSCDC_InitDMS'
- !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:DMSCDC_ImportGlueScript'
- !Sub 'arn:aws:iam::${AWS::AccountId}:role/DMSCDC_ExecutionRole'
- !Sub 'arn:aws:dms:${AWS::Region}:${AWS::AccountId}:endpoint:*'
- !Sub 'arn:aws:dms:${AWS::Region}:${AWS::AccountId}:task:*'
-
Effect: Allow
Action:
- dms:DescribeConnections
- dms:DescribeReplicationTasks
Resource: "*"
LambdaFunctionImportGlueScript:
Type: "AWS::Lambda::Function"
Properties:
FunctionName: DMSCDC_ImportGlueScript
Timeout: 30
Code:
ZipFile: |
import json
import boto3
import cfnresponse
import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def handler(event, context):
logger.info(json.dumps(event))
s3 = boto3.client('s3')
glueBucket = event['ResourceProperties']['glueBucket']
if event['RequestType'] == 'Delete':
try:
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_LoadIncremental.py')
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_LoadIncremental.py.temp')
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_LoadInitial.py')
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_LoadInitial.py.temp')
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_ProcessTable.py')
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_ProcessTable.py.temp')
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_Controller.py')
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_Controller.py.temp')
except Exception as e:
logger.info(e.message)
logger.info('Delete Complete')
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'Data': 'Delete Complete'})
else:
try:
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_LoadIncremental.py')
except Exception as e:
logger.info(e.message)
try:
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_LoadInitial.py')
except Exception as e:
logger.info(e.message)
try:
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_Controller.py')
except Exception as e:
logger.info(e.message)
try:
s3.delete_object(Bucket=glueBucket, Key='DMSCDC_ProcessTable.py')
except Exception as e:
logger.info(e.message)
try:
s3.copy_object(Bucket=glueBucket, CopySource="dmscdc-files/DMSCDC_LoadIncremental.py", Key="DMSCDC_LoadIncremental.py")
s3.copy_object(Bucket=glueBucket, CopySource="dmscdc-files/DMSCDC_LoadInitial.py", Key="DMSCDC_LoadInitial.py")
s3.copy_object(Bucket=glueBucket, CopySource="dmscdc-files/DMSCDC_Controller.py", Key="DMSCDC_Controller.py")
s3.copy_object(Bucket=glueBucket, CopySource="dmscdc-files/DMSCDC_ProcessTable.py", Key="DMSCDC_ProcessTable.py")
logger.info('Copy Complete')
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'Data': 'Copy complete'})
except Exception as e:
message = 'S3 Issue: ' + str(e)
logger.error(message)
cfnresponse.send(event, context, cfnresponse.FAILED, {'Data': message})
Handler: index.handler
Role:
Fn::GetAtt: [ExecutionRole, Arn]
Runtime: python3.7
DependsOn:
- ExecutionRole
ImportScript:
Type: Custom::CopyScript
DependsOn:
- LambdaFunctionImportGlueScript
- GlueBucket
Properties:
ServiceToken:
Fn::GetAtt : [LambdaFunctionImportGlueScript, Arn]
glueBucket:
Ref: GlueBucket
LambdaFunctionInitController:
Type: "AWS::Lambda::Function"
Properties:
FunctionName: DMSCDC_InitController
Timeout: 600
Code:
ZipFile: |
import json
import boto3
import cfnresponse
import time
import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
glue = boto3.client('glue')
def testGlueJob(jobId, count, sec):
i = 0
while i < count:
response = glue.get_job_run(JobName='DMSCDC_Controller', RunId=jobId)
status = response['JobRun']['JobRunState']
if (status == 'SUCCEEDED' or status == 'STARTING' or status == 'STOPPING'):
return 1
elif status == 'RUNNING' :
time.sleep(sec)
i+=1
else:
return 0
if i == count:
return 0
def handler(event, context):
logger.info(json.dumps(event))
if event['RequestType'] != 'Create':
logger.info('No action necessary')
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'Data': 'NA'})
else:
try:
dmsBucket = event['ResourceProperties']['dmsBucket']
dmsPath = event['ResourceProperties']['dmsPath']
lakeBucket = event['ResourceProperties']['lakeBucket']
lakePath = event['ResourceProperties']['lakePath']
lakeDBName = event['ResourceProperties']['lakeDBName']
response = glue.start_job_run(
JobName='DMSCDC_Controller',
Arguments={
'--bucket':dmsBucket,
'--prefix':dmsPath,
'--out_bucket':lakeBucket,
'--out_prefix':lakePath})
if testGlueJob(response['JobRunId'], 20, 30) != 1:
message = 'Error during Controller execution'
logger.info(message)
cfnresponse.send(event, context, cfnresponse.FAILED, {'Data': message})
else:
try:
message = 'Load Complete: https://console.aws.amazon.com/dynamodb/home?#tables:selected=DMSCDC_Controller;tab=items'
response = glue.start_trigger(Name='DMSCDC-Trigger-'+lakeDBName)
logger.info(message)
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'Data': message})
except:
message = 'Load Complete, but trigger failed to initialize'
logger.info(message)
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'Data': message})
except Exception as e:
message = 'Glue Job Issue: ' + str(e)
logger.info(message)
cfnresponse.send(event, context, cfnresponse.FAILED, {'Data': message})
Handler: index.handler
Role:
Fn::GetAtt: [ExecutionRole, Arn]
Runtime: python3.7
DependsOn:
- ExecutionRole
LambdaFunctionInitDMS:
Type: "AWS::Lambda::Function"
Properties:
FunctionName: DMSCDC_InitDMS
Timeout: 600
Code:
ZipFile: |
import json
import boto3
import cfnresponse
import time
import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
dms = boto3.client('dms')
s3 = boto3.resource('s3')
def testConnection(endpointArn, count):
i = 0
while i < count:
connections = dms.describe_connections(Filters=[{'Name':'endpoint-arn','Values':[endpointArn]}])
for connection in connections['Connections']:
if connection['Status'] == 'successful':
return 1
elif connection['Status'] == 'testing':
time.sleep(30)
i += 1
elif connection['Status'] == 'failed':
return 0
if i == count:
return 0
def testRepTask(taskArn, count):
i = 0
while i < count:
replicationTasks = dms.describe_replication_tasks(Filters=[{'Name':'replication-task-arn','Values':[taskArn]}])
for replicationTask in replicationTasks['ReplicationTasks']:
if replicationTask['Status'] == 'running':
return 1
elif replicationTask['Status'] == 'starting':
time.sleep(30)
i += 1
else:
return 0
if i == count:
return 0
def handler(event, context):
logger.info(json.dumps(event))
taskArn = event['ResourceProperties']['taskArn']
sourceArn = event['ResourceProperties']['sourceArn']
targetArn = event['ResourceProperties']['targetArn']
instanceArn = event['ResourceProperties']['instanceArn']
dmsBucket = event['ResourceProperties']['dmsBucket']
if event['RequestType'] == 'Delete':
try:
dms.stop_replication_task(ReplicationTaskArn=taskArn)
time.sleep(30)
s3.Bucket(dmsBucket).objects.all().delete()
logger.info('Task Stopped, Bucket Emptied')
except Exception as e:
message = 'Issue with Stopping Task or Emptying Bucket:' + str(e)
logger.error(message)
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'Data': 'NA'})
else:
taskArn = event['ResourceProperties']['taskArn']
sourceArn = event['ResourceProperties']['sourceArn']
targetArn = event['ResourceProperties']['targetArn']
instanceArn = event['ResourceProperties']['instanceArn']
try:
if testConnection(sourceArn, 1) != 1:
dms.test_connection(
ReplicationInstanceArn=instanceArn,
EndpointArn=sourceArn
)
if testConnection(sourceArn,10) != 1:
message = 'Source Endpoint Connection Failure'
cfnresponse.send(event, context, cfnresponse.FAILED, {'Data': message})
return {'message': message}
if testConnection(targetArn, 1) != 1:
dms.test_connection(
ReplicationInstanceArn=instanceArn,
EndpointArn=targetArn
)
if testConnection(targetArn,10) != 1:
message = 'Target Endpoint Connection Failure'
logger.error(message)
cfnresponse.send(event, context, cfnresponse.FAILED, {'Data': message})
except Exception as e:
message = 'DMS Endpoint Issue: ' + str(e)
logger.error(message)
cfnresponse.send(event, context, cfnresponse.FAILED, {'Data': message})
try:
if testRepTask(taskArn, 1) != 1:
dms.start_replication_task(
ReplicationTaskArn=taskArn,
StartReplicationTaskType='start-replication')
if testRepTask(taskArn, 10) != 1:
message = 'Error during DMS execution'
logger.error(message)
cfnresponse.send(event, context, cfnresponse.FAILED, {'Data': message})
else:
message = 'DMS Execution Successful'
logger.info(message)
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'Data': message})
except Exception as e:
message = 'DMS Task Issue: ' + str(e)
logger.error(message)
cfnresponse.send(event, context, cfnresponse.FAILED, {'Data': message})
Handler: index.handler
Role:
Fn::GetAtt: [ExecutionRole, Arn]
Runtime: python3.7
DependsOn:
- ExecutionRole
GlueBucket:
Type: AWS::S3::Bucket
GlueJobLoadIncremental:
Type: AWS::Glue::Job
Properties:
Name: DMSCDC_LoadIncremental
Role:
Fn::GetAtt: [ExecutionRole, Arn]
ExecutionProperty:
MaxConcurrentRuns: 50
AllocatedCapacity: 2
GlueVersion: 3.0
Command:
Name: glueetl
ScriptLocation: !Sub
- s3://${bucket}/DMSCDC_LoadIncremental.py
- {bucket: !Ref GlueBucket}
DefaultArguments:
"--job-bookmark-option" : "job-bookmark-disable"
"--TempDir" : !Sub
- s3://${bucket}
- {bucket: !Ref GlueBucket}
"--enable-metrics" : ""
DependsOn:
- ExecutionRole
GlueJobLoadInitial:
Type: AWS::Glue::Job
Properties:
Name: DMSCDC_LoadInitial
Role:
Fn::GetAtt: [ExecutionRole, Arn]
ExecutionProperty:
MaxConcurrentRuns: 50
AllocatedCapacity: 2
GlueVersion: 3.0
Command:
Name: glueetl
ScriptLocation: !Sub
- s3://${bucket}/DMSCDC_LoadInitial.py
- {bucket: !Ref GlueBucket}
DefaultArguments:
"--job-bookmark-option" : "job-bookmark-disable"
"--TempDir" : !Sub
- s3://${bucket}
- {bucket: !Ref GlueBucket}
"--enable-metrics" : ""
DependsOn:
- ExecutionRole
GlueJobController:
Type: AWS::Glue::Job
Properties:
Name: DMSCDC_Controller
Role:
Fn::GetAtt: [ExecutionRole, Arn]
ExecutionProperty:
MaxConcurrentRuns: 50
Command:
Name: pythonshell
PythonVersion: 3
ScriptLocation: !Sub
- s3://${bucket}/DMSCDC_Controller.py
- {bucket: !Ref GlueBucket}
DefaultArguments:
"--job-bookmark-option" : "job-bookmark-disable"
"--TempDir" : !Sub
- s3://${bucket}
- {bucket: !Ref GlueBucket}
"--enable-metrics" : ""
DependsOn:
- ExecutionRole
GlueJobProcessTable:
Type: AWS::Glue::Job
Properties:
Name: DMSCDC_ProcessTable
Role:
Fn::GetAtt: [ExecutionRole, Arn]
ExecutionProperty:
MaxConcurrentRuns: 50
Command:
Name: pythonshell
PythonVersion: 3
ScriptLocation: !Sub
- s3://${bucket}/DMSCDC_ProcessTable.py
- {bucket: !Ref GlueBucket}
DefaultArguments:
"--job-bookmark-option" : "job-bookmark-disable"
"--TempDir" : !Sub
- s3://${bucket}
- {bucket: !Ref GlueBucket}
"--enable-metrics" : ""
DependsOn:
- ExecutionRole