Create a DTS synchronization instance using Terraform

Updated at:

Terraform is an open-source tool that allows you to safely and efficiently preview, provision, and manage cloud infrastructure and resources. You can use Terraform to manage some DTS resources. This topic describes how to use Terraform to create a DTS synchronization instance.

Note

You can run the sample code in this tutorial with a single click. Run in one click

Prerequisites

  • To reduce security risks, we recommend that you use a RAM user with the minimum required permissions to complete this tutorial. For more information, see Create a RAM user and Manage RAM user permissions. This tutorial requires the following permission policy:

    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "dts:CreateDtsInstance",
            "dts:DescribeGadInstances",
            "dts:DescribeDtsJobDetail",
            "dts:DescribeInstances",
            "dts:DeleteSynchronizationJob"
          ],
          "Resource": "*"
        }
      ]
    }
  • Prepare a Terraform runtime environment. You can use Terraform in one of the following ways:

    • Explorer: Alibaba Cloud provides an online runtime environment for Terraform. After logging on, you can use and experience Terraform online without needing to install it. This method is ideal for quickly and easily trying or debugging Terraform at no cost.

    • Quickly create resources by using Terraform: Alibaba Cloud Cloud Shell comes pre-installed with Terraform components and configured with identity credentials. You can run Terraform commands directly in Cloud Shell. This method is ideal for fast, convenient, and low-cost access to Terraform.

    • Install and configure Terraform on your on-premises machine: This option is ideal for scenarios with poor network connectivity or when you need a customized development environment.

Resources used

Procedure

  1. Create a working directory, and in that directory, create a configuration file named main.tf and copy the following code into main.tf.

    variable "region" {
      default = "cn-beijing"
    }
    
    provider "alicloud" {
      region = var.region
    }
    
    # DTS synchronization instance
    resource "alicloud_dts_synchronization_instance" "default" {
      payment_type                     = "PayAsYouGo"
      source_endpoint_engine_name      = "MySQL"
      source_endpoint_region           = var.region
      destination_endpoint_engine_name = "MySQL"
      destination_endpoint_region      = var.region
      instance_class                   = "small"
      sync_architecture                = "oneway"
    }
  2. Run the following command to initialize the Terraform runtime environment.

    terraform init

    The following output indicates a successful initialization.

    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. Run the following command to apply the configuration.

    terraform apply

    When prompted, enter yes and press the Enter key. Wait for the command to complete. The following output indicates that the configuration was applied successfully.

    You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  4. Verify the result.

    Terraform show

    In your working directory, run the following command to view the details of the resources created by Terraform:

    terraform show
    shell@Alicloud:~/dts$ terraform show
    # alicloud_dts_synchronization_instance.default:
    resource "alicloud_dts_synchronization_instance" "default" {
        destination_endpoint_engine_name = "MySQL"
        destination_endpoint_region      = "cn-beijing"
        id                               = "dtsxxx"
        instance_class                   = "small"
        payment_type                     = "PayAsYouGo"
        source_endpoint_engine_name      = "MySQL"
        source_endpoint_region           = "cn-beijing"
        sync_architecture                = "oneway"
    }

    Console

    Log on to the Data Transmission Service (DTS) console, navigate to the Data Synchronization page, and view the new DTS synchronization instance.

    In the task list, you can view information such as the task status (for example, Not Configured), link specification, and payment type. In the Actions column, click Configure Task to proceed with the next configuration step.

Clean up resources

When you no longer need the resources managed by Terraform, run the following command to release them. For more information about terraform destroy, see Common commands.

terraform destroy

Complete example

Note

You can run the sample code in this tutorial with a single click. Run in one click

Sample code

variable "region" {
  default = "cn-beijing"
}

provider "alicloud" {
  region = var.region
}

# DTS synchronization instance
resource "alicloud_dts_synchronization_instance" "default" {
  payment_type                     = "PayAsYouGo"
  source_endpoint_engine_name      = "MySQL"
  source_endpoint_region           = var.region
  destination_endpoint_engine_name = "MySQL"
  destination_endpoint_region      = var.region
  instance_class                   = "small"
  sync_architecture                = "oneway"
}

For more complete examples, see More complete examples.

References