Database Migration steps with the AWS CLI - 2
Tags: migration, cli, dms
Published: 2023-01-31 23:55:01

This is 2nd article on Database Migration Steps using AWS CLI, showing on how to set up a **`replication task`**. The replication task runs on the replication instance and migrates data from the source endpoint to the target endpoint.

Please read my 1st article, **[Database Migration steps with the AWS CLI - 1](https://dev.to/aws-builders/database-migration-steps-with-the-aws-cli-1-2k59)**, where I have shown on how to create and set up a replication instance, source and target endpoints.

You can use the AWS DMS console or the AWS CLI or the AWS SDK to perform the database migration. 

Please visit my **[GitHub Repository for Database Migration  articles](https://github.com/awsmine/Database_Migration_Projects)** on various topics being updated on constant basis.

_**Let’s get started!**_

# Objectives:

**6.** Create a replication task.

**7.** Describe the replication task.

**8.** Start the replication task.

**9.** Check the progress of the replication task.

**10.** Stop the replication task.

**11.** Delete the replication task.

**12.** Delete the source and target endpoints.

**13.** Delete the replication instance.


# Pre-requisites:
- AWS user account with admin access, not a root account.
- Cloud9 IDE with AWS CLI.



# Resources Used:

**[AWS Database Migration Service](https://docs.aws.amazon.com/dms/latest/userguide/Welcome.html)**

**[AWS CLI Command Reference on DMS](https://docs.aws.amazon.com/cli/latest/reference/dms/)**


# Steps for implementation to this project:

## 6. Create a replication task.

- If the test connections are successful, use the following command to create the task:

```
aws dms create-replication-task --replication-task-identifier replication-task-1 --source-endpoint-arn $source_endpoint_arn --target-endpoint-arn $target_endpoint_arn --replication-instance-arn $rep_instance_arn --migration-type full-load --table-mappings file:///tmp/table-mappings --replication-task-settings file:///tmp/task-settings

```

- Create table mappings and task settings files in the temp directory (in Linux). 

- For Loading Parameters, read from **[Loading Parameters from a File](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-parameters.html#cli-using-param-file)** 

- For task settings, read from **[Task Settings for AWS Database Migration Service Tasks](https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Tasks.CustomizingTasks.TaskSettings.html)**

- For migration methods, read from **[Migration Methods for AWS Database Migration Service](https://docs.aws.amazon.com/dms/latest/userguide/Welcome.html)**

- Read from for **[Table Mapping to specify task settings](https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Tasks.CustomizingTasks.TableMapping.html)**

- Wait for 4-5 minutes to complete the tsk creation.

## 7. Describe the replication task.

- After the task is created, describe the task to check that it is ready to be executed.

```
aws dms describe-replication-tasks --filters "Name=replication-task-id,Values=replication-task-1"

```

- Run the following command to save the replication task ARN for use in later steps:

**replication task ARN**

```
replication_task_arn=$(aws dms describe-replication-tasks --filters "Name= replication-task-id,Values=replication-task-1" --query "ReplicationTasks[0].ReplicationTaskArn" --output text)

```


- Run the following command to check the status of the task:

```
aws dms describe-replication-tasks --filters "Name=replication-task-arn,Values=$replication_task_arn" --query "ReplicationTasks[0].Status" 

```

## 8. Start the replication task.

- Run the following command to start the task:

```
aws dms start-replication-task --replication-task-arn $replication_task_arn --start-replication-task-type start-replication

```

- Read for **[start-replication-task command] (https://docs.aws.amazon.com/cli/latest/reference/dms/start-replication-task.html)**

## 9. Check the progress of the replication task.

- Run the following commands to keep track of the task progress.

- To monitor the overall task-level statistics, run the following command:

```
aws dms describe-replication-tasks --filters "Name=replication-task-arn,Values=$replication_task_arn" --query "ReplicationTasks[0].ReplicationTaskStats"

```

- To monitor the table-level statistics, run the following command:

```
aws dms describe-table-statistics --replication-task-arn $replication_task_arn

```

- To monitor the task status itself, run the following command:

```
aws dms describe-replication-tasks --filters "Name=replication-task-arn,Values=$replication_task_arn" --query "ReplicationTasks[0].{Status:Status,StopReason:StopReason}" 

```

## 10. Stop the replication task.

- You can stop the migration after data is completely migrated from source to target. 

- Run the following command to stop the migration task:

```
aws dms stop-replication-task --replication-task-arn $replication_task_arn

```

# Cleanup

## 11. Delete the replication task.

- Run the following command to delete it:

```
aws dms delete-replication-task --replication-task-arn $replication_task_arn

```

## 12. Delete the source and target endpoints.

- Run the following commands to delete them:

```
aws dms delete-endpoint --endpoint-arn $source_endpoint_arn

aws dms delete-endpoint --endpoint-arn $target_endpoint_arn

```
## 13. Delete the replication instance.

- Run the following command to delete the replication instance:

```
aws dms delete-replication-instance --replication-instance-arn $rep_instance_arn 

```
# What we have done so far

We have successfully demonstrated on how to set up a **`replication task`**.