本文介绍如何使用Terraform创建、修改和删除RDS PostgreSQL实例。
前提条件
使用Terraform,您需要一个阿里云账号和访问密钥(AccessKey),详细操作请参见创建AccessKey。
已经安装并配置了Terraform,详细操作请参见在Cloud Shell中使用Terraform或在本地安装和配置Terraform。
Terraform版本不低于1.189.0。您可以通过
terraform -version
命令查看当前版本,如果不满足要求,请前往Terraform官方网站下载并安装最新版本。如果首次创建RDS PostgreSQL实例,需先授权服务关联角色(AliyunServiceRoleForRdsPgsqlOnEcs),操作步骤,请参见授权服务关联角色。
创建RDS PostgreSQL实例
以创建规格为pg.n2.2c.2m
的RDS PostgreSQL 13实例为例。
在Terraform执行目录下的terraform.tf文件中,配置如下内容。
如果需要创建VPC和交换机,则配置如下:
resource "alicloud_vpc" "main" { vpc_name = "alicloud" cidr_block = "172.16.0.0/16" } resource "alicloud_vswitch" "main" { vpc_id = alicloud_vpc.main.id cidr_block = "172.16.192.0/20" zone_id = "cn-hangzhou-j" depends_on = [alicloud_vpc.main] } resource "alicloud_db_instance" "instance" { engine = "PostgreSQL" engine_version = "13.0" instance_type = "pg.n2.2c.2m" instance_storage = "30" instance_charge_type = "Postpaid" vswitch_id = alicloud_vswitch.main.id }
如果不需要创建VPC和交换机,使用已有的VPC和交换机,则配置如下:
resource "alicloud_db_instance" "instance" { engine = "PostgreSQL" engine_version = "13.0" instance_type = "pg.n2.2c.2m" instance_storage = "30" instance_charge_type = "Postpaid" vswitch_id = "vsw-****" }
说明如果需要创建多个配置相同的RDS PostgreSQL实例,需要在
resource "alicloud_db_instance" "instance"{}
中增加参数count = x
,其中x
标识需要创建的实例数量。如果需要创建多个配置不同的RDS PostgreSQL实例,需要根据不同的配置,创建不同的
resource "alicloud_db_instance" "instance"{}
。各参数含义,请参见Alicloud Documentation for RDS。
运行
terraform apply
。出现如下配置信息后,确认配置信息并输入yes,开始创建。
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # alicloud_db_instance.instance will be created + resource "alicloud_db_instance" "instance" { + acl = (known after apply) + auto_upgrade_minor_version = (known after apply) + babelfish_port = (known after apply) + ca_type = (known after apply) + connection_string = (known after apply) + connection_string_prefix = (known after apply) + db_instance_storage_type = (known after apply) + db_is_ignore_case = (known after apply) + db_time_zone = (known after apply) + deletion_protection = false + engine = "PostgreSQL" + engine_version = "13.0" + force_restart = false + ha_config = (known after apply) + id = (known after apply) + instance_charge_type = "Postpaid" + instance_storage = 30 + instance_type = "pg.n2.2c.2m" + maintain_time = (known after apply) + monitoring_period = (known after apply) + port = (known after apply) + private_ip_address = (known after apply) + replication_acl = (known after apply) + resource_group_id = (known after apply) + security_group_id = (known after apply) + security_group_ids = (known after apply) + security_ip_mode = "normal" + security_ips = (known after apply) + server_cert = (known after apply) + server_key = (known after apply) + sql_collector_config_value = 30 + sql_collector_status = (known after apply) + ssl_action = (known after apply) + ssl_status = (known after apply) + target_minor_version = (known after apply) + tcp_connection_type = (known after apply) + vpc_id = (known after apply) + vswitch_id = (known after apply) + zone_id = (known after apply) + zone_id_slave_a = (known after apply) + babelfish_config { + babelfish_enabled = (known after apply) + master_user_password = (known after apply) + master_username = (known after apply) + migration_mode = (known after apply) } + parameters { + name = (known after apply) + value = (known after apply) } + pg_hba_conf { + address = (known after apply) + database = (known after apply) + mask = (known after apply) + method = (known after apply) + option = (known after apply) + priority_id = (known after apply) + type = (known after apply) + user = (known after apply) } } # alicloud_vpc.main will be created + resource "alicloud_vpc" "main" { + cidr_block = "172.16.0.0/16" + id = (known after apply) + ipv6_cidr_block = (known after apply) + name = (known after apply) + resource_group_id = (known after apply) + route_table_id = (known after apply) + router_id = (known after apply) + router_table_id = (known after apply) + secondary_cidr_blocks = (known after apply) + status = (known after apply) + vpc_name = "alicloud" } # alicloud_vswitch.main will be created + resource "alicloud_vswitch" "main" { + availability_zone = (known after apply) + cidr_block = "172.16.192.0/20" + id = (known after apply) + name = (known after apply) + status = (known after apply) + vpc_id = (known after apply) + vswitch_name = (known after apply) + zone_id = "cn-hangzhou-j" } Plan: 3 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:
出现类似如下日志时,表示创建成功。
alicloud_vpc.main: Creating... alicloud_vpc.main: Creation complete after 7s [id=vpc-****] alicloud_vswitch.main: Creating... alicloud_vswitch.main: Creation complete after 6s [id=vsw-****] alicloud_db_instance.instance: Creating... alicloud_db_instance.instance: Still creating... [10s elapsed] ... alicloud_db_instance.instance: Still creating... [2m30s elapsed] alicloud_db_instance.instance: Creation complete after 4m3s [id=pgm-****] Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
查看结果。
运行
terraform show
查看RDS PostgreSQL实例信息。# alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { client_ca_enabled = 0 client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-***" db_instance_storage_type = "cloud_essd" db_time_zone = "Asia/Shanghai" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Auto" id = "pgm-****" instance_charge_type = "Postpaid" instance_storage = 30 instance_type = "pg.n2.2c.2m" maintain_time = "18:00Z-22:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "172.16.XX.XX" resource_group_id = "rg-****" security_group_ids = [] security_ip_mode = "normal" security_ips = [ "127.0.0.1", ] sql_collector_config_value = 30 sql_collector_status = "Disabled" storage_threshold = 0 storage_upper_bound = 0 target_minor_version = "rds_postgres_1300_20220730" tcp_connection_type = "LONG" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-hangzhou-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 PostgreSQL实例信息。
修改实例名称
以修改RDS PostgreSQL实例名称为terraformtest
为例。
在terraform.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.
查看结果。
运行
terraform show
查看RDS PostgreSQL实例名称。# alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { client_ca_enabled = 0 client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-****" db_instance_storage_type = "cloud_essd" db_time_zone = "Asia/Shanghai" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Auto" id = "pgm-****" instance_charge_type = "Postpaid" instance_name = "terraformtest" instance_storage = 50 instance_type = "pg.n2.2c.2m" maintain_time = "18:00Z-22:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "172.16.XX.XX" resource_group_id = "rg-****" security_group_ids = [] security_ip_mode = "normal" security_ips = [ "127.0.0.1", ] sql_collector_config_value = 30 sql_collector_status = "Disabled" storage_auto_scale = "Enable" storage_threshold = 30 storage_upper_bound = 100 target_minor_version = "rds_postgres_1300_20220730" tcp_connection_type = "LONG" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-hangzhou-j" }
登录RDS控制台查看RDS PostgreSQL实例名称。
修改实例配置
以修改RDS PostgreSQL实例的存储空间为50 GB为例。
将terraform.tf文件的
resource "alicloud_db_instance" "instance"{}
中的instance_storage
参数改为50,具体配置如下:... 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.
查看结果。
运行
terraform show
查看RDS PostgreSQL实例存储空间信息。# alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { client_ca_enabled = 0 client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-****" db_instance_storage_type = "cloud_essd" db_time_zone = "Asia/Shanghai" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Auto" id = "pgm-****" instance_charge_type = "Postpaid" instance_storage = 50 instance_type = "pg.n2.2c.2m" maintain_time = "18:00Z-22:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "172.16.XX.XX" resource_group_id = "rg-****" security_group_ids = [] security_ip_mode = "normal" security_ips = [ "127.0.0.1", ] sql_collector_config_value = 30 sql_collector_status = "Disabled" storage_threshold = 0 storage_upper_bound = 0 target_minor_version = "rds_postgres_1300_20220730" tcp_connection_type = "LONG" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-hangzhou-j" }
登录RDS控制台查看RDS PostgreSQL实例存储空间信息。
设置存储空间自动扩容
以开启存储空间自动扩容且扩容上限为100 GB为例。
在terraform.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.
查看结果。
运行
terraform show
查看RDS PostgreSQL实例自动扩容配置信息。# alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { client_ca_enabled = 0 client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-****" db_instance_storage_type = "cloud_essd" db_time_zone = "Asia/Shanghai" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Auto" id = "pgm-****" instance_charge_type = "Postpaid" instance_storage = 50 instance_type = "pg.n2.2c.2m" maintain_time = "18:00Z-22:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "172.16.XX.XX" resource_group_id = "rg-****" security_group_ids = [] security_ip_mode = "normal" security_ips = [ "127.0.0.1", ] sql_collector_config_value = 30 sql_collector_status = "Disabled" storage_auto_scale = "Enable" storage_threshold = 30 storage_upper_bound = 100 target_minor_version = "rds_postgres_1300_20220730" tcp_connection_type = "LONG" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-hangzhou-j" }
登录RDS控制台查看RDS PostgreSQL实例自动扩容配置信息。
修改实例可维护时间段
以修改RDS PostgreSQL实例可维护时间段为13:00-14:00
为例。
控制台显示时间为北京时间,通过terraform设置时需要配置为UTC时间,北京时间13:00-14:00
对应UTC时间为05:00Z-06:00Z
。
在terraform.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.
查看结果。
运行
terraform show
查看RDS PostgreSQL实例的可维护时间段。# alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { client_ca_enabled = 0 client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-****" db_instance_storage_type = "cloud_essd" db_time_zone = "Asia/Shanghai" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Auto" id = "pgm-****" instance_charge_type = "Postpaid" instance_name = "terraformtest" instance_storage = 50 instance_type = "pg.n2.2c.2m" maintain_time = "05:00Z-06:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "172.16.XX.XX" resource_group_id = "rg-****" security_group_ids = [] security_ip_mode = "normal" security_ips = [ "127.0.0.1", ] sql_collector_config_value = 30 sql_collector_status = "Disabled" storage_auto_scale = "Enable" storage_threshold = 30 storage_upper_bound = 100 target_minor_version = "rds_postgres_1300_20220730" tcp_connection_type = "LONG" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-hangzhou-j" }
登录RDS控制台查看RDS PostgreSQL实例的可维护时间段。
修改实例资源组
以修改RDS PostgreSQL实例资源组为rg-****
为例。
在terraform.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.
查看结果。
运行
terraform show
查看RDS PostgreSQL实例所属的资源组。# alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { client_ca_enabled = 0 client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-****" db_instance_storage_type = "cloud_essd" db_time_zone = "Asia/Shanghai" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Auto" id = "pgm-****" instance_charge_type = "Postpaid" instance_name = "terraformtest" instance_storage = 50 instance_type = "pg.n2.2c.2m" maintain_time = "05:00Z-06:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "172.16.XX.XX" resource_group_id = "rg-****" security_group_ids = [] security_ip_mode = "normal" security_ips = [ "127.0.0.1", ] sql_collector_config_value = 30 sql_collector_status = "Disabled" storage_auto_scale = "Enable" storage_threshold = 30 storage_upper_bound = 100 target_minor_version = "rds_postgres_1300_20220730" tcp_connection_type = "LONG" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-hangzhou-j" }
登录RDS控制台查看RDS PostgreSQL实例所属的资源组。
修改实例可用性检测方式(仅高可用实例)
以修改RDS PostgreSQL实例可用性检测方式为短连接
为例。
在terraform.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.
查看结果。
运行
terraform show
查看RDS PostgreSQL实例可用性检测方式。# alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { client_ca_enabled = 0 client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-****" db_instance_storage_type = "cloud_essd" db_time_zone = "Asia/Shanghai" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Auto" id = "pgm-****" instance_charge_type = "Postpaid" instance_name = "terraformtest" instance_storage = 50 instance_type = "pg.n2.2c.2m" maintain_time = "05:00Z-06:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "172.16.XX.XX" resource_group_id = "rg-****" security_group_ids = [] security_ip_mode = "normal" security_ips = [ "127.0.0.1", ] sql_collector_config_value = 30 sql_collector_status = "Disabled" storage_auto_scale = "Enable" storage_threshold = 30 storage_upper_bound = 100 target_minor_version = "rds_postgres_1300_20220730" tcp_connection_type = "SHORT" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-hangzhou-j" }
登录RDS控制台查看RDS PostgreSQL实例可用性检测方式。
删除实例
在terraform.tf文件中,删除
resource "alicloud_db_instance" "instance"{}
配置项的内容,例如,删除如下信息:... resource "alicloud_db_instance" "instance" { engine = "PostgreSQL" engine_version = "13.0" instance_type = "pg.n2.2c.2m" instance_storage = "30" instance_charge_type = "Postpaid" vswitch_id = alicloud_vswitch.main.id }
运行
terraform apply
。出现如下配置信息后,确认配置信息并输入yes,开始删除RDS PostgreSQL实例。
alicloud_vpc.main: Refreshing state... [id=vpc-****] alicloud_db_instance.instance: Refreshing state... [id=pgm-****] alicloud_vswitch.main: Refreshing state... [id=vsw-****] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # alicloud_db_instance.instance will be destroyed # (because alicloud_db_instance.instance is not in configuration) - resource "alicloud_db_instance" "instance" { - client_ca_enabled = 0 -> null - client_crl_enabled = 0 -> null - connection_string = "pgm-****.pg.rds.aliyuncs.com" -> null - connection_string_prefix = "pgm-****" -> null - db_instance_storage_type = "cloud_essd" -> null - db_time_zone = "Asia/Shanghai" -> null - deletion_protection = false -> null - engine = "PostgreSQL" -> null - engine_version = "13.0" -> null - force_restart = false -> null - ha_config = "Auto" -> null - id = "pgm-****" -> null - instance_charge_type = "Postpaid" -> null - instance_storage = 30 -> null - instance_type = "pg.n2.2c.2m" -> null - maintain_time = "18:00Z-22:00Z" -> null - monitoring_period = 300 -> null - period = 0 -> null - port = "5432" -> null - private_ip_address = "172.16.XX.XX" -> null - resource_group_id = "rg-****" -> null - security_group_ids = [] -> null - security_ip_mode = "normal" -> null - security_ips = [ - "127.0.0.1", ] -> null - sql_collector_config_value = 30 -> null - sql_collector_status = "Disabled" -> null - storage_threshold = 0 -> null - storage_upper_bound = 0 -> null - target_minor_version = "rds_postgres_1300_20220730" -> null - tcp_connection_type = "LONG" -> null - vpc_id = "vpc-****" -> null - vswitch_id = "vsw-****" -> null - zone_id = "cn-hangzhou-j" -> null } Plan: 0 to add, 0 to change, 1 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:
出现类似如下日志时,表示删除实例成功。
alicloud_db_instance.instance: Destroying... [id=pgm-****] alicloud_db_instance.instance: Still destroying... [id=pgm-****, 10s elapsed] alicloud_db_instance.instance: Still destroying... [id=pgm-****, 20s elapsed] alicloud_db_instance.instance: Still destroying... [id=pgm-****, 30s elapsed] alicloud_db_instance.instance: Destruction complete after 32s Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
查看结果。
运行
terraform show
查看。# alicloud_vpc.main: resource "alicloud_vpc" "main" { cidr_block = "172.16.0.0/16" id = "vpc-****" name = "alicloud" resource_group_id = "rg-****" route_table_id = "vtb-****" router_id = "vrt-****" router_table_id = "vtb-****" secondary_cidr_blocks = [] status = "Available" user_cidrs = [] vpc_name = "alicloud" } # alicloud_vswitch.main: resource "alicloud_vswitch" "main" { availability_zone = "cn-hangzhou-j" cidr_block = "172.16.192.0/20" id = "vsw-****" status = "Available" tags = {} vpc_id = "vpc-****" zone_id = "cn-hangzhou-j" }
登录RDS控制台查看RDS PostgreSQL实例已删除。