Use Terraform to manage an ApsaraDB RDS for PostgreSQL instance

Updated at: 2025-01-26 05:18

This topic describes how to use Terraform to create, modify, and delete an ApsaraDB RDS for PostgreSQL instance.

Note

You can run the sample code in this topic with a few clicks. For more information, visit Terraform Explorer.

Prerequisites

  • An Alibaba Cloud account has all permissions on resources within the account. If an Alibaba Cloud account is leaked, the resources are exposed to major risks. We recommend that you use a Resource Access Management (RAM) user and create an AccessKey pair for the RAM user. For more information, see Create a RAM user and Create an AccessKey pair.

  • You must use RAM to manage access permissions on cloud resources in an efficient manner. This helps meet the requirements for multi-user collaboration and allows you to grant permissions to users based on the principle of least privilege (PoLP) to prevent security vulnerabilities caused by excessive permissions. For more information, see Grant permissions to RAM users.

    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "vpc:DescribeVpcAttribute",
            "vpc:DescribeRouteTableList",
            "vpc:DescribeVSwitchAttributes",
            "vpc:DeleteVpc",
            "vpc:DeleteVSwitch",
            "vpc:CreateVpc",
            "vpc:CreateVSwitch"
          ],
          "Resource": "*"
        },
        {
          "Action": "rds:*",
          "Resource": "*",
          "Effect": "Allow"
        },
        {
          "Action": "dbs:*",
          "Resource": "acs:rds:*:*:*",
          "Effect": "Allow"
        },
        {
          "Action": "hdm:*",
          "Resource": "acs:rds:*:*:*",
          "Effect": "Allow"
        },
        {
          "Action": "dms:LoginDatabase",
          "Resource": "acs:rds:*:*:*",
          "Effect": "Allow"
        },
        {
          "Effect": "Allow",
          "Action": "ram:CreateServiceLinkedRole",
          "Resource": "*",
          "Condition": {
            "StringEquals": {
              "ram:ServiceName": [
                "backupencryption.rds.aliyuncs.com"
              ]
            }
          }
        },
        {
          "Effect": "Allow",
          "Action": "bss:ModifyAgreementRecord",
          "Resource": "*"
        },
        {
          "Effect": "Allow",
          "Action": [
            "bss:DescribeOrderList",
            "bss:DescribeOrderDetail",
            "bss:PayOrder",
            "bss:CancelOrder"
          ],
          "Resource": "*"
        }
      ]
    }
  • Prepare the Terraform environment. You can use one of the following methods to use Terraform:

    • Use Terraform in Terraform Explorer: Alibaba Cloud provides Terraform Explorer, an online runtime environment for Terraform. You can use Terraform after you log on to Terraform Explorer without the need to install Terraform. For more information, see Use Terraform in Terraform Explorer. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at no additional cost.

    • Use Terraform in Cloud Shell: Terraform is preinstalled in Cloud Shell and identity credentials are configured. You can directly run Terraform commands in Cloud Shell. For more information, see Use Terraform in Cloud Shell. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at low cost.

    • Install and configure Terraform on your on-premises machine: This method is suitable for scenarios in which network conditions are poor or a custom development environment is used. For more information, see Install and configure Terraform in the local PC.

Resources

Note

You are charged for specific resources. If you no longer require the resources, you must release or unsubscribe from the resources at the earliest opportunity.

Create an RDS instance

