Modify parameters and query parameter modification logs for an ApsaraDB RDS for PostgreSQL instance using Terraform.
Run the sample code with a few clicks in Terraform Explorer.
Prerequisites
Before you begin, make sure you have:
An RDS instance in the Running state. To verify the status, check the status parameter value by following Query instance details, or log in to the ApsaraDB RDS console and check the instance status directly. To create an instance, see Create an RDS instance
A Resource Access Management (RAM) user with an AccessKey pair, rather than the root account. Following the principle of least privilege (PoLP), attach the following policy to the RAM user. For setup instructions, see Create a RAM user and Create an AccessKey pair
{ "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": "*" } ] }A Terraform environment. Choose one of the following options:
Terraform Explorer (recommended for quick testing): An online runtime environment provided by Alibaba Cloud at no additional cost. No installation required. See Terraform Explorer
Cloud Shell: Terraform is preinstalled with identity credentials already configured. Available at low cost. See Create resources with Terraform
On-premises installation: For poor network conditions or custom development environments. See Install and configure Terraform on your local machine
Resources
The following resources incur charges. Release or unsubscribe from resources you no longer need.
Resource | Description |
Creates a virtual private cloud (VPC) | |
Creates a vSwitch for the VPC | |
Creates an RDS instance |
alicloud_vpc: creates a virtual private cloud (VPC).
alicloud_vswitch: creates a vSwitch for a VPC.
alicloud_db_instance: creates an RDS instance.
Modify parameters
Some parameter changes trigger an immediate instance restart, which disconnects your application. To check whether a parameter requires a restart, see the Force Restart column on the Editable Parameters tab in the ApsaraDB RDS console. Schedule changes during off-peak hours and ensure your application reconnects automatically.
The following steps change the value of the authentication_timeout parameter from 60 to 120.
Create a working directory and a
main.tfconfiguration file inside it. Add the following configuration tomain.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" } # Create a VPC. resource "alicloud_vpc" "main" { vpc_name = "alicloud" cidr_block = "172.16.0.0/16" } # Create a vSwitch. resource "alicloud_vswitch" "main" { vpc_id = alicloud_vpc.main.id cidr_block = "172.16.192.0/20" zone_id = var.zone_id } # Create an RDS instance. # Default minor engine version: rds_postgres_1300_20240229 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 # Modify parameters. Run terraform apply again after changes. parameters { name = "authentication_timeout" value = "120" } }
Initialize Terraform:
terraform initA successful initialization returns output similar to:
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.Preview the changes:
terraform planApply the configuration:
terraform applyEnter
yeswhen prompted and press Enter. A successful apply returns output similar to:alicloud_db_instance.instance: Modifying... [id=pgm-****] alicloud_db_instance.instance: Still modifying... [id=pgm-****, 10s elapsed] ... alicloud_db_instance.instance: Still modifying... [id=pgm-****, 3m10s elapsed] alicloud_db_instance.instance: Modifications complete after 3m10s [id=pgm-****] Apply complete! Resources: 0 added, 1 changed, 0 destroyed.Verify the result.
Run the terraform show command
Run
terraform planto confirm no pending changes remain:terraform planA result of
No changes. Your infrastructure matches the configuration.confirms the parameter is applied. For a detailed state view, run:terraform showThe output includes the applied parameter value:
# 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-f8z03v853neh87k8.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-f8z03v853neh87k8" 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-f8z03v853neh87k8" 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 = "32990438" period = 0 port = "5432" private_ip_address = "172.16.206.226" replication_acl = null resource_group_id = "rg-acfmykd63gtpfpa" 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 target_minor_version = "rds_postgres_1300_20240830" tcp_connection_type = "LONG" tde_status = "Disabled" vpc_id = "vpc-f8zvtuuan0tjyas7muymk" vswitch_id = "vsw-f8zjvdbtv24927fmvrydl" whitelist_network_type = null zone_id = "cn-heyuan-a" parameters { name = "authentication_timeout" value = "120" } }Log on to the ApsaraDB RDS console
To verify in the console, log in to the ApsaraDB RDS console and check the parameter value on the instance's parameters page. In the parameter list of the Editable Parameters tab, you can see that the Running Parameter Value of the
authentication_timeoutparameter has been changed from the default value60to120.
Query parameter modification logs
Add the following configuration to
main.tf:... resource "time_static" "example" {} data "alicloud_rds_modify_parameter_logs" "querylogs" { db_instance_id = alicloud_db_instance.instance.id start_time = formatdate("YYYY-MM-DD'T'hh:mm'Z'", timeadd(time_static.example.rfc3339, "-12h")) end_time = formatdate("YYYY-MM-DD'T'hh:mm'Z'", timeadd(time_static.example.rfc3339, "12h")) }The
time_staticresource records the current time. The query window covers 12 hours before and after that timestamp.Apply the configuration:
terraform applyEnter
yeswhen prompted and press Enter. A successful apply returns output similar to:alicloud_db_instance.instance: Refreshing state... [id=pgm-****] data.alicloud_rds_modify_parameter_logs.querylogs: Reading... alicloud_db_backup_policy.instance: Refreshing state... [id=pgm-****] data.alicloud_rds_modify_parameter_logs.querylogs: Read complete after 0s [id=6335****] 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.Verify the result.
Run the terraform show command
Run
terraform showto display the parameter modification logs:terraform showThe output lists each modification with its old value, new value, and status:
# data.alicloud_rds_modify_parameter_logs.querylogs: data "alicloud_rds_modify_parameter_logs" "querylogs" { db_instance_id = "pgm-f8z03v853neh87k8" end_time = "2024-10-31T23:59Z" id = "6722ee94" logs = [ { modify_time = "1730342339000" new_parameter_value = "120" old_parameter_value = "60" parameter_name = "authentication_timeout" status = "Applied" }, ] start_time = "2024-10-31T00:00Z" }
Clean up resources
To release all resources created above, run:
terraform destroyFor details about terraform destroy, see Common Terraform commands.
Complete sample code
Run the sample code with a few clicks in Terraform Explorer.
Sample code
References
What's next
To manage ApsaraDB RDS resources using Terraform templates in Resource Orchestration Service (ROS), see Create a Terraform template