amazon-web-servicesaws-cdkaws-dms

Creating an AWS DMS task using AWS CDK


I am trying to create an AWS DMS task using AWS CDK. But I don't know where to start. I Could not find a good documentation on how I can create the DMS task using CDK. I found the articles on both of the topics but couldn't find an article which adrresses this - talks about how I can create the DMS task using CDK.

Can anyone point me to the right article which explains this or help me do this?

P.S. - I have initialized the project with dms maven dependency. I am using JAVA.

Thanks


Solution

  • There are no CDK constructs to simplify working with DMS. Therefore you'll have to use the CloudFormation resources: CfnEndpoint, CfnReplicationTask, etc.

    I'm providing the following example to get you started, but be warned, the DMS CloudFormation resources are quite challenging.

    import * as cdk from '@aws-cdk/core';
    import * as dms from '@aws-cdk/aws-dms';
    
    export class DmsStack extends cdk.Stack {
      constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);
    
        // Create a subnet group that allows DMS to access your data
        const subnet = new dms.CfnReplicationSubnetGroup(this, 'SubnetGroup', {
          replicationSubnetGroupIdentifier: 'cdk-subnetgroup',
          replicationSubnetGroupDescription: 'Subnets that have access to my data source and target.',
          subnetIds: [ 'subnet-123', 'subnet-456' ],
        });
    
        // Launch an instance in the subnet group
        const instance = new dms.CfnReplicationInstance(this, 'Instance', {
          replicationInstanceIdentifier: 'cdk-instance',
    
          // Use the appropriate instance class: https://docs.aws.amazon.com/dms/latest/userguide/CHAP_ReplicationInstance.Types.html
          replicationInstanceClass: 'dms.t2.small',
    
          // Setup networking
          replicationSubnetGroupIdentifier: subnet.replicationSubnetGroupIdentifier,
          vpcSecurityGroupIds: [ 'sg-123' ],
        });
    
        // Create endpoints for your data, see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dms-endpoint.html
        const source = new dms.CfnEndpoint(this, 'Source', {
          endpointIdentifier: 'cdk-source',
          endpointType: 'source',
          engineName: 'mysql',
    
          serverName: 'source.database.com',
          port: 3306,
          databaseName: 'database',
          username: 'dms-user',
          password: 'password-from-secret',
        });
    
        const target = new dms.CfnEndpoint(this, 'Target', {
          endpointIdentifier: 'cdk-target',
          endpointType: 'target',
          engineName: 's3',
    
          s3Settings: {
            bucketName: 'target-bucket'
          },
        });
    
        // Define the replication task
        const task = new dms.CfnReplicationTask(this, 'Task', {
          replicationInstanceArn: instance.ref,
    
          migrationType: 'full-load', // https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dms-replicationtask.html#cfn-dms-replicationtask-migrationtype
          sourceEndpointArn: source.ref,
          targetEndpointArn: target.ref,
          tableMappings: JSON.stringify({
            "rules": [{
              "rule-type": "selection",
              "rule-id": "1",
              "rule-name": "1",
              "object-locator": {
                "schema-name": "%",
                "table-name": "%"
              },
              "rule-action": "include"
            }]
          })
        })
      }
    }