This section describes how to create an RDS instance that runs PostgreSQL 13 and uses the pg.n2.2c.2m instance type.

  1. Create a working directory and a configuration file named main.tf in the directory. Copy the following code to the main.tf configuration file:

    variable "region" {
      default = "cn-hangzhou"
    }
    
    provider "alicloud" {
      region = var.region
    }
    
    variable "zone_id" {
      default = "cn-hangzhou-b"
    }
    
    variable "instance_type" {
      default = "pg.n2.2c.2m"
    }
    
    # Create a VPC.
    resource "alicloud_vpc" "main" {
      vpc_name   = "alicloud"
      cidr_block = "172.16.0.0/16"
    }
    
    # Create a vSwitch.
    resource "alicloud_vswitch" "main" {
      vpc_id     = alicloud_vpc.main.id
      cidr_block = "172.16.192.0/20"
      zone_id    = var.zone_id
    }
    
    # Create an RDS instance.
    resource "alicloud_db_instance" "instance" {
      engine               = "PostgreSQL"
      engine_version       = "13.0"
      instance_type        = var.instance_type
      instance_storage     = "30"
      instance_charge_type = "Postpaid"
      vswitch_id           = alicloud_vswitch.main.id
      # If you want to use an existing VPC and vSwitch, use the following configuration:
      # vswitch_id         = "vsw-****"
      # Create multiple RDS instances that have the same configuration. The value x specifies the number of RDS instances that you want to create.
      # count              = x
    }
    Note
    • If you want to create multiple RDS instances that have the same configuration, you must add count = x to the resource "alicloud_db_instance" "instance"{} configuration item. The value x specifies the number of RDS instances that you want to create.

    • If you want to create multiple RDS instances that have different configurations, you must add the resource "alicloud_db_instance" "instance"{} configuration item multiple times and include different fields in the configuration item.

    • For more information about the fields, see Alicloud Documentation for ApsaraDB RDS.

  2. Run the following command to initialize Terraform:

    terraform init

    If the following information is returned, Terraform is initialized.

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1...
    ...
    
    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
  3. Create an execution plan and preview the changes.

    terraform plan
  4. Run the following command to create the resources:

    terraform apply

    During the execution, enter yes as prompted and press the Enter key. Wait until the command is successfully executed. If the following information appears, the operation is successful:

    alicloud_vpc.main: Creating...
    alicloud_vpc.main: Creation complete after 7s [id=vpc-****]
    alicloud_vswitch.main: Creating...
    alicloud_vswitch.main: Creation complete after 6s [id=vsw-****]
    alicloud_db_instance.instance: Creating...
    alicloud_db_instance.instance: Still creating... [10s elapsed]
    ...
    alicloud_db_instance.instance: Still creating... [2m30s elapsed]
    alicloud_db_instance.instance: Creation complete after 4m3s [id=pgm-****]
    
    Apply complete!  Resources: 3 added, 0 changed, 0 destroyed.
  5. Verify the result.

    Run the terraform show command
    Log on to the ApsaraDB RDS console

    Run the following command to query information about the RDS instance that is created by using Terraform:

    terraform show
    # alicloud_db_instance.instance:
    resource "alicloud_db_instance" "instance" {
        client_ca_enabled          = 0
        client_crl_enabled         = 0
        connection_string          = "pgm-****.pg.rds.aliyuncs.com"
        connection_string_prefix   = "pgm-***"
        db_instance_storage_type   = "cloud_essd"
        db_time_zone               = "Asia/Shanghai"
        deletion_protection        = false
        engine                     = "PostgreSQL"
        engine_version             = "13.0"
        force_restart              = false
        ha_config                  = "Auto"
        id                         = "pgm-****"
        instance_charge_type       = "Postpaid"
        instance_storage           = 30
        instance_type              = "pg.n2.2c.2m"
        maintain_time              = "18:00Z-22:00Z"
        monitoring_period          = 300
        period                     = 0
        port                       = "5432"
        private_ip_address         = "172.16.XX.XX"
        resource_group_id          = "rg-****"
        security_group_ids         = []
        security_ip_mode           = "normal"
        security_ips               = [
            "127.0.0.1",
        ]
        sql_collector_config_value = 30
        sql_collector_status       = "Disabled"
        storage_threshold          = 0
        storage_upper_bound        = 0
        target_minor_version       = "rds_postgres_1300_20220730"
        tcp_connection_type        = "LONG"
        vpc_id                     = "vpc-****"
        vswitch_id                 = "vsw-****"
        zone_id                    = "cn-hangzhou-j"
    }
    
    # alicloud_vpc.main:
    resource "alicloud_vpc" "main" {
        cidr_block            = "172.16.0.0/16"
        id                    = "vpc-****"
        name                  = "alicloud"
        resource_group_id     = "rg-****"
        route_table_id        = "vtb-****"
        router_id             = "vrt-****"
        router_table_id       = "vtb-****"
        secondary_cidr_blocks = []
        status                = "Available"
        vpc_name              = "alicloud"
    }
    
    # alicloud_vswitch.main:
    resource "alicloud_vswitch" "main" {
        availability_zone = "cn-hangzhou-j"
        cidr_block        = "172.16.192.0/20"
        id                = "vsw-****"
        status            = "Available"
        vpc_id            = "vpc-****"
        zone_id           = "cn-hangzhou-j"
    }

    Log on to the ApsaraDB RDS console to view information about the RDS instance.

    创建实例

