通過 Terraform 管理 RDS PostgreSQL 實例
本文介紹如何使用Terraform創(chuàng)建、修改和刪除RDS PostgreSQL實例。
本教程所含示例代碼支持一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
由于阿里云賬號(主賬號)具有資源的所有權限,一旦發(fā)生泄露將面臨重大風險。建議您使用RAM用戶,并為該RAM用戶創(chuàng)建AccessKey,具體操作方式請參見創(chuàng)建RAM用戶和創(chuàng)建AccessKey。
通過RAM授權,阿里云用戶可以有效地管理其云資源訪問權限,適應多用戶協(xié)同工作的需求,并且能夠按需為用戶分配最小權限,避免權限過大導致的安全漏洞?。具體操作方式請參見為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運行環(huán)境,您可以選擇以下任一方式來使用Terraform。
在Terraform Explorer中使用Terraform:阿里云提供了Terraform的在線運行環(huán)境,您無需安裝Terraform,登錄后即可在線使用和體驗Terraform。適用于零成本、快速、便捷地體驗和調試Terraform的場景。
Cloud Shell:阿里云Cloud Shell中預裝了Terraform的組件,并已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用于低成本、快速、便捷地訪問和使用Terraform的場景。
在本地安裝和配置Terraform:適用于網絡連接較差或需要自定義開發(fā)環(huán)境的場景。
使用的資源
本教程示例包含的部分資源會產生一定費用,請在不需要時及時進行釋放或退訂。
alicloud_vpc:創(chuàng)建一個專有網絡VPC。
alicloud_vswitch:創(chuàng)建一個專有網絡交換機。
alicloud_db_instance:創(chuàng)建RDS實例。
創(chuàng)建RDS PostgreSQL實例
本文以創(chuàng)建規(guī)格為pg.n2.2c.2m
的RDS PostgreSQL 13實例為例。
創(chuàng)建一個工作目錄,并在該工作目錄中創(chuàng)建名為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" } # 創(chuàng)建VPC resource "alicloud_vpc" "main" { vpc_name = "alicloud" cidr_block = "172.16.0.0/16" } # 創(chuàng)建交換機 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和交換機,使用已有的VPC和交換機 # vswitch_id = "vsw-****" # 創(chuàng)建多個配置相同的RDS PostgreSQL實例,x為需要創(chuàng)建的實例數量 # count = x }
說明如果需要創(chuàng)建多個配置相同的RDS PostgreSQL實例,需要在
resource "alicloud_db_instance" "instance"{}
中增加參數count = x
,其中x
標識需要創(chuàng)建的實例數量。如果需要創(chuàng)建多個配置不同的RDS PostgreSQL實例,需要根據不同的配置,創(chuàng)建不同的
resource "alicloud_db_instance" "instance"{}
。各參數含義,請參見Alicloud Documentation for RDS。
執(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
創(chuàng)建執(zhí)行計劃,并預覽變更。
terraform plan
執(zhí)行以下命令,創(chuàng)建資源。
terraform apply
在執(zhí)行過程中,根據提示輸入
yes
并按下Enter鍵,等待命令執(zhí)行完成,若出現以下信息,則表示運行成功。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.
驗證結果。
執(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實例信息。
修改實例名稱
本文以修改RDS PostgreSQL實例名稱為terraformtest
為例。
在上述main.tf文件的
resource "alicloud_db_instance" "instance" {}
中增加instance_name
配置項,具體配置如下:... resource "alicloud_db_instance" "instance" { ... instance_name = "terraformtest" }
運行
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.
查看結果。
執(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為例。
在上述main.tf文件的
resource "alicloud_db_instance" "instance" {}
中增加instance_storage配置項,具體配置如下:... resource "alicloud_db_instance" "instance" { ... instance_storage = "50" ... }
運行
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.
查看結果。
執(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實例存儲空間信息。
設置存儲空間自動擴容
以開啟存儲空間自動擴容且擴容上限為100 GB為例。
在上述main.tf文件的
resource "alicloud_db_instance" "instance"{}
中增加storage_auto_scale
、storage_threshold
和storage_upper_bound
配置項,具體配置如下:... resource "alicloud_db_instance" "instance" { ... storage_auto_scale = "Enable" storage_threshold = "30" storage_upper_bound = "100" }
運行
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.
查看結果。
執(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_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
。
在上述main.tf文件的
resource "alicloud_db_instance" "instance" {}
中增加maintain_time
配置項,具體配置如下:... resource "alicloud_db_instance" "instance" { ... maintain_time = "05:00Z-06:00Z" }
運行
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.
查看結果。
執(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實例資源組為rg-****
為例。
在上述main.tf文件的
resource "alicloud_db_instance" "instance" {}
中增加resource_group_id
配置項,具體配置如下:... resource "alicloud_db_instance" "instance" { ... resource_group_id = "rg-****" }
運行
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.
查看結果。
執(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實例可用性檢測方式為短連接
為例。
在上述main.tf文件的
resource "alicloud_db_instance" "instance" {}
中增加tcp_connection_type
配置項,具體配置如下:... resource "alicloud_db_instance" "instance" { ... tcp_connection_type = "SHORT" }
運行
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.
查看結果。
執(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實例可用性檢測方式。
清理資源
當您不再需要上述通過Terraform創(chuàng)建或管理的資源時,請運行以下命令以釋放資源。關于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"
}
# 創(chuàng)建VPC
resource "alicloud_vpc" "main" {
vpc_name = "alicloud"
cidr_block = "172.16.0.0/16"
}
# 創(chuàng)建交換機
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"
# 設置存儲空間自動擴容
# 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"
# 如果不需要創(chuàng)建VPC和交換機,使用已有的VPC和交換機
# vswitch_id = "vsw-****"
# 創(chuàng)建多個配置相同的RDS PostgreSQL實例,x為需要創(chuàng)建的實例數量
#count = x
}