通過 Terraform 管理 RDS PostgreSQL 實例標簽
本文介紹如何使用Terraform為RDS PostgreSQL實例綁定標簽以及解綁標簽。
本教程所含示例代碼支持一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
已創(chuàng)建RDS PostgreSQL實例,詳情請參見創(chuàng)建RDS PostgreSQL實例。
實例狀態(tài)為運行中,您可以通過如下兩種方式查看:
由于阿里云賬號(主賬號)具有資源的所有權限,一旦發(fā)生泄露將面臨重大風險。建議您使用RAM用戶,并為該RAM用戶創(chuàng)建AccessKey,具體操作方式請參見創(chuàng)建RAM用戶和創(chuàng)建AccessKey。
通過RAM授權,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。適用于零成本、快速、便捷地體驗和調(diào)試Terraform的場景。
Cloud Shell:阿里云Cloud Shell中預裝了Terraform的組件,并已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用于低成本、快速、便捷地訪問和使用Terraform的場景。
在本地安裝和配置Terraform:適用于網(wǎng)絡連接較差或需要自定義開發(fā)環(huán)境的場景。
使用的資源
本教程示例包含的部分資源會產(chǎn)生一定費用,請在不需要時及時進行釋放或退訂。
alicloud_vpc:創(chuàng)建專有網(wǎng)絡VPC。
alicloud_vswitch:創(chuàng)建專有網(wǎng)絡交換機。
alicloud_db_instance:創(chuàng)建RDS PostgreSQL實例。
創(chuàng)建綁定標簽
以綁定標簽testTarraform: PG12
為例。
創(chuàng)建一個工作目錄,并在該工作目錄中創(chuàng)建名為main.tf的配置文件,然后將以下代碼復制到main.tf中。
創(chuàng)建前置資源。
variable "region" { default = "cn-heyuan" } variable "zone_id" { default = "cn-heyuan-a" } variable "instance_type" { default = "pg.n2.2c.2m" } provider "alicloud" { region = var.region } # 創(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 }
在main.tf文件的
resource "alicloud_db_instance" "instance" {}
中增加tags
配置項,具體配置如下。... resource "alicloud_db_instance" "instance" { ... tags = { testTarraform = "PG12" } }
執(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 commands will detect it and remind you to do so if necessary.
創(chuàng)建執(zhí)行計劃,并預覽變更。
terraform plan
執(zhí)行以下命令,創(chuàng)建資源。
terraform apply
在執(zhí)行過程中,根據(jù)提示輸入
yes
并按下Enter鍵,等待命令執(zhí)行完成,若出現(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.
驗證結果。
執(zhí)行terraform show命令
您可以使用以下命令查看實例標簽。
terraform show
# alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { acl = null auto_upgrade_minor_version = "Auto" ca_type = null category = "HighAvailability" client_ca_cert = (sensitive value) client_ca_enabled = 0 client_cert_revocation_list = null client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-****" create_time = "2024-10-31T02:29:36Z" db_instance_ip_array_attribute = null db_instance_ip_array_name = null db_instance_storage_type = "cloud_essd" db_instance_type = "Primary" db_param_group_id = null 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 = null instance_storage = 30 instance_type = "pg.n2.2c.2m" maintain_time = "18:00Z-22:00Z" manual_ha_time = null monitoring_period = 300 node_id = "3****" period = 0 port = "5432" private_ip_address = "172.16.206.226" replication_acl = null resource_group_id = "rg-****" security_group_ids = [] security_ip_mode = "normal" security_ip_type = null security_ips = [ "127.0.0.1", ] server_cert = (sensitive value) server_key = null sql_collector_config_value = 30 sql_collector_status = "Disabled" ssl_action = "Close" ssl_connection_string = null ssl_status = null status = "Running" storage_auto_scale = null storage_threshold = 0 storage_upper_bound = 0 tags = { "testTarraform" = "PG12" } target_minor_version = "rds_postgres_1300_20240830" tcp_connection_type = "LONG" tde_status = "Disabled" vpc_id = "vpc-****" vswitch_id = "vsw-****" whitelist_network_type = null zone_id = "cn-heyuan-a" parameters { name = "authentication_timeout" value = "120" } }
登錄RDS管理控制臺
登錄RDS管理控制臺查看實例標簽。
解綁標簽
以解綁標簽testTarraform: PG12
為例。
在上述main.tf文件中,刪除
tags
配置項的內(nèi)容。例如,刪除如下信息。... resource "alicloud_db_instance" "instance" { ... tags = { testTarraform = "PG12" } }
執(zhí)行以下命令。
terraform apply
在執(zhí)行過程中,根據(jù)提示輸入
yes
并按下Enter鍵,等待命令執(zhí)行完成,若出現(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.
驗證結果。
執(zhí)行terraform show命令
您可以使用以下命令查看實例標簽。
terraform show
... # alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { acl = "prefer" ca_type = "aliyun" 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/Heyuan" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Manual" id = "pgm-****" instance_charge_type = "Postpaid" instance_name = "terraformtest" instance_storage = 50 instance_type = "pg.n2.2c.2m" maintain_time = "05:00Z-06:00Z" manual_ha_time = "2024-09-30T09:00:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "192.168.XX.XX" replication_acl = "prefer" resource_group_id = "rg-****" security_group_id = "sg-****" security_group_ids = [ "sg-****", ] security_ip_mode = "normal" security_ips = [ "0.0.0.0/0", ] server_cert = <<-EOT -----BEGIN CERTIFICATE----- MIIE7jCCA9a****fM4ALgBJ2 N9xwKlPQ65q/kux0yErtwhAD -----END CERTIFICATE----- EOT server_key = <<-EOT -----BEGIN RSA PRIVATE KEY----- MIIJKQIBAAKCAg****4yE+e+VAdGp -----END RSA PRIVATE KEY----- EOT sql_collector_config_value = 30 sql_collector_status = "Disabled" ssl_action = "Open" ssl_status = "0" storage_auto_scale = "Enable" storage_threshold = 30 storage_upper_bound = 100 target_minor_version = "rds_postgres_1300_20220830" tcp_connection_type = "SHORT" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-heyuan-a" parameters { name = "authentication_timeout" value = "120" } pg_hba_conf { address = "127.0.0.1" database = "all" method = "md5" priority_id = 1 type = "host" user = "all" } }
登錄RDS管理控制臺
登錄RDS管理控制臺查看實例標簽已刪除。
清理資源
當您不再需要上述通過Terraform創(chuàng)建或管理的資源時,請運行以下命令以釋放資源。關于terraform destroy
的更多信息,請參見Terraform常用命令。
terraform destroy
完整示例
當前示例代碼支持一鍵運行,您可以直接運行代碼。一鍵運行
示例代碼
variable "region" {
default = "cn-heyuan"
}
variable "zone_id" {
default = "cn-heyuan-a"
}
variable "instance_type" {
default = "pg.n2.2c.2m"
}
provider "alicloud" {
region = var.region
}
# 創(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)建綁定標簽
tags = {
testTarraform = "PG12"
}
# 解綁標簽
#tags = {}
}
如果您想體驗更多完整示例,請前往更多完整示例中對應產(chǎn)品的文件夾查看。
相關文檔
Terraform介紹,請參見Terraform產(chǎn)品介紹。
當您遇到由于網(wǎng)絡延遲等原因造成的terraform init超時,導致無法正常下載Provider等情況時,請參見Terraform Init 加速方案配置。
資源編排服務為Terraform提供了托管的能力,您可以創(chuàng)建Terraform類型的模板,定義RDS資源,配置資源參數(shù)和資源間的依賴關系。更多信息,請參見創(chuàng)建Terraform類型模板。