Modify the instance name

This section describes how to change the name of an RDS instance to terraformtest.

  1. In the main.tf file, add the instance_name field to the resource "alicloud_db_instance" "instance" {} configuration item and configure the field based on the following code snippet:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      instance_name    = "terraformtest"
    }
  2. Run the terraform apply command.

    After the following information appears, confirm the information and enter yes to modify the configuration of the RDS instance:

    alicloud_vpc.main: Refreshing state... [id=vpc-****]
    alicloud_vswitch.main: Refreshing state... [id=vsw-****]
    alicloud_db_instance.instance: Refreshing state... [id=pgm-****]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
    following symbols:
      ~ update in-place
    
    Terraform will perform the following actions:
    
      # alicloud_db_instance.instance will be updated in-place
      ~ resource "alicloud_db_instance" "instance" {
            id                         = "pgm-****"
          + instance_name              = "terraformtest"
            # (33 unchanged attributes hidden)
        }
    
    Plan: 0 to add, 1 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:

    If the following logs appear, the operation is successful:

    alicloud_db_instance.instance: Modifying... [id=pgm-****]
    alicloud_db_instance.instance: Modifications complete after 3s [id=pgm-****]
    
    Apply complete!  Resources: 0 added, 1 changed, 0 destroyed.
  3. View the result.

    Run the terraform show command
    Log on to the ApsaraDB RDS console

    Run the following command to view the name of the RDS instance:

    terraform show
    # alicloud_db_instance.instance:
    resource "alicloud_db_instance" "instance" {
        client_ca_enabled          = 0
        client_crl_enabled         = 0
        connection_string          = "pgm-****.pg.rds.aliyuncs.com"
        connection_string_prefix   = "pgm-****"
        db_instance_storage_type   = "cloud_essd"
        db_time_zone               = "Asia/Shanghai"
        deletion_protection        = false
        engine                     = "PostgreSQL"
        engine_version             = "13.0"
        force_restart              = false
        ha_config                  = "Auto"
        id                         = "pgm-****"
        instance_charge_type       = "Postpaid"
        instance_name              = "terraformtest"
        instance_storage           = 50
        instance_type              = "pg.n2.2c.2m"
        maintain_time              = "18:00Z-22:00Z"
        monitoring_period          = 300
        period                     = 0
        port                       = "5432"
        private_ip_address         = "172.16.XX.XX"
        resource_group_id          = "rg-****"
        security_group_ids         = []
        security_ip_mode           = "normal"
        security_ips               = [
            "127.0.0.1",
        ]
        sql_collector_config_value = 30
        sql_collector_status       = "Disabled"
        storage_auto_scale         = "Enable"
        storage_threshold          = 30
        storage_upper_bound        = 100
        target_minor_version       = "rds_postgres_1300_20220730"
        tcp_connection_type        = "LONG"
        vpc_id                     = "vpc-****"
        vswitch_id                 = "vsw-****"
        zone_id                    = "cn-hangzhou-j"
    }
                                    

    Log on to the ApsaraDB RDS console to view the name of the RDS instance.

    实例名称

