通過 Terraform 查詢 RDS PostgreSQL 實例相關配置
本文介紹如何使用Terraform查詢RDS PostgreSQL實例的相關配置。
本教程所含示例代碼支持一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
已創建RDS PostgreSQL實例,詳情請參見創建RDS PostgreSQL實例。
實例狀態為運行中,您可以通過如下兩種方式查看:
由于阿里云賬號(主賬號)具有資源的所有權限,一旦發生泄露將面臨重大風險。建議您使用RAM用戶,并為該RAM用戶創建AccessKey,具體操作方式請參見創建RAM用戶和創建AccessKey。
通過RAM授權,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:適用于網絡連接較差或需要自定義開發環境的場景。
使用的資源
本教程示例包含的部分資源會產生一定費用,請在不需要時及時進行釋放或退訂。
alicloud_vpc:創建專有網絡VPC。
alicloud_vswitch:創建專有網絡交換機。
alicloud_db_instance:創建RDS PostgreSQL實例。
alicloud_db_zones:查詢實例可用區資源。
alicloud_db_instance_classes:查詢可購買的實例規格。
alicloud_regions:查詢實例地域信息。
alicloud_db_instances:查詢實例列表。
查詢可用區資源
本示例演示如何查詢RDS PostgreSQL實例的可用區資源。
創建一個工作目錄,并在該工作目錄中創建名為main.tf的配置文件,然后將以下代碼復制到main.tf中。
創建前置資源。
variable "region" { default = "cn-heyuan" } provider "alicloud" { region = var.region } variable "zone_id" { default = "cn-heyuan-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 }
在main.tf文件中增加
data "alicloud_db_zones" "queryzones" {}
配置項。... data "alicloud_db_zones" "queryzones" { instance_charge_type = "PostPaid" engine = "PostgreSQL" db_instance_storage_type = "cloud_essd" }
執行以下命令,初始化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 commands will detect it and remind you to do so if necessary.
創建執行計劃,并預覽變更。
terraform plan
執行以下命令,創建資源。
terraform apply
在執行過程中,根據提示輸入
yes
并按下Enter鍵,等待命令執行完成,若出現以下信息,則表示運行成功。data.alicloud_db_zones.queryzones: Reading... data.alicloud_db_zones.queryzones: Read complete after 1s [id=262******] No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
驗證結果。
您可以使用以下命令查看結果:
terraform show
# data.alicloud_db_zones.queryzones: data "alicloud_db_zones" "queryzones" { db_instance_storage_type = "cloud_essd" engine = "PostgreSQL" id = "262******" ids = [ "cn-heyuan-b", ] instance_charge_type = "PostPaid" multi = false multi_zone = false zones = [ { id = "cn-hangzhou-b" multi_zone_ids = [] }, ] }
查詢可購買的實例規格
在main.tf文件增加如下內容。
... data "alicloud_db_instance_classes" "queryclasses" { instance_charge_type= "PostPaid" engine = "PostgreSQL" db_instance_storage_type = "cloud_essd" }
執行以下命令。
terraform apply
在執行過程中,根據提示輸入
yes
并按下Enter鍵,等待命令執行完成,若出現以下信息,則表示運行成功。data.alicloud_db_instance_classes.queryclasses: Reading... alicloud_vpc.main: Refreshing state... [id=vpc-****] alicloud_vswitch.main: Refreshing state... [id=vsw-****] alicloud_db_instance.instance: Refreshing state... [id=pgm-****] data.alicloud_db_instance_classes.queryclasses: Still reading... [10s elapsed] data.alicloud_db_instance_classes.queryclasses: Still reading... [20s elapsed] data.alicloud_db_instance_classes.queryclasses: Still reading... [30s elapsed] data.alicloud_db_instance_classes.queryclasses: Still reading... [40s elapsed] data.alicloud_db_instance_classes.queryclasses: Still reading... [50s elapsed] data.alicloud_db_instance_classes.queryclasses: Still reading... [1m0s elapsed] ... data.alicloud_db_instance_classes.queryclasses: Still reading... [6m50s elapsed] data.alicloud_db_instance_classes.queryclasses: Still reading... [7m0s elapsed] data.alicloud_db_instance_classes.queryclasses: Read complete after 7m9s [id=130302****] No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
驗證結果。
您可以使用以下命令查看結果:
terraform show
{ instance_class = "pg.n8.8xlarge.1" price = "" storage_range = { "max" = "32000" "min" = "1500" "step" = "5" } zone_ids = [ { id = "cn-hangzhou-k" sub_zone_ids = [] }, ] }, { instance_class = "pg.n2.small.1" price = "" storage_range = { "max" = "32000" "min" = "1500" "step" = "5" } zone_ids = [ { id = "cn-hangzhou-k" sub_zone_ids = [] }, ] }, ......
查詢地域信息
在main.tf文件增加如下內容。
data "alicloud_regions" "query_regions" { }
執行以下命令。
terraform apply
在執行過程中,根據提示輸入
yes
并按下Enter鍵,等待命令執行完成,若出現以下信息,則表示運行成功。data.alicloud_regions.query_regions: Reading... alicloud_vpc.main: Refreshing state... [id=vpc-****] alicloud_vswitch.main: Refreshing state... [id=vsw-****] data.alicloud_regions.query_regions: Read complete after 1s [id=2105****] alicloud_db_instance.instance: Refreshing state... [id=pgm-****] No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
驗證結果。
您可以使用以下命令查看結果:
terraform show
# data.alicloud_regions.query_regions: data "alicloud_regions" "query_regions" { id = "2105470773" ids = [ "cn-qingdao", "cn-beijing", "cn-zhangjiakou", "cn-huhehaote", "cn-wulanchabu", "cn-hangzhou", "cn-shanghai", "cn-nanjing", "cn-shenzhen", "cn-heyuan", "cn-guangzhou", "cn-fuzhou", "cn-chengdu", "cn-hongkong", "ap-northeast-1", "ap-northeast-2", "ap-southeast-1", "ap-southeast-2", "ap-southeast-3", "ap-southeast-6", "ap-southeast-5", "ap-southeast-7", "us-east-1", "us-west-1", "eu-west-1", "me-east-1", "me-central-1", "eu-central-1", ] regions = [ { id = "cn-qingdao" local_name = "華北1(青島)" region_id = "cn-qingdao" }, { id = "cn-beijing" local_name = "華北2(北京)" region_id = "cn-beijing" }, { id = "cn-zhangjiakou" local_name = "華北3(張家口)" region_id = "cn-zhangjiakou" }, { id = "cn-huhehaote" local_name = "華北5(呼和浩特)" region_id = "cn-huhehaote" }, { id = "cn-wulanchabu" local_name = "華北6(烏蘭察布)" region_id = "cn-wulanchabu" }, { id = "cn-hangzhou" local_name = "華東1(杭州)" region_id = "cn-hangzhou" }, { id = "cn-shanghai" local_name = "華東2(上海)" region_id = "cn-shanghai" }, { id = "cn-nanjing" local_name = "華東5(南京-本地地域)" region_id = "cn-nanjing" }, { id = "cn-shenzhen" local_name = "華南1(深圳)" region_id = "cn-shenzhen" }, { id = "cn-heyuan" local_name = "華南2(河源)" region_id = "cn-heyuan" }, { id = "cn-guangzhou" local_name = "華南3(廣州)" region_id = "cn-guangzhou" }, { id = "cn-fuzhou" local_name = "華東6(福州-本地地域)" region_id = "cn-fuzhou" }, { id = "cn-chengdu" local_name = "西南1(成都)" region_id = "cn-chengdu" }, { id = "cn-hongkong" local_name = "中國(香港)" region_id = "cn-hongkong" }, { id = "ap-northeast-1" local_name = "亞太東北 1 (東京)" region_id = "ap-northeast-1" }, { id = "ap-northeast-2" local_name = "韓國(首爾)" region_id = "ap-northeast-2" }, { id = "ap-southeast-1" local_name = "亞太東南 1 (新加坡)" region_id = "ap-southeast-1" }, { id = "ap-southeast-2" local_name = "亞太東南 2 (悉尼)" region_id = "ap-southeast-2" }, { id = "ap-southeast-3" local_name = "亞太東南 3 (吉隆坡)" region_id = "ap-southeast-3" }, { id = "ap-southeast-6" local_name = "菲律賓(馬尼拉)" region_id = "ap-southeast-6" }, { id = "ap-southeast-5" local_name = "亞太東南 5 (雅加達)" region_id = "ap-southeast-5" }, { id = "ap-southeast-7" local_name = "泰國(曼谷)" region_id = "ap-southeast-7" }, { id = "us-east-1" local_name = "美國東部 1 (弗吉尼亞)" region_id = "us-east-1" }, { id = "us-west-1" local_name = "美國西部 1 (硅谷)" region_id = "us-west-1" }, { id = "eu-west-1" local_name = "英國 (倫敦)" region_id = "eu-west-1" }, { id = "me-east-1" local_name = "中東東部 1 (迪拜)" region_id = "me-east-1" }, { id = "me-central-1" local_name = "沙特(利雅得)" region_id = "me-central-1" }, { id = "eu-central-1" local_name = "歐洲中部 1 (法蘭克福)" region_id = "eu-central-1" }, ] }
查詢實例列表
在main.tf文件增加如下內容。
data "alicloud_db_instances" "queryinstances" { }
執行以下命令。
terraform apply
在執行過程中,根據提示輸入
yes
并按下Enter鍵,等待命令執行完成,若出現以下信息,則表示運行成功。data.alicloud_db_instances.queryinstances: Reading... alicloud_vpc.main: Refreshing state... [id=vpc-****] alicloud_vswitch.main: Refreshing state... [id=vsw-****] data.alicloud_db_instances.queryinstances: Read complete after 1s [id=277****] alicloud_db_instance.instance: Refreshing state... [id=pgm-****] No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
驗證結果。
您可以使用以下命令查看結果:
terraform show
# data.alicloud_db_instances.queryinstances: data "alicloud_db_instances" "queryinstances" { enable_details = false id = "27790****" ids = [ "pgm-bp1zirc6i2****", ] instances = [ { acl = "" availability_zone = "cn-hangzhou-j" ca_type = "" charge_type = "Postpaid" client_ca_cert = "" client_ca_cert_expire_time = "" client_cert_revocation_list = "" connection_mode = "Standard" connection_string = "pgm-****.pg.rds.aliyuncs.com" create_time = "2022-09-28T06:15:32Z" creator = "" db_instance_storage_type = "cloud_essd" db_type = "Primary" delete_date = "" deletion_protection = false description = "" encryption_key = "" encryption_key_status = "" engine = "PostgreSQL" engine_version = "13.0" expire_time = "" guard_instance_id = "<nil>" id = "pgm-****" instance_storage = 50 instance_type = "pg.n2.2c.2m" key_usage = "" last_modify_status = "" master_instance_id = "<nil>" master_zone = "" material_expire_time = "" modify_status_reason = "" name = "terraformtest" net_type = "Intranet" origin = "" parameters = [] port = "5432" readonly_instance_ids = [] region_id = "cn-hangzhou" replication_acl = "" require_update = "" require_update_item = "" require_update_reason = "" server_ca_url = "" server_cert = "" server_key = "" ssl_create_time = "" ssl_enabled = "off" ssl_expire_time = "" status = "Running" temp_instance_id = "<nil>" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id_slave_a = "" zone_id_slave_b = "" }, ] names = [ "terraformtest", ] page_size = 100 total_count = 1 }
查詢實例詳情
在main.tf文件增加如下內容。
查詢指定實例:
說明已創建RDS PostgreSQL實例,詳情請參見創建RDS PostgreSQL實例。
實例狀態為運行中,您可以通過如下兩種方式查看:
data "alicloud_db_instances" "queryinstance" { ids = ["pgm-f8z04t******"] engine = "PostgreSQL" }
查詢指定地域(環境變量中設置的地域)下所有實例:
data "alicloud_db_instances" "queryinstance" { ids = [] engine = "PostgreSQL" }
執行以下命令。
terraform apply
在執行過程中,根據提示輸入
yes
并按下Enter鍵,等待命令執行完成,若出現以下信息,則表示運行成功。data.alicloud_db_instances.queryinstance: Reading... data.alicloud_db_instances.queryinstance: Read complete after 5s [id=69816****] No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
驗證結果。
您可以使用以下命令查看結果:
terraform show
# data.alicloud_db_instances.queryinstance: data "alicloud_db_instances" "queryinstance" { enable_details = false engine = "PostgreSQL" id = "277908****" ids = [ "pgm-****", ] instances = [ { acl = "" availability_zone = "cn-hangzhou-j" ca_type = "" charge_type = "Postpaid" client_ca_cert = "" client_ca_cert_expire_time = "" client_cert_revocation_list = "" connection_mode = "Standard" connection_string = "pgm-****.pg.rds.aliyuncs.com" create_time = "2022-09-28T06:15:32Z" creator = "" db_instance_storage_type = "cloud_essd" db_type = "Primary" delete_date = "" deletion_protection = false description = "" encryption_key = "" encryption_key_status = "" engine = "PostgreSQL" engine_version = "13.0" expire_time = "" guard_instance_id = "<nil>" id = "pgm-****" instance_storage = 50 instance_type = "pg.n2.2c.2m" key_usage = "" last_modify_status = "" master_instance_id = "<nil>" master_zone = "" material_expire_time = "" modify_status_reason = "" name = "terraformtest" net_type = "Intranet" origin = "" parameters = [] port = "5432" readonly_instance_ids = [] region_id = "cn-hangzhou" replication_acl = "" require_update = "" require_update_item = "" require_update_reason = "" server_ca_url = "" server_cert = "" server_key = "" ssl_create_time = "" ssl_enabled = "off" ssl_expire_time = "" status = "Running" temp_instance_id = "<nil>" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id_slave_a = "" zone_id_slave_b = "" }, ] names = [ "terraformtest", ] page_size = 100 total_count = 1 }
當您不再需要上述通過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
}
# 查詢可用區資源
data "alicloud_db_zones" "queryzones" {
instance_charge_type = "PostPaid"
engine = "PostgreSQL"
db_instance_storage_type = "cloud_essd"
}
# 詢可購買的實例規格
data "alicloud_db_instance_classes" "queryclasses" {
instance_charge_type = "PostPaid"
engine = "PostgreSQL"
db_instance_storage_type = "cloud_essd"
}
# 查詢地域信息
data "alicloud_regions" "query_regions" {
}
# 查詢實例列表
data "alicloud_db_instances" "queryinstances" {
}
# 查詢指定實例
data "alicloud_db_instances" "queryinstance" {
ids = [alicloud_db_instance.instance.id]
# 查詢指定地域(環境變量中設置的地域)下所有實例
# ids = []
engine = "PostgreSQL"
}
如果您想體驗更多完整示例,請前往更多完整示例中對應產品的文件夾查看。