Create a SelectDB instance using Terraform

Updated at:

This topic describes how a Resource Access Management (RAM) user can create an ApsaraDB for SelectDB instance using Terraform.

Note

The sample template code supports one-click execution.

Prerequisites

  • An Alibaba Cloud account has full permissions on all resources. Leaked identity credentials for this account pose a significant security threat. To mitigate this risk, use a RAM user and create an AccessKey for the RAM user.

  • Grant the AliyunSelectDBFullAccess (for managing ApsaraDB for SelectDB) and AliyunVPCFullAccess (for managing virtual private cloud (VPC)) permissions to the RAM user. For more information, see Manage RAM user permissions.

    Sample authorization policy

    {
        "Version": "1",
        "Statement": [
            {
                "Action": "selectdb:*",
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": "ram:CreateServiceLinkedRole",
                "Resource": "*",
                "Effect": "Allow",
                "Condition": {
                    "StringEquals": {
                        "ram:ServiceName": "selectdb.aliyuncs.com"
                    }
                }
            },
            {
                "Action": [
                    "vpc:CreateVpc",
                    "vpc:DescribeVpc",
                    "vpc:ModifyVpcAttribute",
                    "vpc:DeleteVpc",
                    "vpc:CreateVSwitch",
                    "vpc:DescribeVSwitch",
                    "vpc:ModifyVSwitchAttribute",
                    "vpc:DeleteVSwitch"
                ],
                "Resource": "*",
                "Effect": "Allow"
            }
        ]
    }
  • Prepare a Terraform runtime environment. You can use Terraform in one of the following ways.

    • Terraform Explorer: Alibaba Cloud provides an online runtime environment for Terraform. You do not need to install Terraform. You can log on to use and test Terraform online. This method is suitable for quickly and easily testing and debugging Terraform at no cost.

    • Cloud Shell: Cloud Shell comes with pre-installed Terraform components and pre-configured identity credentials. You can run Terraform commands directly in Cloud Shell. This method is suitable for quickly and easily accessing and using Terraform at a low cost.

    • Install and configure Terraform on your local machine: This method is suitable for scenarios with poor network connectivity or when you need a custom developer environment.

      Important

      Make sure that your Terraform version is 0.12.28 or later. To check the current version, run the terraform --version command.

Resources used

The sample code in this topic uses the following Terraform resources.

Important

Some resources in this tutorial incur fees. To avoid unnecessary charges, release these resources when they are no longer needed.

Create an instance

  1. Create a configuration file.

    Create a working directory. In the working directory, create a configuration file named main.tf. main.tf is the main Terraform file that defines the resources to be deployed.

    variable "region" {
      default = "cn-hangzhou"
    }
    variable "zone" {
      default = "cn-hangzhou-k"
    }
    provider "alicloud" {
      region = var.region
    }
    variable "name" {
      default = "tf-example-SelectDB"
    }
    
    # Create an alicloud_vpc resource
    resource "alicloud_vpc" "vpc_selectdb" {
      vpc_name   = var.name
      cidr_block = "172.16.0.0/12"
    }
    # Create an alicloud_vswitch resource
    resource "alicloud_vswitch" "default" {
      vswitch_name = var.name
      cidr_block   = "172.16.20.0/24"
      vpc_id       = alicloud_vpc.vpc_selectdb.id
      zone_id      = var.zone
    }
    
    # Create a SelectDB instance
    resource "alicloud_selectdb_db_instance" "default" {
      db_instance_class       = "selectdb.xlarge"
      db_instance_description = var.name
      cache_size              = 200
      payment_type            = "PayAsYouGo"
      engine_minor_version    = "3.0.14"
      vpc_id                  = alicloud_vpc.vpc_selectdb.id
      zone_id                 = alicloud_vswitch.default.zone_id
      vswitch_id              = alicloud_vswitch.default.id
    }
  2. Initialize the Terraform runtime environment.

    terraform init

    The following output indicates that the initialization is successful.

    Initializing the backend...
    Initializing provider plugins...
    - Finding latest version of hashicorp/alicloud...
    - Installing hashicorp/alicloud v1.252.0...
    - Installed hashicorp/alicloud v1.252.0 (signed by HashiCorp)
    Terraform has created a lock file .terraform.lock.hcl to record the provider
    selections it made above. Include this file in your version control repository
    so that Terraform can guarantee to make the same selections by default when
    you run "terraform init" in the future.
    
    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. Create an execution plan and preview the changes.

    terraform plan
  4. Run the following command to create a SelectDB instance.

    terraform apply

    When prompted during the execution, enter yes and press Enter. Wait for the command to finish. The following output indicates that the instance is created.

    Plan: 3 to add, 0 to change, 0 to destroy.
    
    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
    
    alicloud_vpc.vpc_selectdb: Creating...
    alicloud_vpc.vpc_selectdb: Creation complete after 6s [id=vpc-bp16jelczg9qt0w92****]
    alicloud_vswitch.default: Creating...
    alicloud_vswitch.default: Creation complete after 4s [id=vsw-bp112qrbjti564bng****]
    alicloud_selectdb_db_instance.default: Creating...
    alicloud_selectdb_db_instance.default: Still creating... [00m10s elapsed]
    alicloud_selectdb_db_instance.default: Still creating... [00m20s elapsed]
    alicloud_selectdb_db_instance.default: Still creating... [00m30s elapsed]
    ......
    alicloud_selectdb_db_instance.default: Creation complete after 13m9s [id=selectdb-xxxz631****]
    
    Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
  5. You can verify the result.

    Run the terraform show command

    You can run the following command to query the details of the resources created by Terraform:

    terraform show

    Log on to the SelectDB console

    After the instance is created, you can log on to the ApsaraDB for SelectDB console to verify that the instance was created.

    image

Clean up resources

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

terraform destroy