AWS Database Migration Service (DMS)¶
AWS Database Migration Service (AWS DMS) is a web service you can use to migrate data from your database that is on-premises, on an Amazon Relational Database Service (Amazon RDS) DB instance, or in a database on an Amazon Elastic Compute Cloud (Amazon EC2) instance to a database on an AWS service. These services can include a database on Amazon RDS or a database on an Amazon EC2 instance. You can also migrate a database from an AWS service to an on-premises database. You can migrate between source and target endpoints that use the same database engine, such as from an Oracle database to an Oracle database. You can also migrate between source and target endpoints that use different database engines, such as from an Oracle database to a PostgreSQL database.
Prerequisite Tasks¶
To use these operators, you must do a few things:
Create necessary resources using AWS Console or AWS CLI.
Install API libraries via pip.
pip install 'apache-airflow[amazon]'Detailed information is available Installation of Airflow®
Generic Parameters¶
- aws_conn_id
Reference to Amazon Web Services Connection ID. If this parameter is set to
Nonethen the default boto3 behaviour is used without a connection lookup. Otherwise use the credentials stored in the Connection. Default:aws_default- region_name
AWS Region Name. If this parameter is set to
Noneor omitted then region_name from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:None- verify
Whether or not to verify SSL certificates.
False- Do not validate SSL certificates.path/to/cert/bundle.pem - A filename of the CA cert bundle to use. You can specify this argument if you want to use a different CA cert bundle than the one used by botocore.
If this parameter is set to
Noneor is omitted then verify from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:None- botocore_config
The provided dictionary is used to construct a botocore.config.Config. This configuration can be used to configure Avoid Throttling exceptions, timeouts, etc.
Example, for more detail about parameters please have a look botocore.config.Config¶{ "signature_version": "unsigned", "s3": { "us_east_1_regional_endpoint": True, }, "retries": { "mode": "standard", "max_attempts": 10, }, "connect_timeout": 300, "read_timeout": 300, "tcp_keepalive": True, }
If this parameter is set to
Noneor omitted then config_kwargs from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:NoneNote
Specifying an empty dictionary,
{}, will overwrite the connection configuration for botocore.config.Config
Operators¶
Create a replication task¶
To create a replication task you can use
DmsCreateTaskOperator.
create_task = DmsCreateTaskOperator(
task_id="create_task",
replication_task_id=dms_replication_task_id,
source_endpoint_arn=create_assets["source_endpoint_arn"],
target_endpoint_arn=create_assets["target_endpoint_arn"],
replication_instance_arn=create_assets["replication_instance_arn"],
table_mappings=table_mappings,
migration_type="full-load-and-cdc",
create_task_kwargs={
"ReplicationTaskSettings": json.dumps({"ValidationSettings": {"EnableValidation": True}})
},
)
Modify a replication task¶
To modify an existing replication task (e.g. to update table mappings for a backfill) you can use
DmsModifyTaskOperator.
The task must be stopped before modification — use DmsStopTaskOperator
upstream in the Dag, and DmsStartTaskOperator
downstream to restart it afterwards if needed.
modify_task = DmsModifyTaskOperator(
task_id="modify_task",
replication_task_arn=task_arn,
table_mappings={
"rules": [
{
"rule-type": "selection",
"rule-id": "1",
"rule-name": "1",
"object-locator": {"schema-name": "%", "table-name": "%"},
"rule-action": "include",
}
]
},
)
Start a replication task¶
To start a replication task you can use
DmsStartTaskOperator.
start_task = DmsStartTaskOperator(
task_id="start_task",
replication_task_arn=task_arn,
)
Reload tables for a replication task¶
To reload selected target tables from their source data, use
DmsReloadTablesOperator.
The required parameters are replication_task_arn and tables_to_reload. Each item in
tables_to_reload must contain SchemaName and TableName. The optional reload_option
defaults to data-reload, which reloads the data and runs validation again when it is enabled.
Use validate-only to revalidate without reloading data; this option only applies when validation
is enabled for the task.
The replication task must be in the RUNNING state. By default, the operator waits until every
requested operation completes. With data-reload, it waits for TableState to reach
Table completed. With validate-only, it waits for ValidationState to reach Validated
and fails if DMS reports mismatched or suspended records, a table error, or another terminal
validation failure. The first waiter poll runs immediately after the request returns. Set
deferrable=True to release the worker slot while waiting, or set wait_for_completion=False
to return the replication task ARN immediately. Use waiter_delay and waiter_max_attempts to
control subsequent polls for each table.
AWS DMS accepts up to 10 unique tables per request and supports tasks using the full-load or
full-load-and-cdc migration type. DMS applies the task’s TargetTablePrepMode setting before
reloading each table; when it is DO_NOTHING, truncate the target table manually before reloading.
See Reloading tables during a task for details.
reload_tables = DmsReloadTablesOperator(
task_id="reload_tables",
replication_task_arn=task_arn,
tables_to_reload=[{"SchemaName": "public", "TableName": rds_table_name}],
reload_option="data-reload",
wait_for_completion=True,
deferrable=True,
)
revalidate_tables = DmsReloadTablesOperator(
task_id="revalidate_tables",
replication_task_arn=task_arn,
tables_to_reload=[{"SchemaName": "public", "TableName": rds_table_name}],
reload_option="validate-only",
wait_for_completion=True,
deferrable=True,
)
Get details of replication tasks¶
To retrieve the details for a list of replication tasks you can use
DmsDescribeTasksOperator.
describe_tasks = DmsDescribeTasksOperator(
task_id="describe_tasks",
describe_tasks_kwargs={
"Filters": [
{
"Name": "replication-instance-arn",
"Values": [create_assets["replication_instance_arn"]],
}
]
},
do_xcom_push=False,
)
Stop a replication task¶
To stop a replication task you can use
DmsStopTaskOperator.
stop_task = DmsStopTaskOperator(
task_id="stop_task",
replication_task_arn=task_arn,
)
Delete a replication task¶
To delete a replication task you can use
DmsDeleteTaskOperator.
delete_task = DmsDeleteTaskOperator(
task_id="delete_task",
replication_task_arn=task_arn,
)
Create a serverless replication config¶
To create a serverless replication config use
DmsCreateReplicationConfigOperator.
create_replication_config = DmsCreateReplicationConfigOperator(
task_id="create_replication_config",
replication_config_id=replication_id,
source_endpoint_arn=create_assets["source_endpoint_arn"],
target_endpoint_arn=create_assets["target_endpoint_arn"],
compute_config={
"MaxCapacityUnits": 4,
"MinCapacityUnits": 1,
"MultiAZ": False,
"ReplicationSubnetGroupId": "default",
},
replication_type="full-load",
table_mappings=json.dumps(table_mappings),
)
Describe a serverless replication config¶
To describe a serverless replication config use
DmsDescribeReplicationConfigsOperator.
describe_replication_configs = DmsDescribeReplicationConfigsOperator(
task_id="describe_replication_configs",
)
Start a serverless replication¶
To start a serverless replication use
DmsStartReplicationOperator.
replicate = DmsStartReplicationOperator(
task_id="replicate",
replication_config_arn="{{ task_instance.xcom_pull(task_ids='create_replication_config', key='return_value') }}",
replication_start_type="start-replication",
wait_for_completion=True,
waiter_delay=60,
waiter_max_attempts=200,
)
Stop a serverless replication¶
To stop a serverless replication use
DmsStopReplicationOperator.
stop_replication = DmsStopReplicationOperator(
task_id="stop_replication",
replication_config_arn="{{ task_instance.xcom_pull(task_ids='create_replication_config', key='return_value') }}",
wait_for_completion=True,
waiter_delay=120,
waiter_max_attempts=200,
)
Get the status of a serverless replication¶
To get the status of a serverless replication use
DmsDescribeReplicationsOperator.
describe_replications = DmsDescribeReplicationsOperator(
task_id="describe_replications",
)
Delete a serverless replication configuration¶
To delete a serverless replication config use
DmsDeleteReplicationConfigOperator.
delete_replication_config = DmsDeleteReplicationConfigOperator(
task_id="delete_replication_config",
waiter_max_attempts=200,
replication_config_arn="{{ task_instance.xcom_pull(task_ids='create_replication_config', key='return_value') }}",
)
Sensors¶
Wait for a replication task to complete¶
To check the state of a replication task until it is completed, you can use
DmsTaskCompletedSensor.
await_task_stop = DmsTaskCompletedSensor(
task_id="await_task_stop",
replication_task_arn=task_arn,
)