Modify instance configurations

This section describes how to change the storage capacity of an RDS instance to 50 GB.

  1. In the main.tf file, add the instance_storage field to the resource "alicloud_db_instance" "instance" {} configuration item and configure the field based on the following code snippet:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      instance_storage = "50"
    ...
    }
  2. Run the terraform apply command.

    After the following information appears, confirm the information and enter yes to modify the configuration of the RDS instance:

    alicloud_vpc.main: Refreshing state... [id=vpc-****]
    alicloud_vswitch.main: Refreshing state... [id=vsw-****]
    alicloud_db_instance.instance: Refreshing state... [id=pgm-****]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
    following symbols:
      ~ update in-place
    
    Terraform will perform the following actions:
    
      # alicloud_db_instance.instance will be updated in-place
      ~ resource "alicloud_db_instance" "instance" {
            id                         = "pgm-****"
          ~ instance_storage           = 30 -> 50
            # (31 unchanged attributes hidden)
        }
    
    Plan: 0 to add, 1 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:

    If the following logs appear, the operation is successful:

    alicloud_db_instance.instance: Modifying... [id=pgm-****]
    alicloud_db_instance.instance: Still modifying... [id=pgm-****, 10s elapsed]
    ...
    alicloud_db_instance.instance: Still modifying... [id=pgm-****, 4m0s elapsed]
    alicloud_db_instance.instance: Modifications complete after 4m1s [id=pgm-***]
    
    Apply complete!  Resources: 0 added, 1 changed, 0 destroyed.
  3. View the result.

    Run the terraform show command
    Log on to the ApsaraDB RDS console

    Run the following command to view the storage capacity of the RDS instance:

    terraform show
    # alicloud_db_instance.instance:
    resource "alicloud_db_instance" "instance" {
        client_ca_enabled          = 0
        client_crl_enabled         = 0
        connection_string          = "pgm-****.pg.rds.aliyuncs.com"
        connection_string_prefix   = "pgm-****"
        db_instance_storage_type   = "cloud_essd"
        db_time_zone               = "Asia/Shanghai"
        deletion_protection        = false
        engine                     = "PostgreSQL"
        engine_version             = "13.0"
        force_restart              = false
        ha_config                  = "Auto"
        id                         = "pgm-****"
        instance_charge_type       = "Postpaid"
        instance_storage           = 50
        instance_type              = "pg.n2.2c.2m"
        maintain_time              = "18:00Z-22:00Z"
        monitoring_period          = 300
        period                     = 0
        port                       = "5432"
        private_ip_address         = "172.16.XX.XX"
        resource_group_id          = "rg-****"
        security_group_ids         = []
        security_ip_mode           = "normal"
        security_ips               = [
            "127.0.0.1",
        ]
        sql_collector_config_value = 30
        sql_collector_status       = "Disabled"
        storage_threshold          = 0
        storage_upper_bound        = 0
        target_minor_version       = "rds_postgres_1300_20220730"
        tcp_connection_type        = "LONG"
        vpc_id                     = "vpc-****"
        vswitch_id                 = "vsw-****"
        zone_id                    = "cn-hangzhou-j"
    }
                                    

    Log on to the ApsaraDB RDS console to view the storage capacity of the RDS instance.RDS实例

Configure automatic storage expansion

