Automating AWS DMS Migration Tasks
This blog post gives you a quick overview of how you can schedule migration tasks for the purpose of automating your migration. You create these tasks by using the AWS Database Migration Service (AWS DMS) and using native operating system tools for either Linux or Microsoft Windows.
AWS DMS helps you migrate databases to AWS quickly and securely. You can migrate your data to and from most widely used commercial and open-source databases, such as Oracle, Microsoft SQL Server, PostgreSQL. The service supports homogeneous migrations such as Oracle to Oracle, and also heterogeneous migrations between different database platforms, such as Oracle to PostgreSQL or MySQL to Oracle.
To work with AWS DMS, either your source or target database must be in AWS. Currently, DMS doesn’t support migrating from an on-premises database to another on-premises database.
The AWS DMS migration process consists of 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.
To automate migration tasks, you use the AWS Command Line Interface (AWS CLI) to perform the database migration. If you are a first-time AWS CLI user, we recommend that you read the following documentation and get accustomed to how the CLI should be used and configured:
- Create an AWS Identity and Access Management (IAM) user.
- Set up required permissions for the IAM user.
- Set up the roles required to use AWS DMS.
- Set up the AWS CLI.
On many teams, version control is a usual part of business, but databases are left out in the cold. Perhaps the team exports a massive database dump or SQL file from time to time as the definition of the database. But deploying database changes across Dev, QA, and Production environments is still an issue, and the process isn’t reliable when synced up manually.
In other cases, database data might need to be updated for dashboards or weekly or daily reports that fetch the data from the database. But this process is often something that is static and performed as a one-time task. Also, the team often has to do the process following a particular schedule, for example before office hours or before a particular time when a report is published.
In many organizations, database migration is something that needs to be done like a backup job. Migration ideally should done only in off-business hours, to avoid the load that DMS can cause in the production environment.
Currently, DMS tasks cannot be scheduled using the DMS console. To schedule DMS tasks, we need to use the native tools present in the OS (Windows or Linux) that you use to access AWS resources. This blog post shows you how to do so.
In this blog post, the tools that automate the task execution have been run on an Amazon EC2 environment with Windows and Amazon Linux distribution. The native tools used for showing task execution in Linux are available in other distributions (RedHat, Ubuntu, Oracle Linux, and so on). Native tools for showing task execution also available for almost all the versions of Windows (2003 Server onwards and from XP till 10).
Moving forward, the process that this post describes should greatly simplify automated task deployments and modification scenarios. Following is a detailed description on how to automatically execute the DMS task or schedule it for execution in Linux and Windows.
- Install the AWS CLI on your Linux machine from Install the AWS Command Line Interface on Linux in the AWS documentation.
You don’t need to explicitly configure your AWS CLI using the IAM roles if you plan just to use the CLI to automate execution of the DMS tasks. Explicit configuration isn’t necessary because you’ll be including the ACCESS_KEY and SECRET_KEY values in the bash script file that includes the CLI command. However, in general, it’s a good idea to configure the CLI to run commands by using aws configure.
- Create a bash script file, using the following command.
- In the preceding script, include the following contents.
In the file, include the SECRET_KEY and ACCESS_KEY values for the user that you configured the CLI with in the earlier step. Specify the REGION value based on the AWS DMS resources you have deployed. The JSON output format is optional.
- Save the file.
- Grant permissions on the file. Following, I grant all the permissions to all the users on the file; this can be modified to fit the user’s environment.
- To schedule this bash script as a part of CRON job, run the following command.
- After the preceding command runs, a file opens. For the purpose of this article, I include the following command.
So in CRON, each asterisk has the following meaning:
In the preceding command, the bash script is scheduled to execute every day at 6:06 a.m. The following example runs a shell program called export_dump.sh at 23:45 (11:45 PM) every Saturday.
For more information on crontab, run the following command.
- Save the file.
- Locate the logs related to the CRON job. Generally, they’re located in this directory:
If the location doesn’t exist in the Linux distribution that you are using, then run the following command to find the logs:
Based on the output given by the preceding command, you can look for the logs in the indicated locations.
- To check if the script was executed or not, run the following command.
Replace filename with the name of the bash script that you created. In this case it was task. After running the preceding command, you can see the following output.
If there are errors with the execution, you will see that in the logs. To get the last 30 lines of the log file, you can run the following command:
You can check this part of the log file for any messages related to your task.
- Install the AWS CLI on your Windows machine from Install the AWS Command Line Interface on Microsoft Windows in the AWS documentation.
- Configure the installed CLI by following the instructions from Configuring the AWS CLI in the AWS documentation.
- Once the installation and configuration is done, from the Start menu, open the Windows PowerShell ISE program.
- The program interface should look like the following.
- In the editor section, paste the following command.
- Save the file. For this post, the file is named ps1.
- From the Start menu, open Task Scheduler.
- From the right pane of the program, choose Create Task.
- On the Create Task window, fill in the following options:
- Name: Provide a task name. For this post, we use dmstask.
- Description (Optional): Comments about the task.
- Security Options: Make sure that the user account used has enough privileges to run the task. Check the option Run whether user is Logged on or not. Selecting Run with Highest Privileges is optional. If you select this check box, Task Scheduler runs the task using an elevated privileges token rather than a least privileges (UAC) token. Only tasks that require elevated privileges to complete their actions should run with elevated privileges.
- Navigate to the Triggers tab and create a schedule for task execution, by choosing New.
The preceding trigger makes sure that a PowerShell script executes every day from today onward at 3:20 p.m. EST. Choose OK when you’re done.
- Navigate to the Actions tab and choose New. Here, you can design how the script runs. Fill in the fields in the following manner.
Action: Start a program
Add arguments (Optional): -ExecutionPolicy Bypass C:\task.ps1
Choose OK once you’re done.
- On the Condition tab, make sure that you have the following options selected.
- When you choose OK, you are prompted to enter the password for the user account logged in to your Windows machine, or with the user account that you have set to run the task.
- Choose OK when you’re ready. At this point, the migration task is created.
- Choose the History tab for the task to see the job execution status and logs for the task.
We recommend regular monitoring of the CRON logs or Task Scheduler history to ensure that your migration tasks are executing as you want them to. Also, creating DMS event subscriptions related to task state changes should help you to get an accurate idea if the tasks execute as you expect and on schedule. Hopefully this blog post gives you a head start before embarking on your migration.
Good luck and happy migrating!
About the Author
Abhinav Singh is a database engineer in Database Migration Service at Amazon Web Services. He works with our customers to provide guidance and technical assistance on database migration projects, helping them improving the value of their solutions when using AWS.