Terraform is an open source tool that safely and efficiently provisions and manages cloud infrastructure and resources. This topic shows you how to use Terraform to create an ApsaraDB RDS for PostgreSQL instance.
Supported resources
For a list of ApsaraDB RDS resources and data sources that you can use with Terraform, see ApsaraDB RDS resources and data sources. If you are new to Terraform, see Introduction to Terraform.
Resource
alicloud_db_account: Manages a database account.
alicloud_db_account_privilege: Manages the permissions of a database account to access a specific database.
alicloud_db_backup_policy: Manages a database backup policy.
alicloud_db_connection: Manages a database endpoint.
alicloud_db_database: Manages a database.
alicloud_db_instance: Manages an RDS instance.
alicloud_db_read_write_splitting_connection: Manages read/write splitting for a database.
alicloud_db_readonly_instance: Manages a read-only instance.
alicloud_rds_account: Manages an instance account.
alicloud_rds_backup: Manages an instance backup.
alicloud_rds_clone_db_instance: Restores an instance.
alicloud_rds_db_instance_endpoint: Manages an endpoint for a cluster series instance.
alicloud_rds_db_instance_endpoint_address: Manages a public endpoint for a cluster series instance.
alicloud_rds_db_node: Manages a node for a cluster series instance.
alicloud_rds_db_proxy: Manages a database proxy.
alicloud_rds_ddr_instance: Manages cross-region disaster recovery.
alicloud_rds_instance_cross_backup_policy: Manages a cross-region backup policy for an instance.
alicloud_rds_parameter_group: Manages a parameter template.
alicloud_rds_service_linked_role: Manages a service-linked role (SLR).
alicloud_rds_upgrade_db_instance: Manages an instance upgrade policy.
Data Source
alicloud_db_instance_class_infos: Queries instance type information.
alicloud_db_instance_classes: Queries instance resource information.
alicloud_db_instance_engines: Queries instance engine information.
alicloud_db_instances: Queries instance information.
alicloud_db_zones: Queries zone information.
alicloud_instance_keywords: Queries reserved keyword information.
alicloud_rds_accounts: Queries account information.
alicloud_rds_backups: Queries backup information.
alicloud_rds_character_set_names: Queries supported character sets.
alicloud_rds_class_details: Queries the details of instance types.
alicloud_rds_collation_time_zones: Queries available character set collations and time zone information.
alicloud_rds_cross_region_backups: Queries cross-region backup information for an instance.
alicloud_rds_cross_regions: Queries information about zones that support cross-region backups for an instance.
alicloud_rds_modify_parameter_logs: Queries parameter modification logs.
alicloud_rds_parameter_group: Queries parameter template information.
alicloud_rds_slots: Queries replication slot information.
Configure permissions
To use Terraform, you need an Alibaba Cloud account and an AccessKey pair. For security purposes, we recommend that you do not use your main Alibaba Cloud account to access ApsaraDB RDS. Instead, create a Resource Access Management (RAM) user, obtain the AccessKey pair for the RAM user, and grant the required permissions to the RAM user.
Create a RAM user:
Go to the RAM User List page and click Create User.
Set Login Name to rds-test-operator and select Use permanent AccessKey for access for Access Mode.
Click OK to create the RAM user and then save the AccessKey ID and AccessKey secret.
Grant permissions:
Go to the RAM User List page. In the Actions column for the target RAM user, click Add Permissions.
In the text box, search for
AliyunRDSand select AliyunRDSFullAccess. This policy grants full control over RDS.In the text box, search for
VPCand select AliyunVPCFullAccess. This policy grants full control over VPC.NoteIn this example, a VPC and a vSwitch are created with the RDS instance. You can also select other permission policies or create custom policies as needed. For more information, see Create a custom permission policy.
Click OK to add the permissions.
Procedure
Install Terraform
You can use Alibaba Cloud Cloud Shell. Cloud Shell is a free product that helps you with operations and maintenance (O&M). It comes pre-installed with Terraform components and is configured with identity credentials. You can run Terraform commands directly in Cloud Shell. For more information, see Cloud Shell.
To install and configure Terraform locally, see Install and configure Terraform locally.
After the installation is complete, open a command-line terminal and enter
terraform version. If the version information is returned, Terraform is successfully installed.
Write a template
Terraform uses commands to create, modify, view, and delete the resources defined in a Terraform template.
Create and navigate to an execution directory.
NoteCreate a separate execution directory for each Terraform project.
Linux or macOS:
sudo mkdir /usr/local/terraform cd /usr/local/rds_terraformImportant
If you are not the root user, you must also grant permissions on the
rds_terraformdirectory to the current user. To do this, run thesudo chown -R <current_username>:<user_group_name> /usr/local/terraformcommand to change the owner of therds_terraformfolder to the current user.Windows: For example, create the
rds_terraformfolder on the D drive and navigate to therds_terraformfolder.
In the execution directory, create a Terraform template file named terraform.tf.
Linux or macOS:
touch terraform.tfWindows: Manually create the
terraform.tffile.
For example, to query zone information for RDS for PostgreSQL, you can edit the
terraform.tffile and add the following information.resource "alicloud_vpc" "main" { vpc_name = "alicloud" cidr_block = "172.16.0.0/16" } resource "alicloud_vswitch" "main" { vpc_id = alicloud_vpc.main.id cidr_block = "172.16.192.0/20" zone_id = "cn-hangzhou-j" depends_on = [alicloud_vpc.main] } resource "alicloud_db_instance" "instance" { engine = "PostgreSQL" engine_version = "13.0" instance_type = "pg.n2.2c.2m" instance_storage = "30" instance_charge_type = "Postpaid" vswitch_id = alicloud_vswitch.main.id }
Run the template
This example shows how to use a local installation of Terraform on a Windows operating system. The commands may vary depending on the operating system.
Navigate to the
D:\rds_terraformdirectory and initialize the modules, which include providers and other templates.terraform initValidate the template syntax.
terraform validateResponse:
Success! The configuration is valid.Preview the template.
terraform planApply the template configuration.
terraform applyAfter the following configuration information appears, confirm the configuration and enter
yesto start the creation process.If a log similar to the following one appears, the instance was successfully created.
View the result.
Go to the RDS Instances page to view the RDS instance that you created.

References
For detailed examples of how to call RDS OpenAPI operations using Terraform, see Terraform.