Deploy PostgreSQL HA Instance

Application Scenario

Huawei Cloud Relational Database Service (RDS) PostgreSQL HA instance functionality provides highly available, high-performance PostgreSQL database services, supporting primary-standby architecture, automatic failover, read-write separation, and other enterprise-level functions. By configuring PostgreSQL HA instances, you can build highly available database clusters, meeting strict requirements for data security and service continuity in production environments.

This best practice is particularly suitable for scenarios that require highly available database services, implementing data redundancy backup, building enterprise application backends, such as production system databases, critical business applications, data warehouses, etc. This best practice will introduce how to use Terraform to automatically deploy RDS PostgreSQL HA instances, including VPC network, security group, RDS instance, PostgreSQL account, database, schema, and backup creation, implementing a complete PostgreSQL high-availability database solution.

This best practice involves the following main resources and data sources:

Data Sources

Resources

Resource/Data Source Dependencies

Operation Steps

1. Script Preparation

Prepare the TF file (e.g., main.tf) in the specified workspace for writing the current best practice script, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. Refer to the "Preparation Before Deploying Huawei Cloud Resources" document for configuration introduction.

2. Create VPC Resource

Add the following script to the TF file to instruct Terraform to create a VPC resource:

Parameter Description:

  • name: VPC name, assigned by referencing the input variable vpc_name

  • cidr: VPC CIDR block, assigned by referencing the input variable vpc_cidr, default value is "192.168.0.0/16"

3. Query Availability Zone Information

Add the following script to the TF file to instruct Terraform to query availability zone information:

Parameter Description:

  • count: Query availability zone information when availability_zones variable is empty, otherwise do not query

  • availability_zones: Availability zone list, assigned by referencing the input variable availability_zones, supports validation rules to ensure HA mode requires multiple availability zones

4. Create VPC Subnet

Add the following script to the TF file to instruct Terraform to create a VPC subnet resource:

Parameter Description:

  • vpc_id: VPC ID, referencing the ID of the previously created VPC resource

  • name: Subnet name, assigned by referencing the input variable subnet_name

  • cidr: Subnet CIDR block, prioritizes using subnet_cidr variable, automatically calculated if empty

  • gateway_ip: Gateway IP address, prioritizes using gateway_ip variable, automatically calculated if empty

  • availability_zone: Availability zone, prioritizes using the first element of availability_zones variable, otherwise uses the first queried availability zone

5. Query RDS Flavor Information

Add the following script to the TF file to instruct Terraform to query RDS flavor information:

Parameter Description:

  • count: Query RDS flavor information when instance_flavor_id variable is empty, otherwise do not query

  • db_type: Database engine type, assigned by referencing the input variable instance_db_type, default value is "PostgreSQL"

  • db_version: Database engine version, assigned by referencing the input variable instance_db_version, default value is "16"

  • instance_mode: Instance mode, assigned by referencing the input variable instance_mode, default value is "ha" (HA mode)

  • group_type: Flavor group type, assigned by referencing the input variable instance_flavor_group_type, default value is "general"

  • vcpus: Number of CPU cores, assigned by referencing the input variable instance_flavor_vcpus, default value is 4

  • memory: Memory size, assigned by referencing the input variable instance_flavor_memory, default value is 8

  • availability_zone: Availability zone, prioritizes using the first element of availability_zones variable, otherwise uses the first queried availability zone

6. Create Security Group

Add the following script to the TF file to instruct Terraform to create a security group resource:

Parameter Description:

  • name: Security group name, assigned by referencing the input variable security_group_name

  • delete_default_rules: Whether to delete default rules, set to true to delete default security group rules

7. Create Security Group Rules

Add the following script to the TF file to instruct Terraform to create security group rule resources:

Parameter Description:

  • security_group_id: Security group ID, referencing the ID of the previously created security group resource

  • direction: Rule direction, set to "ingress" for inbound rules

  • ethertype: Ethernet type, set to "IPv4"

  • remote_ip_prefix: Remote IP prefix, assigned by referencing the input variable vpc_cidr, allows access within VPC

  • ports: Port number, assigned by referencing the input variable instance_db_port, default value is 5432 (PostgreSQL default port)

  • protocol: Protocol type, set to "tcp"

8. Create Random Password

Add the following script to the TF file to instruct Terraform to create a random password resource:

Parameter Description:

  • count: Create random password when instance_password variable is empty, otherwise do not create

  • length: Password length, set to 12 characters

  • special: Whether to include special characters, set to true

  • override_special: Special character set, set to "!@%^*-_=+"

9. Create RDS Instance

