Created
December 27, 2023 13:41
-
-
Save SaundersB/5c1b00d433563e6ced6c29a16a6dd458 to your computer and use it in GitHub Desktop.
Sample Database Migration Service AWS CDK Stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as cdk from 'aws-cdk-lib/core'; | |
import * as ecr from 'aws-cdk-lib/aws-ecr'; | |
import { StackProps } from 'aws-cdk-lib'; | |
import { Construct } from 'constructs'; | |
import * as iam from 'aws-cdk-lib/aws-iam'; | |
import * as dms from 'aws-cdk-lib/aws-dms'; | |
import * as ec2 from 'aws-cdk-lib/aws-ec2'; | |
export class Stack extends cdk.Stack { | |
constructor(scope: Construct, id: string, props?: StackProps) { | |
super(scope, id, props); | |
const environment = process.env.ENVIRONMENT || 'development'; | |
const vpc: any = ec2.Vpc.fromLookup(this, `VPC`, { | |
vpcId: 'vpc-YOUR_VPC' | |
}); | |
const dmsSubnetGroup = new dms.CfnReplicationSubnetGroup( | |
this, | |
'MyDmsSubnetGroup', | |
{ | |
replicationSubnetGroupDescription: | |
'Subnet group for DMS replication instance', | |
replicationSubnetGroupIdentifier: `dms-subnet-group-${environment}`, | |
subnetIds: [ | |
'subnet-SUBNET_1', | |
'subnet-SUBNET_2', | |
], | |
} | |
); | |
const dmsSecurityGroup = new ec2.SecurityGroup( | |
this, | |
'DMSSecurityGroup', | |
{ | |
vpc: vpc, | |
description: 'Allow DMS to access source and target databases', | |
allowAllOutbound: true, | |
} | |
); | |
dmsSecurityGroup.addIngressRule( | |
ec2.Peer.ipv4('YOUR_CIDR/8,16'), | |
ec2.Port.tcp(3306), | |
'Allow MySQL access for DMS' | |
); | |
const dbCredentials = relationalDb.cluster.secret; | |
const replicationInstance = new dms.CfnReplicationInstance( | |
this, | |
'ReplicationInstance', | |
{ | |
replicationInstanceIdentifier: `replication-instance-${environment}`, | |
replicationInstanceClass: 'dms.t2.small', | |
allocatedStorage: 100, | |
vpcSecurityGroupIds: [dmsSecurityGroup.securityGroupId], | |
replicationSubnetGroupIdentifier: `dms-subnet-group-${environment}`, | |
publiclyAccessible: false, | |
tags: [ | |
{ | |
key: 'Name', | |
value: 'replication-instance', | |
}, | |
], | |
} | |
); | |
const sourceEndpoint = new dms.CfnEndpoint(this, 'SourceEndpoint', { | |
endpointIdentifier: 'mysql57-source-endpoint', | |
endpointType: 'source', | |
engineName: 'mysql', | |
username: dbCredentials.secretValueFromJson('username').toString(), | |
password: dbCredentials.secretValueFromJson('password').toString(), | |
serverName: relationalDb.cluster.dbInstanceEndpointAddress, | |
port: cdk.Token.asNumber(relationalDb.cluster.dbInstanceEndpointPort), | |
databaseName: 'db', | |
}); | |
const tableMappings = JSON.stringify({ | |
rules: [ | |
{ | |
'rule-type': 'selection', | |
'rule-id': '1', | |
'rule-name': '1', | |
'object-locator': { | |
'schema-name': '%', | |
'table-name': '%', | |
}, | |
'rule-action': 'include', | |
filters: [], | |
}, | |
], | |
}); | |
const mysql8DbCredentials = mysql8db.cluster.secret; | |
const targetEndpoint = new dms.CfnEndpoint(this, 'TargetEndpoint', { | |
endpointIdentifier: 'mysql80-target-endpoint', | |
endpointType: 'target', | |
engineName: 'mysql', | |
username: mysql8DbCredentials | |
.secretValueFromJson('username') | |
.toString(), | |
password: mysql8DbCredentials | |
.secretValueFromJson('password') | |
.toString(), | |
serverName: mysql8db.cluster.dbInstanceEndpointAddress, | |
port: cdk.Token.asNumber(mysql8db.cluster.dbInstanceEndpointPort), | |
databaseName: 'root_db', | |
}); | |
// Set up the DMS replication task | |
new dms.CfnReplicationTask( | |
this, | |
'ReplicationTask', | |
{ | |
migrationType: 'full-load-and-cdc', | |
replicationInstanceArn: replicationInstance.ref, | |
sourceEndpointArn: sourceEndpoint.ref, | |
targetEndpointArn: targetEndpoint.ref, | |
tableMappings: tableMappings, | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment