Database Migration steps with the AWS CLI - 1
Tags: cli, dms, migration
Published: 2023-01-31 02:09:48

According to AWS documentation "AWS Database Migration Service (AWS DMS) is a cloud service that makes it possible to migrate relational databases, data warehouses, NoSQL databases, and other types of data stores. You can use AWS DMS to migrate your data into the AWS Cloud or between combinations of cloud and on-premises setups."

The process of **Database Migration** consists of creating and setting up a **`replication instance, source and target endpoints, and a replication task`**. The replication task runs on the replication instance and migrates data from the source endpoint to the target endpoint.

In the 1st article, **Database Migration steps with the AWS CLI - 1**, I will be showing on how to create and set up a **`replication instance, source and target endpoints`**.

The rest of the steps will be covered in my **[2nd article](https://dev.to/aws-builders/database-migration-steps-with-the-aws-cli-2-3001)**.

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:

**1.** Create a replication instance.

**2.** Describe the replication instance.

**3.** Create the source and target endpoints.

**4.** Test source and target endpoints from the replication instance.

**5.** Describe connections to the source and target endpoints.


# 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:

## 1. Create a replication instance.

-  A replication instance is an Amazon EC2 instance that can host
replication tasks within AWS DMS.

- Use the following command to create a replication instance with the name **`dms-instance`**.

**dms-instance**
```
aws dms create-replication-instance --replication-instance-identifier dms-instance --replication-instance-class dms.t2.micro --allocated-storage 50

```

- This command creates the replication instance on a **`t2.micro`** instance with **`50 GB of allotted storage`**. 

- Use default values for other parameters. 


## 2. Describe the replication instance.

- Run the following command to describe the replication instance. 

- The response of this command will include the status of create-replication-instance. 

```
aws dms describe-replication-instances --filter=Name=replication-instance-id,Values=dms-instance

```

- Run the following command to save the **`ReplicationInstanceArn`** for use in later steps.

**ReplicationInstanceArn**

```
rep_instance_arn=$(aws dms describe-replication-instances --filter=Name=replication-instance-id,Values=dms-instance --query 'ReplicationInstances[0].ReplicationInstanceArn' --output text)

```

- Wait 4-5 minutes to create the replication instance. 


## 3. Create the source and target endpoints.

- An endpoint describes the connection address, credentials, and other information required to connect to a database.

- Run the following command to create the **`source and target endpoints`**. 

- Provide source and target database details like the **`engine-name`**, the **`hostname`** and **`port`** and the **`username`** and **`password`**.

**source-endpoint**

```
aws dms create-endpoint --endpoint-identifier source-endpoint --endpoint-type source --engine-name --username --password --server-name --port

```

**target-endpoint**

```
aws dms create-endpoint --endpoint-identifier target-endpoint --endpoint-type target --engine-name --username --password --server-name --port

```

- Run the following commands to save the endpoint ARNs for use in later steps.

**source_endpoint_arn**

```
source_endpoint_arn=$(aws dms describe-endpoints --filter="Name=endpoint-id,Values=source-endpoint " --query="Endpoints[0].EndpointArn" --output text)

```

**target_endpoint_arn**

```
target_endpoint_arn=$(aws dms describe-endpoints --filter="Name=endpoint-id,Values=target-endpoint" --query="Endpoints[0].EndpointArn" --output text) 

```

## 4. Test source and target endpoints from the replication instance.

- After the replication instance is active and the endpoints have been successfully created and before you save your endpoint, you should test the connection to ensure it was configured correctly.

- Run the following commands to test connectivity tests from the replication instance to the database endpoints:

```
aws dms test-connection --replication-instance-arn $rep_instance_arn --endpoint-arn $source_endpoint_arn

```

```
aws dms test-connection --replication-instance-arn $rep_instance_arn --endpoint-arn $target_endpoint_arn 

```

## 5. Describe connections to the source and target endpoints. 

- Describe the connections to ensure that the tests are successful. 

- If the test connection fails, it must be fixed and retested.

```
aws dms describe-connections --filter "Name=endpoint-arn,Values=$source_endpoint_arn,$target_endpoint_arn"

```

# What we have done so far

We have successfully demonstrated on how to create and set up a **`replication instance, source and target endpoints`**.