This section describes how to enable automatic storage expansion for an RDS instance and set the maximum storage capacity to which you can expand to 100 GB.

  1. In the main.tf file, add the storage_auto_scale, storage_threshold, and storage_upper_bound fields to the resource "alicloud_db_instance" "instance"{} configuration item and configure the fields based on the following code snippet:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      storage_auto_scale  = "Enable"
      storage_threshold   = "30"
      storage_upper_bound = "100"
    }
  2. Run the terraform apply command.

    After the following information appears, confirm the information and enter yes to modify the configuration of the RDS instance:

    alicloud_vpc.main: Refreshing state... [id=vpc-****]
    alicloud_vswitch.main: Refreshing state... [id=vsw-****]
    alicloud_db_instance.instance: Refreshing state... [id=pgm-****]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
    following symbols:
      ~ update in-place
    
    Terraform will perform the following actions:
    
      # alicloud_db_instance.instance will be updated in-place
      ~ resource "alicloud_db_instance" "instance" {
            id                         = "pgm-****"
          + storage_auto_scale         = "Enable"
          ~ storage_threshold          = 0 -> 30
          ~ storage_upper_bound        = 0 -> 100
            # (30 unchanged attributes hidden)
        }
    
    Plan: 0 to add, 1 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:

    If the following logs appear, the operation is successful:

    alicloud_db_instance.instance: Modifying... [id=pgm-****]
    alicloud_db_instance.instance: Still modifying... [id=pgm-****, 10s elapsed]
    ...
    alicloud_db_instance.instance: Still modifying... [id=pgm-****, 6m0s elapsed]
    alicloud_db_instance.instance: Modifications complete after 6m7s [id=pgm-****]
    
    Apply complete!  Resources: 0 added, 1 changed, 0 destroyed.
  3. View the result.

    Run the terraform show command
    Log on to the ApsaraDB RDS console

    Run the following command to view the configuration of automatic storage capacity of the RDS instance:

    terraform show
    # alicloud_db_instance.instance:
    resource "alicloud_db_instance" "instance" {
        client_ca_enabled          = 0
        client_crl_enabled         = 0
        connection_string          = "pgm-****.pg.rds.aliyuncs.com"
        connection_string_prefix   = "pgm-****"
        db_instance_storage_type   = "cloud_essd"
        db_time_zone               = "Asia/Shanghai"
        deletion_protection        = false
        engine                     = "PostgreSQL"
        engine_version             = "13.0"
        force_restart              = false
        ha_config                  = "Auto"
        id                         = "pgm-****"
        instance_charge_type       = "Postpaid"
        instance_storage           = 50
        instance_type              = "pg.n2.2c.2m"
        maintain_time              = "18:00Z-22:00Z"
        monitoring_period          = 300
        period                     = 0
        port                       = "5432"
        private_ip_address         = "172.16.XX.XX"
        resource_group_id          = "rg-****"
        security_group_ids         = []
        security_ip_mode           = "normal"
        security_ips               = [
            "127.0.0.1",
        ]
        sql_collector_config_value = 30
        sql_collector_status       = "Disabled"
        storage_auto_scale         = "Enable"
        storage_threshold          = 30
        storage_upper_bound        = 100
        target_minor_version       = "rds_postgres_1300_20220730"
        tcp_connection_type        = "LONG"
        vpc_id                     = "vpc-****"
        vswitch_id                 = "vsw-****"
        zone_id                    = "cn-hangzhou-j"
    }
                                    

    Log on to the ApsaraDB RDS console to view the configuration of automatic storage capacity of the RDS instance:自动扩容

Modify the maintenance window

This section describes how to change the maintenance window of an RDS instance to 13:00-14:00.

Note

