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

Prerequisites

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. In the Terraform directory, configure the terraform.tf file.
    • If you want to create a virtual private cloud (VPC) and a vSwitch for the RDS instance, use the following configuration:
      resource "alicloud_vpc" "main" {
        vpc_name       = "alicloud"
        cidr_block = "172.16.0.0/16"
      }
      
      resource "alicloud_vswitch" "main" {
        vpc_id            = alicloud_vpc.main.id
        cidr_block        = "172.16.192.0/20"
        zone_id = "cn-hangzhou-h"
        depends_on = [alicloud_vpc.main]
      }
      
      resource "alicloud_db_instance" "instance" {
        engine           = "PostgreSQL"
        engine_version   = "13.0"
        instance_type    = "pg.n2.2c.2m"
        instance_storage = "30"
        instance_charge_type = "Postpaid"
        vswitch_id       = alicloud_vswitch.main.id
      }
    • If you want to use an existing VPC and vSwitch for the RDS instance, use the following configuration:
      resource "alicloud_db_instance" "instance" {
        engine           = "PostgreSQL"
        engine_version   = "13.0"
        instance_type    = "pg.n2.2c.2m"
        instance_storage = "30"
        instance_charge_type = "Postpaid"
        vswitch_id       = "vsw-****"
      }
    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 for the count field 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 terraform apply command.
    After the following information appears, confirm the information and enter yes to create the RDS instance:
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
    following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # alicloud_db_instance.instance will be created
      + resource "alicloud_db_instance" "instance" {
          + acl                        = (known after apply)
          + auto_upgrade_minor_version = (known after apply)
          + babelfish_port             = (known after apply)
          + ca_type                    = (known after apply)
          + connection_string          = (known after apply)
          + connection_string_prefix   = (known after apply)
          + db_instance_storage_type   = (known after apply)
          + db_is_ignore_case          = (known after apply)
          + db_time_zone               = (known after apply)
          + deletion_protection        = false
          + engine                     = "PostgreSQL"
          + engine_version             = "13.0"
          + force_restart              = false
          + ha_config                  = (known after apply)
          + id                         = (known after apply)
          + instance_charge_type       = "Postpaid"
          + instance_storage           = 30
          + instance_type              = "pg.n2.2c.2m"
          + maintain_time              = (known after apply)
          + monitoring_period          = (known after apply)
          + port                       = (known after apply)
          + private_ip_address         = (known after apply)
          + replication_acl            = (known after apply)
          + resource_group_id          = (known after apply)
          + security_group_id          = (known after apply)
          + security_group_ids         = (known after apply)
          + security_ip_mode           = "normal"
          + security_ips               = (known after apply)
          + server_cert                = (known after apply)
          + server_key                 = (known after apply)
          + sql_collector_config_value = 30
          + sql_collector_status       = (known after apply)
          + ssl_action                 = (known after apply)
          + ssl_status                 = (known after apply)
          + target_minor_version       = (known after apply)
          + tcp_connection_type        = (known after apply)
          + vpc_id                     = (known after apply)
          + vswitch_id                 = (known after apply)
          + zone_id                    = (known after apply)
          + zone_id_slave_a            = (known after apply)
    
          + babelfish_config {
              + babelfish_enabled    = (known after apply)
              + master_user_password = (known after apply)
              + master_username      = (known after apply)
              + migration_mode       = (known after apply)
            }
    
          + parameters {
              + name  = (known after apply)
              + value = (known after apply)
            }
    
          + pg_hba_conf {
              + address     = (known after apply)
              + database    = (known after apply)
              + mask        = (known after apply)
              + method      = (known after apply)
              + option      = (known after apply)
              + priority_id = (known after apply)
              + type        = (known after apply)
              + user        = (known after apply)
            }
        }
    
      # alicloud_vpc.main will be created
      + resource "alicloud_vpc" "main" {
          + cidr_block            = "172.16.0.0/16"
          + id                    = (known after apply)
          + ipv6_cidr_block       = (known after apply)
          + name                  = (known after apply)
          + resource_group_id     = (known after apply)
          + route_table_id        = (known after apply)
          + router_id             = (known after apply)
          + router_table_id       = (known after apply)
          + secondary_cidr_blocks = (known after apply)
          + status                = (known after apply)
          + vpc_name              = "alicloud"
        }
    
      # alicloud_vswitch.main will be created
      + resource "alicloud_vswitch" "main" {
          + availability_zone = (known after apply)
          + cidr_block        = "172.16.192.0/20"
          + id                = (known after apply)
          + name              = (known after apply)
          + status            = (known after apply)
          + vpc_id            = (known after apply)
          + vswitch_name      = (known after apply)
          + zone_id           = "cn-hangzhou-h"
        }
    
    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:

    If the following logs appear, 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.
  3. View the result.
    • Run the terraform show command to view information about the created RDS instance.
      # 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-h"
      }
      
      # 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-h"
          cidr_block        = "172.16.192.0/20"
          id                = "vsw-****"
          status            = "Available"
          vpc_id            = "vpc-****"
          zone_id           = "cn-hangzhou-h"
      }
    • Log on to the ApsaraDB RDS console to view information about the created RDS instance. Create an RDS instance

Change the name of an RDS instance

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

  1. In the terraform.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 apply the changes:
    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 to view the name of the RDS instance.
      # 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-h"
      }
                                      
    • Log on to the ApsaraDB RDS console to view the name of the RDS instance. Instance name

