You must install and configure Terraform before you can use its simple template language to define, preview, and deploy cloud infrastructure.
Procedure
- Download the appropriate software package for your operating system from the official website of Terraform.
- Decompress the package to an on-premises path, such as a /usr/local/bin. If you decompress the executable file to another directory, you must specify a global path for the file by using one of the following methods:
- For Linux operating systems, use the method described in How to permanently set $PATH on Linux/UNIX?
- For Windows operating systems, use the method described in Where can I set path to make.exe on Windows?
- For macOS operating systems, use the method described in How to permanently set $PATH on Linux/UNIX?
- Run the
terraform
command to verify the path.If a list that is similar to the following list of available Terraform options is displayed, the installation is complete:username:~$ terraform Usage: terraform [-version] [-help] <command> [args]
- For higher flexibility and security in permission management, we recommend that you create a RAM user and grant the required permissions to the RAM user.
- Log on to the Resource Access Management (RAM) console by using an Alibaba Cloud account.
- Create a RAM user named Terraform and create an AccessKey pair for the RAM user. For more information, see Create a RAM user.
- Grant the required permissions to the RAM user. In this example, the AliyunEDASFullAccess and
AliyunVPCFullAccess
permissions are granted to theTerraform
RAM user. For more information, see Grant permissions to the RAM user.
- Create a test directory. You must create a separate working directory for each Terraform project. In this example, create a test directory named terraform-test.
[user@test bin]#mkdir terraform-test
- Go to the terraform-test directory.
[user@test bin]#cd terraform-test [user@test terraform-test]#
- Create configuration files. Terraform reads all the *.tf and *.tfvars files in the directory when Terraform is running. You can write configuration information to different files based on your business needs. The following list describes the frequently used configuration files:
For example, when you create the provider.tf file, you can configure your authentication information in the following format:provider.tf -- used to configure providers terraform.tfvars -- used to configure the variables that are required to configure providers varable.tf -- used to configure universal variables resource.tf -- used to define resources data.tf -- used to define package files output.tf -- used to configure outputs
[user@test terraform-test]# vim provider.tf provider "alicloud" { region = "cn-beijing" access_key = "LTA**********NO2" secret_key = "MOk8x0*********************wwff" }
For more information about the configuration, see alicloud_edas_application.
- Initialize your working directory.
[user@test terraform-test]#terraform init Initializing provider plugins... - Checking for available provider plugins on https://releases.hashicorp.com... - Downloading plugin for provider "alicloud" (1.25.0)... The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.alicloud: version = "~> 1.25" 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.
Important After you create a working directory and configuration files for a Terraform project, you must initialize the working directory.