全部產品
Search
文件中心

ApsaraDB RDS:通過Terraform管理RDS PostgreSQL執行個體

更新時間:Mar 13, 2025

本文介紹如何使用Terraform建立、修改和刪除RDS PostgreSQL執行個體。

說明

本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行

前提條件

  • 由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作方式請參見建立RAM使用者建立AccessKey

  • 通過RAM授權,阿里雲使用者可以有效地管理其雲資源存取權限,適應多使用者協同工作的需求,並且能夠按需為使用者指派最小許可權,避免許可權過大導致的安全性漏洞‌。具體操作方式請參見為RAM使用者授權

    {
      "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": "*"
        }
      ]
    }
  • 準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。

    • 在Terraform Explorer中使用Terraform:阿里雲提供了Terraform的線上運行環境,您無需安裝Terraform,登入後即可線上使用和體驗Terraform。適用於零成本、快速、便捷地體驗和調試Terraform的情境。

    • Cloud Shell:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。

    • 在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。

使用的資源

說明

本教程樣本包含的部分資源會產生一定費用,請在不需要時及時進行釋放或退訂。

建立RDS PostgreSQL執行個體

本文以建立規格為pg.n2.2c.2m的RDS PostgreSQL 13執行個體為例。

  1. 建立一個工作目錄,並在該工作目錄中建立名為main.tf的設定檔,然後將以下代碼複製到main.tf

    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"
    }
    
    # 建立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    = var.zone_id
    }
    
    # 建立RDS PostgreSQL執行個體
    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
      # 如果不需要建立VPC和交換器,使用已有的VPC和交換器
      # vswitch_id         = "vsw-****"
      # 建立多個配置相同的RDS PostgreSQL執行個體,x為需要建立的執行個體數量
      # count              = x
    }
    說明
    • 如果需要建立多個配置相同的RDS PostgreSQL執行個體,需要在resource "alicloud_db_instance" "instance"{}中增加參數count = x,其中x標識需要建立的執行個體數量。

    • 如果需要建立多個配置不同的RDS PostgreSQL執行個體,需要根據不同的配置,建立不同的resource "alicloud_db_instance" "instance"{}

    • 各參數含義,請參見Alicloud Documentation for RDS

  2. 執行以下命令,初始化Terraform運行環境。

    terraform init

    返回如下資訊,表示Terraform初始化成功。

    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. 建立執行計畫,並預覽變更。

    terraform plan
  4. 執行以下命令,建立資源。

    terraform apply

    在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示運行成功。

    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. 驗證結果。

    執行terraform show命令

    您可以使用以下命令查詢Terraform已建立的RDS PostgreSQL執行個體資訊:

    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"
    }

    登入RDS管理主控台

    登入RDS管理主控台查看RDS PostgreSQL執行個體資訊。

    建立執行個體

修改執行個體名稱

本文以修改RDS PostgreSQL執行個體名稱為terraformtest為例。

  1. 在上述main.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執行個體名稱:

    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"
    }
                                    

    登入RDS管理主控台

    登入RDS管理主控台查看RDS PostgreSQL執行個體名稱。

    執行個體名稱

修改執行個體配置

本文以修改RDS PostgreSQL執行個體的儲存空間為50 GB為例。

  1. 在上述main.tf檔案的resource "alicloud_db_instance" "instance" {}中增加instance_storage配置項,具體配置如下:

    ...
    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執行個體儲存空間資訊:

    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"
    }
                                    

    登入RDS管理主控台

    登入RDS管理主控台查看RDS PostgreSQL執行個體儲存空間資訊。RDS執行個體

設定儲存空間自動擴容

以開啟儲存空間自動擴容且擴容上限為100 GB為例。

  1. 在上述main.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執行個體自動擴容配置資訊:

    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"
    }
                                    

    登入RDS管理主控台

    登入RDS管理主控台查看RDS PostgreSQL執行個體自動擴容配置資訊自動擴容

修改執行個體可維護時間段

本文以修改RDS PostgreSQL執行個體可維護時間段為13:00-14:00為例。

說明

控制台時間顯示為北京時間,通過terraform設定時需要配置為UTC時間,北京時間13:00-14:00對應UTC時間為05:00Z-06:00Z

  1. 在上述main.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執行個體的可維護時間段:

    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"
    }
                                    

    登入RDS管理主控台

    登入RDS管理主控台查看RDS PostgreSQL執行個體的可維護時間段。可維護時間段

修改執行個體資源群組

本文以修改RDS PostgreSQL執行個體資源群組為rg-****為例。

  1. 在上述main.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執行個體所屬的資源群組:

    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"
    }
                                    

    登入RDS管理主控台

    登入RDS管理主控台查看RDS PostgreSQL執行個體所屬的資源群組。資源群組

修改執行個體可用性檢測方式(僅高可用執行個體)

本文以修改RDS PostgreSQL執行個體可用性檢測方式為短串連為例。

  1. 在上述main.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執行個體可用性檢測方式:

    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"
    }
                                    

    登入RDS管理主控台

    登入RDS管理主控台查看RDS PostgreSQL執行個體可用性檢測方式。服務可用性

清理資源

當您不再需要上述通過Terraform建立或管理的資源時,請運行以下命令以釋放資源。關於terraform destroy的更多資訊,請參見常用命令

terraform destroy

完整樣本

說明

當前範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行

範例程式碼

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"
}

# 建立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    = var.zone_id
}

# 建立RDS PostgreSQL執行個體
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
  # 修改執行個體名稱
  # instance_name    = "terraformtest"
  # 修改執行個體配置(以修改RDS PostgreSQL執行個體的儲存空間為50 GB為例)
  # instance_storage = "50"
  # 設定儲存空間自動擴容
  # storage_auto_scale = "Enable"
  # storage_threshold = "30"
  # storage_upper_bound = "100"
  # 修改執行個體可維護時間段
  # maintain_time    = "05:00Z-06:00Z"
  # 修改執行個體資源群組
  # resource_group_id = "rg-****"
  # 修改執行個體可用性檢測方式(僅高可用執行個體)
  # tcp_connection_type = "SHORT"
  # 如果不需要建立VPC和交換器,使用已有的VPC和交換器
  # vswitch_id       = "vsw-****"
  # 建立多個配置相同的RDS PostgreSQL執行個體,x為需要建立的執行個體數量
  #count = x
}