Change the configuration of an RDS instance

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

  1. In the terraform.tf file, change the value of the instance_storage field in the resource "alicloud_db_instance" "instance"{} configuration item to 50.
    ...
    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 apply the changes:
    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 to view the storage capacity of the RDS instance.
      # 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-h"
      }
                                      
    • Log on to the ApsaraDB RDS console to view the storage capacity of the RDS instance. RDS instance

Configure automatic storage expansion for an RDS instance

This section describes how to enable automatic storage expansion for an RDS instance and set the expansion threshold to 100 GB.

  1. In the terraform.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 apply the changes:
    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 to view the information about automatic storage expansion for the RDS instance.
      # 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-h"
      }
                                      
    • Log on to the ApsaraDB RDS console to view the information about automatic storage expansion for the RDS instance. Automatic storage expansion

Change the maintenance window of an RDS instance

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, set the maintenance window to 05:00Z-06:00Z, which specifies 13:00-14:00 in UTC+8.
  1. In the terraform.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 apply the changes:
    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 to view the maintenance window of the RDS instance.
      # 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-h"
      }
                                      
    • Log on to the ApsaraDB RDS console to view the maintenance window of the RDS instance. Maintenance window

Change the resource group of an RDS instance

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

  1. In the terraform.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 apply the changes:
    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 to view the resource group of the RDS instance.
      # 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-h"
      }
                                      
    • Log on to the ApsaraDB RDS console to view the resource group of the RDS instance. Resource group

Change the availability check mode of an RDS instances (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 terraform.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 apply the changes:
    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 to view the availability check mode of the RDS instance.
      # 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-h"
      }
                                      
    • Log on to the ApsaraDB RDS console to view the availability check mode of the RDS instance. Service availability

Delete an RDS instance

  1. In the terraform.tf file, delete the resource "alicloud_db_instance" "instance"{} configuration item. In this example, delete the following information:
    ...
    resource "alicloud_db_instance" "instance" {
      engine           = "PostgreSQL"
      engine_version   = "13.0"
      instance_type    = "pg.n2.2c.2m"
      instance_storage = "30"
      instance_charge_type = "Postpaid"
      vswitch_id       = alicloud_vswitch.main.id
    }
  2. Run the terraform apply command.

    After the following information appears, confirm the information and enter yes to delete the RDS instance:

    alicloud_vpc.main: Refreshing state... [id=vpc-****]
    alicloud_db_instance.instance: Refreshing state... [id=pgm-****]
    alicloud_vswitch.main: Refreshing state... [id=vsw-****]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
    following symbols:
      - destroy
    
    Terraform will perform the following actions:
    
      # alicloud_db_instance.instance will be destroyed
      # (because alicloud_db_instance.instance is not in configuration)
      - resource "alicloud_db_instance" "instance" {
          - client_ca_enabled          = 0 -> null
          - client_crl_enabled         = 0 -> null
          - connection_string          = "pgm-****.pg.rds.aliyuncs.com" -> null
          - connection_string_prefix   = "pgm-****" -> null
          - db_instance_storage_type   = "cloud_essd" -> null
          - db_time_zone               = "Asia/Shanghai" -> null
          - deletion_protection        = false -> null
          - engine                     = "PostgreSQL" -> null
          - engine_version             = "13.0" -> null
          - force_restart              = false -> null
          - ha_config                  = "Auto" -> null
          - id                         = "pgm-****" -> null
          - instance_charge_type       = "Postpaid" -> null
          - instance_storage           = 30 -> null
          - instance_type              = "pg.n2.2c.2m" -> null
          - maintain_time              = "18:00Z-22:00Z" -> null
          - monitoring_period          = 300 -> null
          - period                     = 0 -> null
          - port                       = "5432" -> null
          - private_ip_address         = "172.16.XX.XX" -> null
          - resource_group_id          = "rg-****" -> null
          - security_group_ids         = [] -> null
          - security_ip_mode           = "normal" -> null
          - security_ips               = [
              - "127.0.0.1",
            ] -> null
          - sql_collector_config_value = 30 -> null
          - sql_collector_status       = "Disabled" -> null
          - storage_threshold          = 0 -> null
          - storage_upper_bound        = 0 -> null
          - target_minor_version       = "rds_postgres_1300_20220730" -> null
          - tcp_connection_type        = "LONG" -> null
          - vpc_id                     = "vpc-****" -> null
          - vswitch_id                 = "vsw-****" -> null
          - zone_id                    = "cn-hangzhou-h" -> null
        }
    
    Plan: 0 to add, 0 to change, 1 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 RDS instance is deleted:

    alicloud_db_instance.instance: Destroying... [id=pgm-****]
    alicloud_db_instance.instance: Still destroying... [id=pgm-****, 10s elapsed]
    alicloud_db_instance.instance: Still destroying... [id=pgm-****, 20s elapsed]
    alicloud_db_instance.instance: Still destroying... [id=pgm-****, 30s elapsed]
    alicloud_db_instance.instance: Destruction complete after 32s
    
    Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
  3. View the result.
    • Run the terraform show command to check whether the RDS instance is deleted.
      # 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"
          user_cidrs            = []
          vpc_name              = "alicloud"
      }
      
      # alicloud_vswitch.main:
      resource "alicloud_vswitch" "main" {
          availability_zone = "cn-hangzhou-h"
          cidr_block        = "172.16.192.0/20"
          id                = "vsw-****"
          status            = "Available"
          tags              = {}
          vpc_id            = "vpc-****"
          zone_id           = "cn-hangzhou-h"
      }
    • Log on to the ApsaraDB RDS console to check whether the RDS instance is deleted.