文档

实例管理

更新时间:

本文介绍如何使用Terraform创建、修改和删除RDS PostgreSQL实例。

前提条件

创建RDS PostgreSQL实例

以创建规格为pg.n2.2c.2m的RDS PostgreSQL 13实例为例。

  1. 在Terraform执行目录下的terraform.tf文件中,配置如下内容。

    • 如果需要创建VPC和交换机,则配置如下:

      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
      }
    • 如果不需要创建VPC和交换机,使用已有的VPC和交换机,则配置如下:

      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-****"
      }
    说明
    • 如果需要创建多个配置相同的RDS PostgreSQL实例,需要在resource "alicloud_db_instance" "instance"{}中增加参数count = x,其中x标识需要创建的实例数量。

    • 如果需要创建多个配置不同的RDS PostgreSQL实例,需要根据不同的配置,创建不同的resource "alicloud_db_instance" "instance"{}

    • 各参数含义,请参见Alicloud Documentation for RDS

  2. 运行terraform apply

    出现如下配置信息后,确认配置信息并输入yes,开始创建。

    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:

    出现类似如下日志时,表示创建成功。

    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. 查看结果。

    • 运行terraform show查看RDS PostgreSQL实例信息。

      # 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"
      }
    • 登录RDS控制台查看RDS PostgreSQL实例信息。创建实例

修改实例名称

以修改RDS PostgreSQL实例名称为terraformtest为例。

  1. 在terraform.tf文件的resource "alicloud_db_instance" "instance" {}中增加instance_name配置项,具体配置如下:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      instance_name    = "terraformtest"
    }
  2. 运行terraform apply

    出现如下配置信息后,确认配置信息并输入yes,开始修改RDS PostgreSQL实例配置。

    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:

    出现类似如下日志时,表示配置成功。

    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. 查看结果。

    • 运行terraform show查看RDS PostgreSQL实例名称。

      # 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"
      }
                                      
    • 登录RDS控制台查看RDS PostgreSQL实例名称。实例名称

修改实例配置

以修改RDS PostgreSQL实例的存储空间为50 GB为例。

  1. 将terraform.tf文件的resource "alicloud_db_instance" "instance"{}中的instance_storage参数改为50,具体配置如下:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      instance_storage = "50"
    ...
    }
  2. 运行terraform apply

    出现如下配置信息后,确认配置信息并输入yes,开始修改RDS PostgreSQL实例配置。

    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:

    出现类似如下日志时,表示修改RDS PostgreSQL实例配置成功。

    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. 查看结果。

    • 运行terraform show查看RDS PostgreSQL实例存储空间信息。

      # 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"
      }
                                      
    • 登录RDS控制台查看RDS PostgreSQL实例存储空间信息。RDS实例

设置存储空间自动扩容

以开启存储空间自动扩容且扩容上限为100 GB为例。

  1. 在terraform.tf文件的resource "alicloud_db_instance" "instance"{}中增加storage_auto_scalestorage_thresholdstorage_upper_bound配置项,具体配置如下:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      storage_auto_scale = "Enable"
      storage_threshold = "30"
      storage_upper_bound = "100"
    }
  2. 运行terraform apply

    出现如下配置信息后,确认配置信息并输入yes,开始修改RDS PostgreSQL实例配置。

    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:

    出现类似如下日志时,表示修改RDS PostgreSQL实例配置成功。

    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. 查看结果。

    • 运行terraform show查看RDS PostgreSQL实例自动扩容配置信息。

      # 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"
      }
                                      
    • 登录RDS控制台查看RDS PostgreSQL实例自动扩容配置信息。自动扩容

修改实例可维护时间段

以修改RDS PostgreSQL实例可维护时间段为13:00-14:00为例。

说明

控制台显示时间为北京时间,通过terraform设置时需要配置为UTC时间,北京时间13:00-14:00对应UTC时间为05:00Z-06:00Z

  1. 在terraform.tf文件的resource "alicloud_db_instance" "instance" {}中增加maintain_time配置项,具体配置如下:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      maintain_time    = "05:00Z-06:00Z"
    }
  2. 运行terraform apply

    出现如下配置信息后,确认配置信息并输入yes,开始修改RDS PostgreSQL实例可维护时间段。

    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:

    出现类似如下日志时,表示配置成功。

    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. 查看结果。

    • 运行terraform show查看RDS PostgreSQL实例的可维护时间段。

      # 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"
      }
                                      
    • 登录RDS控制台查看RDS PostgreSQL实例的可维护时间段。可维护时间段

修改实例资源组

以修改RDS PostgreSQL实例资源组为rg-****为例。

  1. 在terraform.tf文件的resource "alicloud_db_instance" "instance" {}中增加resource_group_id配置项,具体配置如下:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      resource_group_id = "rg-****"
    }
  2. 运行terraform apply

    出现如下配置信息后,确认配置信息并输入yes,开始修改RDS PostgreSQL实例配置。

    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:

    出现类似如下日志时,表示配置成功。

    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. 查看结果。

    • 运行terraform show查看RDS PostgreSQL实例所属的资源组。

      # 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"
      }
                                      
    • 登录RDS控制台查看RDS PostgreSQL实例所属的资源组。资源组

修改实例可用性检测方式(仅高可用实例)

以修改RDS PostgreSQL实例可用性检测方式为短连接为例。

  1. 在terraform.tf文件的resource "alicloud_db_instance" "instance" {}中增加tcp_connection_type配置项,具体配置如下:

    ...
    resource "alicloud_db_instance" "instance" {
    ...
      tcp_connection_type = "SHORT"
    }
  2. 运行terraform apply

    出现如下配置信息后,确认配置信息并输入yes,开始修改RDS PostgreSQL实例配置。

    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:

    出现类似如下日志时,表示配置成功。

    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. 查看结果。

    • 运行terraform show查看RDS PostgreSQL实例可用性检测方式。

      # 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"
      }
                                      
    • 登录RDS控制台查看RDS PostgreSQL实例可用性检测方式。服务可用性

删除实例

  1. 在terraform.tf文件中,删除resource "alicloud_db_instance" "instance"{}配置项的内容,例如,删除如下信息:

    ...
    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. 运行terraform apply

    出现如下配置信息后,确认配置信息并输入yes,开始删除RDS PostgreSQL实例。

    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:

    出现类似如下日志时,表示删除实例成功。

    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. 查看结果。

    • 运行terraform show查看。

      # 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"
      }
    • 登录RDS控制台查看RDS PostgreSQL实例已删除。

  • 本页导读 (1)
文档反馈