通过 Terraform 管理 RDS PostgreSQL 实例

更新时间:

本文介绍如何使用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常用命令

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
}