本文介绍如何使用Terraform查询RDS PostgreSQL实例的相关配置。
查询可用区资源
在terraform.tf文件增加如下内容:
... data "alicloud_db_zones" "queryzones" { instance_charge_type= "PostPaid" engine = "PostgreSQL" db_instance_storage_type = "cloud_essd" }
运行
terraform apply
查询可用区资源。出现类似如下日志时,表示查询成功。
alicloud_vpc.main: Refreshing state... [id=vpc-****] data.alicloud_db_zones.queryzones: Reading... alicloud_vswitch.main: Refreshing state... [id=vsw-****] alicloud_db_instance.instance: Refreshing state... [id=pgm-****] data.alicloud_db_zones.queryzones: Read complete after 4s [id=49124****] 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 = "491248936" ids = [ "cn-hangzhou-g", "cn-hangzhou-j", "cn-hangzhou-k", ] instance_charge_type = "PostPaid" multi = false multi_zone = false zones = [ { id = "cn-hangzhou-g" multi_zone_ids = [] }, { id = "cn-hangzhou-j" multi_zone_ids = [] }, { id = "cn-hangzhou-k" multi_zone_ids = [] }, ] }
查询可购买的实例规格
在terraform.tf文件增加如下内容:
... data "alicloud_db_instance_classes" "queryclasses" { instance_charge_type= "PostPaid" engine = "PostgreSQL" db_instance_storage_type = "cloud_essd" }
运行
terraform apply
查询可购买的实例规格。出现类似如下日志时,表示查询成功。
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 = [] }, ] }, ......
查询地域信息
在terraform.tf文件增加如下内容:
data "alicloud_regions" "query_regions" { }
运行
terraform apply
查询地域信息。出现类似如下日志时,表示查询成功。
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 = "210547****" 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-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-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 = "eu-central-1" local_name = "欧洲中部 1 (法兰克福)" region_id = "eu-central-1" }, ] }
查询实例列表
在terraform.tf文件增加如下内容:
data "alicloud_db_instances" "queryinstances" { }
运行
terraform apply
查询实例列表。出现类似如下日志时,表示查询成功。
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 }
查询实例详情
在terraform.tf文件增加如下内容:
查询指定实例:
data "alicloud_db_instances" "queryinstance" { ids = ["实例ID"] engine = "PostgreSQL" }
查询指定地域(环境变量中设置的地域)下所有实例:
data "alicloud_db_instances" "queryinstance" { ids = [] engine = "PostgreSQL" }
运行
terraform apply
查询实例详情。出现类似如下日志时,表示查询成功。
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 }