日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

通過 Terraform 管理 RDS PostgreSQL 實例

本文介紹如何使用Terraform創(chuàng)建、修改和刪除RDS PostgreSQL實例。

說明

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

前提條件

  • 由于阿里云賬號(主賬號)具有資源的所有權(quán)限,一旦發(fā)生泄露將面臨重大風(fēng)險。建議您使用RAM用戶,并為該RAM用戶創(chuàng)建AccessKey,具體操作方式請參見創(chuàng)建RAM用戶創(chuàng)建AccessKey

  • 通過RAM授權(quán),阿里云用戶可以有效地管理其云資源訪問權(quán)限,適應(yīng)多用戶協(xié)同工作的需求,并且能夠按需為用戶分配最小權(quán)限,避免權(quán)限過大導(dǎo)致的安全漏洞?。具體操作方式請參見為RAM用戶授權(quán)

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

    • 在Terraform Explorer中使用Terraform:阿里云提供了Terraform的在線運行環(huán)境,您無需安裝Terraform,登錄后即可在線使用和體驗Terraform。適用于零成本、快速、便捷地體驗和調(diào)試Terraform的場景。

    • 在Cloud Shell中使用Terraform:阿里云Cloud Shell中預(yù)裝了Terraform的組件,并已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用于低成本、快速、便捷地訪問和使用Terraform的場景。

    • 在本地安裝和配置Terraform:適用于網(wǎng)絡(luò)連接較差或需要自定義開發(fā)環(huán)境的場景。

使用的資源

說明

本教程示例包含的部分資源會產(chǎn)生一定費用,請在不需要時及時進(jìn)行釋放或退訂。

創(chuàng)建RDS PostgreSQL實例

本文以創(chuàng)建規(guī)格為pg.n2.2c.2m的RDS PostgreSQL 13實例為例。

  1. 創(chuàng)建一個工作目錄,并在該工作目錄中創(chuàng)建名為main.tf的配置文件,然后將以下代碼復(fù)制到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"
    }
    
    # 創(chuàng)建VPC
    resource "alicloud_vpc" "main" {
      vpc_name   = "alicloud"
      cidr_block = "172.16.0.0/16"
    }
    
    # 創(chuàng)建交換機(jī)
    resource "alicloud_vswitch" "main" {
      vpc_id     = alicloud_vpc.main.id
      cidr_block = "172.16.192.0/20"
      zone_id    = var.zone_id
    }
    
    # 創(chuàng)建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
      # 如果不需要創(chuàng)建VPC和交換機(jī),使用已有的VPC和交換機(jī)
      # vswitch_id         = "vsw-****"
      # 創(chuàng)建多個配置相同的RDS PostgreSQL實例,x為需要創(chuàng)建的實例數(shù)量
      # count              = x
    }
    說明
    • 如果需要創(chuàng)建多個配置相同的RDS PostgreSQL實例,需要在resource "alicloud_db_instance" "instance"{}中增加參數(shù)count = x,其中x標(biāo)識需要創(chuàng)建的實例數(shù)量。

    • 如果需要創(chuàng)建多個配置不同的RDS PostgreSQL實例,需要根據(jù)不同的配置,創(chuàng)建不同的resource "alicloud_db_instance" "instance"{}

    • 各參數(shù)含義,請參見Alicloud Documentation for RDS

  2. 執(zhí)行以下命令,初始化Terraform運行環(huán)境。

    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. 創(chuàng)建執(zhí)行計劃,并預(yù)覽變更。

    terraform plan
  4. 執(zhí)行以下命令,創(chuàng)建資源。

    terraform apply

    在執(zhí)行過程中,根據(jù)提示輸入yes并按下Enter鍵,等待命令執(zhí)行完成,若出現(xiàn)以下信息,則表示運行成功。

    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. 驗證結(jié)果。

    執(zhí)行terraform show命令

    您可以使用以下命令查詢Terraform已創(chuàng)建的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實例信息。

    創(chuàng)建實例

