Before you can use the template syntax of Terraform to define, preview, and deploy cloud resources, you must install Terraform and configure it.
Procedure
Install Terraform.
Download the Terraform installation package that suits your operating system from the Terraform official website.
Decompress the Terraform installation package.
For a Linux or macOS operating system, decompress the package to the
/usr/local/bin
directory. If you decompress the package to a different directory, you must configure environment variables. For more information, see How to permanently set $PATH on Linux/Unix?For a Windows operating system, decompress the package to any directory, such as
D:\terraform\
, and add the directory to the value of the path environment variable. For more information, see Where can I set path to make.exe on Windows?
Run the
terraform
command to check whether Terraform is installed.If Terraform is installed, a command output that contains available Terraform options is returned. Sample command output:
Usage: terraform [global options] <subcommand> [args] The available commands for execution are listed below. The primary workflow commands are given first, followed by less common or more advanced commands. Main commands: init Prepare your working directory for other commands validate Check whether the configuration is valid plan Show changes required by the current configuration apply Create or update infrastructure destroy Destroy previously-created infrastructure All other commands: console Try Terraform expressions at an interactive command prompt fmt Reformat your configuration in the standard style force-unlock Release a stuck lock on the current workspace get Install or upgrade remote Terraform modules graph Generate a Graphviz graph of the steps in an operation import Associate existing infrastructure with a Terraform resource login Obtain and save credentials for a remote host logout Remove locally-stored credentials for a remote host output Show output values from your root module providers Show the providers required for this configuration refresh Update the state to match remote systems show Show the current state or a saved plan state Advanced state management taint Mark a resource instance as not fully functional test Experimental support for module integration testing untaint Remove the 'tainted' state from a resource instance version Show the current Terraform version workspace Workspace management Global options (use these before the subcommand, if any): -chdir=DIR Switch to a different working directory before executing the given subcommand. -help Show this help output, or the help for a specified subcommand. -version An alias for the "version" subcommand.
For higher flexibility and security in permission management, create a Resource Access Management (RAM) user and grant the required permissions to the RAM user.
Log on to the RAM console.
Create a RAM user named Terraform and create an AccessKey pair for the user. For more information, see Create a RAM user and Obtain an AccessKey pair.
Attach the required policy to the RAM user. For this example, attach the AliyunRDSFullAccess and AliyunVPCFullAccess policies to the Terraform user. For more information, see Grant permissions to a RAM user.
Add environment variables to store authentication information.
For Linux or macOS operating systems, run the following commands:
export ALICLOUD_ACCESS_KEY="*****" export ALICLOUD_SECRET_KEY="*****" export ALICLOUD_REGION="cn-hangzhou"
NoteThe ALICLOUD_REGION environment variable specifies the region in which you want to manage your resource. The value of this environment variable must be the same as the region in which the resource resides. You can change the value based on your business requirements.
For Windows operating systems, add the ALICLOUD_ACCESS_KEY, ALICLOUD_SECRET_KEY, and ALICLOUD_REGION environment variables.
Create an execution directory and a Terraform template named terraform.tf.
Create an execution directory and access the directory.
NoteYou must create an execution directory for each Terraform project.
For Linux or macOS operating systems, run the following command:
touch terraform.tf
For Windows operating systems, create a folder such as
rdspg
in Drive D and access therdspg
folder.
In the execution directory, create a Terraform template named terraform.tf.
For Linux or macOS operating systems, run the following commands:
mkdir /usr/local/terraform cd /usr/local/terraform
For Windows operating systems, manually create the
terraform.tf
file.
For this example, edit the terraform.tf file to query zone information.
For Linux or macOS operating systems, run the following command:
vim terraform.tf
For Windows operating systems, manually edit the
terraform.tf
file.For this example, add the following information to the file:
data "alicloud_db_zones" "queryzones" { instance_charge_type= "PostPaid" engine = "PostgreSQL" db_instance_storage_type = "cloud_essd" }
NoteAfter the vim command is run, you must press Esc and enter
:wq
to save the configuration and exit.Run the terraform init command in the execution directory to initialize Terraform.
In this example, a Windows client is used, and the following result is returned:
PS D:\rdspg> terraform init Initializing the backend... Initializing provider plugins... - Reusing previous version of hashicorp/alicloud from the dependency lock file - Using previously-installed hashicorp/alicloud v1.186.0 ╷ │ Warning: Additional provider information from registry │ │ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud: │ - For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in │ required_providers. ╵ Terraform has been successfully initialized! 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.
Run the
terraform plan
command to preview the configuration.In this example, a Windows client is used, and the following result is returned:
PS D:\rdspg> terraform plan data.alicloud_db_zones.queryzones: Reading... data.alicloud_db_zones.queryzones: Read complete after 4s [id=491248936] 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.
Run the
terraform apply
command to apply the configuration.In this example, a Windows client is used, and the following result is returned:
PS D:\rdspg> terraform apply data.alicloud_db_zones.queryzones: Reading... data.alicloud_db_zones.queryzones: Read complete after 0s [id=491248936] 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 query the result.In this example, a Windows client is used, and the following result is returned:
PS D:\rdspg> terraform show # 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 = [] }, ] }