Add the following script to the TF file to instruct Terraform to create an RDS instance resource:

Parameter Description:

  • name: RDS instance name, assigned by referencing the input variable instance_name

  • flavor: Instance flavor, prioritizes using instance_flavor_id variable, uses the first queried flavor if empty

  • vpc_id: VPC ID, referencing the ID of the previously created VPC resource

  • subnet_id: Subnet ID, referencing the ID of the previously created subnet resource

  • security_group_id: Security group ID, referencing the ID of the previously created security group resource

  • availability_zone: Availability zone list, HA mode requires multiple availability zones, single mode only needs one availability zone

  • ha_replication_mode: HA replication mode, assigned by referencing the input variable ha_replication_mode, default value is "async"

  • db: Database configuration block, includes database type, version, port, and password

  • volume: Storage configuration block, includes storage type and size

  • backup_strategy: Backup strategy configuration block, includes backup time window and retention days

  • lifecycle: Lifecycle configuration, ignores changes to flavor and availability_zone

10. Create RDS PostgreSQL Account

Add the following script to the TF file to instruct Terraform to create an RDS PostgreSQL account resource:

Parameter Description:

  • instance_id: RDS instance ID, referencing the ID of the previously created RDS instance resource

  • name: Account name, assigned by referencing the input variable account_name

  • password: Account password, prioritizes using account_password variable, uses randomly generated password if empty

11. Create RDS PostgreSQL Account Privileges

Add the following script to the TF file to instruct Terraform to create an RDS PostgreSQL account privileges resource:

Parameter Description:

  • instance_id: RDS instance ID, referencing the ID of the previously created RDS instance resource

  • user_name: Username, referencing the name of the previously created RDS PostgreSQL account resource

  • role_privileges: Role privileges list, set to ["CREATEROLE", "CREATEDB", "LOGIN", "REPLICATION"]

  • system_role_privileges: System role privileges list, set to ["pg_signal_backend"]

12. Create RDS PostgreSQL Database

Add the following script to the TF file to instruct Terraform to create an RDS PostgreSQL database resource:

Parameter Description:

  • instance_id: RDS instance ID, referencing the ID of the previously created RDS instance resource

  • name: Database name, assigned by referencing the input variable database_name

13. Create RDS PostgreSQL Schema

Add the following script to the TF file to instruct Terraform to create an RDS PostgreSQL schema resource:

Parameter Description:

  • instance_id: RDS instance ID, referencing the ID of the previously created RDS instance resource

  • db_name: Database name, referencing the name of the previously created RDS PostgreSQL database resource

  • owner: Schema owner, referencing the name of the previously created RDS PostgreSQL account resource

  • schema_name: Schema name, assigned by referencing the input variable schema_name

14. Create RDS Backup

Add the following script to the TF file to instruct Terraform to create an RDS backup resource:

Parameter Description:

  • instance_id: RDS instance ID, referencing the ID of the previously created RDS instance resource

  • name: Backup name, assigned by referencing the input variable backup_name

  • depends_on: Explicit dependency relationship, ensures schema is configured before backup creation

15. Preset Input Parameters Required for Resource Deployment (Optional)

In this practice, some resources and data sources use input variables to assign values to configuration content. These input parameters need to be manually entered during subsequent deployments. At the same time, Terraform provides a method to preset these configurations through .tfvars files, which can avoid repeated input during each execution.

Create a terraform.tfvars file in the working directory with the following example content:

Usage:

  1. Save the above content as terraform.tfvars file in the working directory (this file name allows users to automatically import the content of this tfvars file when executing terraform commands; for other names, .auto needs to be added before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values as needed

  3. When executing terraform plan or terraform apply, Terraform will automatically read the variable values from this file

In addition to using terraform.tfvars file, variable values can also be set in the following ways:

  1. Command line parameters: terraform apply -var="vpc_name=my-vpc" -var="instance_name=my-instance"

  2. Environment variables: export TF_VAR_vpc_name=my-vpc

  3. Custom named variable files: terraform apply -var-file="custom.tfvars"

Note: If the same variable is set in multiple ways, Terraform will use the variable value according to the following priority: command line parameters > variable files > environment variables > default values.

16. Initialize and Apply Terraform Configuration

After completing the above script configuration, execute the following steps to create resources:

  1. Run terraform init to initialize the environment

  2. Run terraform plan to view the resource creation plan

  3. After confirming the resource plan is correct, run terraform apply to start creating PostgreSQL HA instances

  4. Run terraform show to view the created PostgreSQL HA instance details

Reference Information

Last updated