修改實例名稱

本文以修改RDS PostgreSQL實例名稱為terraformtest為例。

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

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

    出現(xiàn)如下配置信息后,確認(rèn)配置信息并輸入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:

    出現(xiàn)類似如下日志時,表示配置成功。

    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. 查看結(jié)果。

    執(zhí)行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

    出現(xiàn)如下配置信息后,確認(rèn)配置信息并輸入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:

    出現(xiàn)類似如下日志時,表示修改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. 查看結(jié)果。

    執(zhí)行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實例

設(shè)置存儲空間自動擴(kuò)容

以開啟存儲空間自動擴(kuò)容且擴(kuò)容上限為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

    出現(xiàn)如下配置信息后,確認(rèn)配置信息并輸入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:

    出現(xiàn)類似如下日志時,表示修改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. 查看結(jié)果。

    執(zhí)行terraform show命令

    您可以使用以下命令查看RDS PostgreSQL實例自動擴(kuò)容配置信息:

    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實例自動擴(kuò)容配置信息自動擴(kuò)容

修改實例可維護(hù)時間段

本文以修改RDS PostgreSQL實例可維護(hù)時間段為13:00-14:00為例。

說明

控制臺顯示時間為北京時間,通過terraform設(shè)置時需要配置為UTC時間,北京時間13:00-14:00對應(yīng)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

    出現(xiàn)如下配置信息后,確認(rèn)配置信息并輸入yes,開始修改RDS PostgreSQL實例可維護(hù)時間段。

    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:

    出現(xiàn)類似如下日志時,表示配置成功。

    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. 查看結(jié)果。

    執(zhí)行terraform show命令

    您可以使用以下命令查看RDS PostgreSQL實例的可維護(hù)時間段:

    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實例的可維護(hù)時間段。可維護(hù)時間段

修改實例資源組

本文以修改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

    出現(xiàn)如下配置信息后,確認(rèn)配置信息并輸入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:

    出現(xiàn)類似如下日志時,表示配置成功。

    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. 查看結(jié)果。

    執(zhí)行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

    出現(xiàn)如下配置信息后,確認(rèn)配置信息并輸入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:

    出現(xiàn)類似如下日志時,表示配置成功。

    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. 查看結(jié)果。

    執(zhí)行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實例可用性檢測方式。服務(wù)可用性

清理資源

當(dāng)您不再需要上述通過Terraform創(chuàng)建或管理的資源時,請運行以下命令以釋放資源。關(guān)于terraform destroy的更多信息,請參見Terraform常用命令

terraform destroy

完整示例

說明

當(dāng)前示例代碼支持一鍵運行,您可以直接運行代碼。一鍵運行

示例代碼

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

# 創(chuàng)建VPC
resource "alicloud_vpc" "main" {
  vpc_name   = "alicloud"
  cidr_block = "172.16.0.0/16"
}

# 創(chuàng)建交換機(jī)
resource "alicloud_vswitch" "main" {
  vpc_id     = alicloud_vpc.main.id
  cidr_block = "172.16.192.0/20"
  zone_id    = var.zone_id
}

# 創(chuàng)建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"
  # 設(shè)置存儲空間自動擴(kuò)容
  # storage_auto_scale = "Enable"
  # storage_threshold = "30"
  # storage_upper_bound = "100"
  # 修改實例可維護(hù)時間段
  # maintain_time    = "05:00Z-06:00Z"
  # 修改實例資源組
  # resource_group_id = "rg-****"
  # 修改實例可用性檢測方式(僅高可用實例)
  # tcp_connection_type = "SHORT"
  # 如果不需要創(chuàng)建VPC和交換機(jī),使用已有的VPC和交換機(jī)
  # vswitch_id       = "vsw-****"
  # 創(chuàng)建多個配置相同的RDS PostgreSQL實例,x為需要創(chuàng)建的實例數(shù)量
  #count = x
}