The display time in the ApsaraDB RDS console is UTC+8. If you want to use Terraform to set the maintenance window, you must set the start time and end time of the maintenance window in UTC. In this example, the maintenance window is set to 05:00Z-06:00Z, which specifies 13:00-14:00 in UTC+8.

  1. In the main.tf file, add the maintain_time field to the resource "alicloud_db_instance" "instance" {} configuration item and configure the field based on the following code snippet:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      maintain_time    = "05:00Z-06:00Z"
    }
  2. Run the terraform apply command.

    After the following information appears, confirm the information and enter yes to modify the maintenance window of the RDS instance:

    alicloud_vpc.main: Refreshing state... [id=vpc-****]
    alicloud_vswitch.main: Refreshing state... [id=vsw-****]
    alicloud_db_instance.instance: Refreshing state... [id=pgm-****]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
    following symbols:
      ~ update in-place
    
    Terraform will perform the following actions:
    
      # alicloud_db_instance.instance will be updated in-place
      ~ resource "alicloud_db_instance" "instance" {
            id                         = "pgm-****"
          ~ maintain_time              = "18:00Z-22:00Z" -> "05:00Z-06:00Z"
            # (33 unchanged attributes hidden)
        }
    
    Plan: 0 to add, 1 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:

    If the following logs appear, the operation is successful:

    alicloud_db_instance.instance: Modifying... [id=pgm-****]
    alicloud_db_instance.instance: Modifications complete after 4s [id=pgm-****]
    
    Apply complete!  Resources: 0 added, 1 changed, 0 destroyed.
  3. View the result.

    Run the terraform show command
    Log on to the ApsaraDB RDS console

    Run the following command to view the maintenance window of the RDS instance:

    terraform show
    # alicloud_db_instance.instance:
    resource "alicloud_db_instance" "instance" {
        client_ca_enabled          = 0
        client_crl_enabled         = 0
        connection_string          = "pgm-****.pg.rds.aliyuncs.com"
        connection_string_prefix   = "pgm-****"
        db_instance_storage_type   = "cloud_essd"
        db_time_zone               = "Asia/Shanghai"
        deletion_protection        = false
        engine                     = "PostgreSQL"
        engine_version             = "13.0"
        force_restart              = false
        ha_config                  = "Auto"
        id                         = "pgm-****"
        instance_charge_type       = "Postpaid"
        instance_name              = "terraformtest"
        instance_storage           = 50
        instance_type              = "pg.n2.2c.2m"
        maintain_time              = "05:00Z-06:00Z"
        monitoring_period          = 300
        period                     = 0
        port                       = "5432"
        private_ip_address         = "172.16.XX.XX"
        resource_group_id          = "rg-****"
        security_group_ids         = []
        security_ip_mode           = "normal"
        security_ips               = [
            "127.0.0.1",
        ]
        sql_collector_config_value = 30
        sql_collector_status       = "Disabled"
        storage_auto_scale         = "Enable"
        storage_threshold          = 30
        storage_upper_bound        = 100
        target_minor_version       = "rds_postgres_1300_20220730"
        tcp_connection_type        = "LONG"
        vpc_id                     = "vpc-****"
        vswitch_id                 = "vsw-****"
        zone_id                    = "cn-hangzhou-j"
    }
                                    

    Log on to the ApsaraDB RDS console to view the maintenance window of the RDS instance.可维护时间段

Change the resource group

