This topic describes how to use Terraform to query configurations in ApsaraDB RDS for PostgreSQL.
Query available zones
In the terraform.tf file, add the following content:
... data "alicloud_db_zones" "queryzones" { instance_charge_type= "PostPaid" engine = "PostgreSQL" db_instance_storage_type = "cloud_essd" }
Run the
terraform apply
command to query available zones.If the following logs appear, the query is successful:
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.
Run the
terraform show
command to view the query result.# 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-h", "cn-hangzhou-i", "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-h" multi_zone_ids = [] }, { id = "cn-hangzhou-i" multi_zone_ids = [] }, { id = "cn-hangzhou-j" multi_zone_ids = [] }, { id = "cn-hangzhou-k" multi_zone_ids = [] }, ] }
Query available instance types
In the terraform.tf file, add the following content:
... data "alicloud_db_instance_classes" "queryclasses" { instance_charge_type= "PostPaid" engine = "PostgreSQL" db_instance_storage_type = "cloud_essd" }
Run the
terraform apply
command to query available instance types.If the following logs appear, the query is successful:
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.
Run the
terraform show
command to view the result.{ 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 = [] }, ] }, ......
Query regions
In the terraform.tf file, add the following content:
data "alicloud_regions" "query_regions" { }
Run the
terraform apply
command to query regions.If the following logs appear, the query is successful:
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.
Run the
terraform show
command to view the result.# 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-south-1", "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 = "China (Qingdao)" region_id = "cn-qingdao" }, { id = "cn-beijing" =local_name = "China (Beijing)" region_id = "cn-beijing" }, { id = "cn-zhangjiakou" local_name = "China (Zhangjiakou)" region_id = "cn-zhangjiakou" }, { id = "cn-huhehaote" local_name = "China (Hohhot)" region_id = "cn-huhehaote" }, { id = "cn-wulanchabu" local_name = "China (Ulanqab)" region_id = "cn-wulanchabu" }, { id = "cn-hangzhou" local_name = "China (Hangzhou)" region_id = "cn-hangzhou" }, { id = "cn-shanghai" local_name = "China (Shanghai)" region_id = "cn-shanghai" }, { id = "cn-shenzhen" local_name = "China (Shenzhen)" region_id = "cn-shenzhen" }, { id = "cn-heyuan" local_name = "China (Heyuan)" region_id = "cn-heyuan" }, { id = "cn-guangzhou" local_name = "China (Guangzhou)" region_id = "cn-guangzhou" }, { id = "cn-chengdu" local_name = "China (Chengdu)" region_id = "cn-chengdu" }, { id = "cn-hongkong" local_name = "China (Hong Kong)" region_id = "cn-hongkong" }, { id = "ap-northeast-1" "localName": "Japan (Tokyo)" region_id = "ap-northeast-1" }, { id = "ap-northeast-2" local_name = "South Korea (Seoul)" region_id = "ap-northeast-2" }, { id = "ap-southeast-1" "localName": "Singapore" region_id = "ap-southeast-1" }, { id = "ap-southeast-2" "localName": "Australia (Sydney)" region_id = "ap-southeast-2" }, { id = "ap-southeast-3" "localName": "Malaysia (Kuala Lumpur)" region_id = "ap-southeast-3" }, { id = "ap-southeast-6" "LocalName" : "Philippines (Manila)" region_id = "ap-southeast-6" }, { id = "ap-southeast-5" "localName": "Indonesia (Jakarta)" region_id = "ap-southeast-5" }, { id = "ap-south-1" "localName": "India (Mumbai)" region_id = "ap-south-1" }, { id = "ap-southeast-7" local_name = "Thailand (Bangkok)" region_id = "ap-southeast-7" }, { id = "us-east-1" "localName": "US (Virginia)" region_id = "us-east-1" }, { id = "us-west-1" "localName": "US (Silicon Valley)" region_id = "us-west-1" }, { id = "eu-west-1" local_name = "UK (London)" region_id = "eu-west-1" }, { id = "me-east-1" "localName": "UAE (Dubai)" region_id = "me-east-1" }, { id = "eu-central-1" "localName": "Germany (Frankfurt)" region_id = "eu-central-1" }, ] }
Query RDS instances
In the terraform.tf file, add the following content:
data "alicloud_db_instances" "queryinstances" { }
Run the
terraform apply
command to query RDS instances.If the following logs appear, the query is successful:
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.
Run the
terraform show
command to view the result.# data.alicloud_db_instances.queryinstances: data "alicloud_db_instances" "queryinstances" { enable_details = false id = "27790****" ids = [ "pgm-bp1zirc6i2****", ] instances = [ { acl = "" availability_zone = "cn-hangzhou-h" 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 }
Query the details of an RDS instance
In the terraform.tf file, add the following content:
Query a specified RDS instance.
data "alicloud_db_instances" "queryinstance" { ids = ["Instance ID"] engine = "PostgreSQL" }
Query all instances in a region that is specified by the environment variable.
data "alicloud_db_instances" "queryinstance" { ids = [] engine = "PostgreSQL" }
Run the
terraform apply
command to query the details of an RDS instance.If the following logs appear, the query is successful:
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.
Run the
terraform show
command to view the result.# 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-h" 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 }