通过 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
完整示例
当前示例代码支持一键运行,您可以直接运行代码。一键运行
示例代码
如果您想体验更多完整示例,请前往更多完整示例中对应产品的文件夹查看。