This section describes how to change the resource group of an RDS instance to rg-****.

  1. In the main.tf file, add the resource_group_id field to the resource "alicloud_db_instance" "instance" {} configuration item and configure the field based on the following code snippet:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      resource_group_id = "rg-****"
    }
  2. Run the terraform apply command.

    After the following information appears, confirm the information and enter yes to modify the configuration of the RDS instance:

    alicloud_vpc.main: Refreshing state... [id=vpc-****]
    alicloud_vswitch.main: Refreshing state... [id=vsw-****]
    alicloud_db_instance.instance: Refreshing state... [id=pgm-****]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
    following symbols:
      ~ update in-place
    
    Terraform will perform the following actions:
    
      # alicloud_db_instance.instance will be updated in-place
      ~ resource "alicloud_db_instance" "instance" {
            id                         = "pgm-****"
          ~ resource_group_id          = "rg-****" -> "rg-****"
            # (33 unchanged attributes hidden)
        }
    
    Plan: 0 to add, 1 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:

    If the following logs appear, the operation is successful:

    alicloud_db_instance.instance: Modifying... [id=pgm-****]
    alicloud_db_instance.instance: Modifications complete after 4s [id=pgm-****]
    
    Apply complete!  Resources: 0 added, 1 changed, 0 destroyed.
  3. View the result.

    Run the terraform show command
    Log on to the ApsaraDB RDS console

    Run the following command to view the resource group of the RDS instance:

    terraform show
    # alicloud_db_instance.instance:
    resource "alicloud_db_instance" "instance" {
        client_ca_enabled          = 0
        client_crl_enabled         = 0
        connection_string          = "pgm-****.pg.rds.aliyuncs.com"
        connection_string_prefix   = "pgm-****"
        db_instance_storage_type   = "cloud_essd"
        db_time_zone               = "Asia/Shanghai"
        deletion_protection        = false
        engine                     = "PostgreSQL"
        engine_version             = "13.0"
        force_restart              = false
        ha_config                  = "Auto"
        id                         = "pgm-****"
        instance_charge_type       = "Postpaid"
        instance_name              = "terraformtest"
        instance_storage           = 50
        instance_type              = "pg.n2.2c.2m"
        maintain_time              = "05:00Z-06:00Z"
        monitoring_period          = 300
        period                     = 0
        port                       = "5432"
        private_ip_address         = "172.16.XX.XX"
        resource_group_id          = "rg-****"
        security_group_ids         = []
        security_ip_mode           = "normal"
        security_ips               = [
            "127.0.0.1",
        ]
        sql_collector_config_value = 30
        sql_collector_status       = "Disabled"
        storage_auto_scale         = "Enable"
        storage_threshold          = 30
        storage_upper_bound        = 100
        target_minor_version       = "rds_postgres_1300_20220730"
        tcp_connection_type        = "LONG"
        vpc_id                     = "vpc-****"
        vswitch_id                 = "vsw-****"
        zone_id                    = "cn-hangzhou-j"
    }
                                    

    Log on to the ApsaraDB RDS console to view the resource group of the RDS instance.资源组

Modify the availability check mode (only for RDS instances that run RDS High-availability Edition)

This section describes how to change the availability check mode of an RDS instance to short-lived connection.

  1. In the main.tf file, add the tcp_connection_type field to the resource "alicloud_db_instance" "instance" {} configuration item and configure the field based on the following code snippet:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      tcp_connection_type = "SHORT"
    }
  2. Run the terraform apply command.

    After the following information appears, confirm the information and enter yes to modify the configuration of the RDS instance:

    alicloud_vpc.main: Refreshing state... [id=vpc-****]
    alicloud_vswitch.main: Refreshing state... [id=vsw-****]
    alicloud_db_instance.instance: Refreshing state... [id=pgm-****]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
    following symbols:
      ~ update in-place
    
    Terraform will perform the following actions:
    
      # alicloud_db_instance.instance will be updated in-place
      ~ resource "alicloud_db_instance" "instance" {
            id                         = "pgm-****"
          ~ tcp_connection_type        = "LONG" -> "SHORT"
            # (33 unchanged attributes hidden)
        }
    
    Plan: 0 to add, 1 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:

    If the following logs appear, the operation is successful:

    alicloud_db_instance.instance: Modifying... [id=pgm-****]
    alicloud_db_instance.instance: Modifications complete after 3s [id=pgm-****]
    
    Apply complete!  Resources: 0 added, 1 changed, 0 destroyed.
  3. View the result.

    Run the terraform show command
    Log on to the ApsaraDB RDS console

    Run the following command to view the availability check mode of the RDS instance:

    terraform show
    # alicloud_db_instance.instance:
    resource "alicloud_db_instance" "instance" {
        client_ca_enabled          = 0
        client_crl_enabled         = 0
        connection_string          = "pgm-****.pg.rds.aliyuncs.com"
        connection_string_prefix   = "pgm-****"
        db_instance_storage_type   = "cloud_essd"
        db_time_zone               = "Asia/Shanghai"
        deletion_protection        = false
        engine                     = "PostgreSQL"
        engine_version             = "13.0"
        force_restart              = false
        ha_config                  = "Auto"
        id                         = "pgm-****"
        instance_charge_type       = "Postpaid"
        instance_name              = "terraformtest"
        instance_storage           = 50
        instance_type              = "pg.n2.2c.2m"
        maintain_time              = "05:00Z-06:00Z"
        monitoring_period          = 300
        period                     = 0
        port                       = "5432"
        private_ip_address         = "172.16.XX.XX"
        resource_group_id          = "rg-****"
        security_group_ids         = []
        security_ip_mode           = "normal"
        security_ips               = [
            "127.0.0.1",
        ]
        sql_collector_config_value = 30
        sql_collector_status       = "Disabled"
        storage_auto_scale         = "Enable"
        storage_threshold          = 30
        storage_upper_bound        = 100
        target_minor_version       = "rds_postgres_1300_20220730"
        tcp_connection_type        = "SHORT"
        vpc_id                     = "vpc-****"
        vswitch_id                 = "vsw-****"
        zone_id                    = "cn-hangzhou-j"
    }
                                    

    Log on to the ApsaraDB RDS console to view the availability check mode of the RDS instance.服务可用性

