This topic describes how to use Terraform to modify parameters and query parameter modification logs of an ApsaraDB RDS for PostgreSQL instance.
You can run the sample code in this topic with a few clicks. For more information, visit Terraform Explorer.
Prerequisites
An RDS instance is created. For more information, see Create an RDS instance.
The RDS instance is in the Running state. You can use one of the following methods to check whether the RDS instance is in the Running state:
Check the value of the status parameter by following the instructions provided in Query instance details. If the value is Running, the RDS instance is in the Running state.
Log on to the ApsaraDB RDS console, switch to the required region, find the RDS instance, and then check the instance status.
An Alibaba Cloud account has all permissions on resources within the account. If an Alibaba Cloud account is leaked, the resources are exposed to major risks. We recommend that you use a Resource Access Management (RAM) user and create an AccessKey pair for the RAM user. For more information, see Create a RAM user and Create an AccessKey pair.
You must use RAM to manage access permissions on cloud resources in an efficient manner. This helps meet the requirements for multi-user collaboration and allows you to grant permissions to users based on the principle of least privilege (PoLP) to prevent security vulnerabilities caused by excessive permissions. For more information, see Grant permissions to RAM users.
{ "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": "*" } ] }Prepare the Terraform environment. You can use one of the following methods to use Terraform:
Use Terraform in Terraform Explorer: Alibaba Cloud provides Terraform Explorer, an online runtime environment for Terraform. You can use Terraform after you log on to Terraform Explorer without the need to install Terraform. For more information, see Explorer. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at no additional cost.
Use Terraform in Cloud Shell: Terraform is preinstalled in Cloud Shell and identity credentials are configured. You can directly run Terraform commands in Cloud Shell. For more information, see Use Terraform in Cloud Shell. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at low cost.
Install and configure Terraform on your on-premises machine: This method is suitable for scenarios in which network conditions are poor or a custom development environment is used. For more information, see Install and configure Terraform in the local PC.
Resources
You are charged for specific resources. If you no longer require the resources, you must release or unsubscribe from the resources at the earliest opportunity.
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
The modification of some parameters triggers a restart of your RDS instance. After you modify the parameters, your RDS instance immediately restarts. To check whether the modification of a parameter triggers a restart, you need to log on to the ApsaraDB RDS console, go to the Editable Parameters tab, and then view the value in the Force Restart column of the parameter. If the value is Yes, the modification of the parameter triggers a restart. If the value is No, the modification of the parameter does not trigger a restart. If the RDS instance restarts, your application is disconnected from the RDS instance. Make sure that your application is configured to reconnect to your RDS instance. To minimize the impacts, we recommend that you arrange your business before the restart of the RDS instance. Proceed with caution.
This section describes how to change the value of the authentication_timeout parameter to 120.
Create a working directory and a configuration file named main.tf in the directory. Copy the following code to the main.tf configuration file:
Create the required resources.
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 with the default minor engine version of 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 }In the main.tf file, add the
parametersfield to theresource "alicloud_db_instance" "instance" {}configuration item and configure the field based on the following code snippet:... resource "alicloud_db_instance" "instance" { ... parameters { name = "authentication_timeout" value = "120" } }
Run the following command to initialize the runtime environment for Terraform:
terraform initIf the following information is returned, Terraform is initialized.
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.Create an execution plan and preview the changes.
terraform planRun the following command to create the resources:
terraform applyDuring the execution, enter
yesas prompted and press the Enter key. Wait until the command is successfully executed. If the following information appears, the operation is successful: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 the following command to check whether the parameter is modified:
terraform show# 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
Log on to the ApsaraDB RDS console to check whether the parameter is modified.
Query parameter modification logs
In the main.tf file, add the
data "alicloud_rds_modify_parameter_logs" "querylogs"configuration item and configure the configuration item based on the following snippet:... 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")) }Run the following command:
terraform applyDuring the execution, enter
yesas prompted and press the Enter key. Wait until the command is successfully executed. If the following information appears, the operation is successful: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 the following command to view parameter modification logs:
terraform show# 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" }
Clear resources
If you no longer require the preceding resources that are created or managed by using Terraform, run the following command to release the resources. For more information about the terraform destroy command, see Common commands.
terraform destroyComplete sample code
You can run the sample code in this topic with a few clicks. For more information, visit Terraform Explorer.
Sample code
If you want to experience more examples, go to the quickstarts page and view the examples in the folder of the corresponding service.
References
For more information about Terraform, see What is Terraform?
Terraform is available as a managed service in Resource Orchestration Service (ROS). You can create Terraform templates to define ApsaraDB RDS resources, specify resource parameters, and configure dependency relationships for resources. For more information, see Create a Terraform template.