Clear resources

If you no longer require the preceding resources that are created or managed by using Terraform, run the following command to release the resources. For more information about the terraform destroy command, see Common commands.

terraform destroy

Complete sample code

Note

You can run the sample code in this topic with a few clicks. For more information, visit Terraform Explorer.

Sample code

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

provider "alicloud" {
  region = var.region
}

variable "zone_id" {
  default = "cn-hangzhou-b"
}

variable "instance_type" {
  default = "pg.n2.2c.2m"
}

# Create a VPC.
resource "alicloud_vpc" "main" {
  vpc_name   = "alicloud"
  cidr_block = "172.16.0.0/16"
}

# Create a vSwitch.
resource "alicloud_vswitch" "main" {
  vpc_id     = alicloud_vpc.main.id
  cidr_block = "172.16.192.0/20"
  zone_id    = var.zone_id
}

# Create an RDS instance.
resource "alicloud_db_instance" "instance" {
  engine               = "PostgreSQL"
  engine_version       = "13.0"
  instance_type        = var.instance_type
  instance_storage     = "30"
  instance_charge_type = "Postpaid"
  vswitch_id           = alicloud_vswitch.main.id
  # Modify the instance name.
  # instance_name    = "terraformtest"
  # Change the storage capacity of an RDS instance to 50 GB.
  # instance_storage = "50"
  # Configure automatic storage expansion.
  # storage_auto_scale = "Enable"
  # storage_threshold = "30"
  # storage_upper_bound = "100"
  # Modify the maintenance window.
  # maintain_time    = "05:00Z-06:00Z"
  # Change the resource group of the instance.
  # resource_group_id = "rg-****"
  # Modify the availability check mode (only for RDS instances that run RDS High-availability Edition).
  # tcp_connection_type = "SHORT"
  # If you want to use an existing VPC and vSwitch, use the following configuration:
  # vswitch_id       = "vsw-****"
  # Create multiple RDS instances that have the same configuration. The value x specifies the number of RDS instances that you want to create.
  #count = x
}
  • On this page (1, T)
  • Prerequisites
  • Resources
  • Create an RDS instance
  • Modify the instance name
  • Modify instance configurations
  • Configure automatic storage expansion
  • Modify the maintenance window
  • Change the resource group
  • Modify the availability check mode (only for RDS instances that run RDS High-availability Edition)
  • Clear resources
  • Complete sample code
  • Sample code
  • References
Feedback