After you install Terraform, you can use Terraform commands in the directory that contains your root module. This topic describes common commands, including terraform init, terraform plan, terraform apply, terraform destroy, and terraform fmt.
terraform init: Initializes the working directory and automatically downloads the required provider plug-in.
terraform plan: Previews the resources that Terraform will create after you run
terraform apply.terraform apply: Creates infrastructure resources.
terraform destroy: Destroys infrastructure resources.
terraform fmt: Automatically formats code to match the canonical format and style.
Init
The terraform init command is used in the initialization phase. It is the first command to run after you write your Terraform configuration. terraform init downloads and installs the Alibaba Cloud provider plug-in and other necessary files into a subdirectory of the current working directory.

The provider block in your configuration includes a source property that specifies where to download the provider plug-in from.
Terraform uses a plug-in-based architecture to support multiple infrastructure and service providers. Each provider is a separate binary file distributed independently of Terraform. The terraform init command automatically downloads and installs the binary files for any provider used in the Terraform configuration. In this example, the provider is alicloud.
When you run terraform init, Terraform displays an "Initializing provider plugins" message. This indicates that Terraform is finding the specified version of the plug-in and downloading the relevant files. After the command completes, a hidden .terraform directory is created in the current working directory to store the downloaded plug-ins.
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding aliyun/alicloud versions matching "1.225.0"...
- Reusing previous version of hashicorp/alicloud from the dependency lock file
- Installing aliyun/alicloud v1.225.0...
- Installed aliyun/alicloud v1.225.0 (verified checksum)
...
Terraform has been successfully initialized!The output of the terraform init command shows the version of the provider that Terraform installed. In this example, the version is 1.225.0.
Plan
terraform plan creates an execution plan. This plan previews the resources that Terraform will create, modify, or destroy when you run terraform apply.
When you run terraform plan, Terraform performs the following operations:
Reads the current state of any existing remote resources to ensure that the Terraform state is up-to-date.
Compares the current configuration with the prior state and notes any differences.
Proposes an execution plan that makes the necessary changes to achieve the desired state described in the configuration.
terraform plan does not create or change any infrastructure resources. Instead, it lets you preview the changes. For example, before you commit the Terraform configuration to versioning, you can run this command to ensure that its behavior is as expected.

The symbols next to the resources and parameters indicate the specific operations that will be performed:
Plus sign (+)
A plus sign next to a resource means that Terraform will create that resource and displays the properties that will be set.
Minus sign followed by a plus sign (-/+)
Indicates that Terraform will destroy the current resource and create a new one instead of updating its properties in place.
Tilde (~)
Indicates that Terraform will update the resource properties in place.
Minus sign (-)
Indicates that the current resource instance will be destroyed.
When you run the terraform plan command, you can use the optional -out=<file_name> parameter to save the generated plan to a file. You can then pass this file to terraform apply to execute the saved plan.
$ terraform plan -out=tf.tfplanApply
terraform apply executes a pre-generated plan from terraform plan, or it generates a plan and prompts you for confirmation before applying it. This command creates resources and establishes the dependencies between them.
terraform apply can execute a plan in two ways:
Execute a plan file. You can use the
-outparameter withterraform planto save the execution plan to a file, such astf.tfplan. Then, you can runterraform applywith the plan file as an argument. Terraform applies the plan without prompting for confirmation:
$ terraform apply tf.tfplanGenerate a new execution plan and wait for confirmation before execution.
Similar to the terraform plan command, terraform apply displays the details of resource changes, creates an execution plan, and waits for your confirmation. If the plan is successfully created, Terraform pauses and waits for you to confirm before proceeding. If you find anything incorrect or unsafe in the plan, you can abort the operation without changing your infrastructure.

If terraform apply fails, read the error message and resolve the issue.
Destroy
The terraform destroy command destroys resources. It is similar to terraform apply, but it behaves as if all resource definitions were removed from the configuration.
Terraform can destroy resources in three main ways:
Run the
terraform destroycommand.Similar to
terraform apply,terraform destroydisplays the details of the resources to be destroyed, creates a destruction plan, and waits for your confirmation. If the plan is created, Terraform pauses and waits for you to confirm before proceeding. If you find anything incorrect or unsafe in the plan, you can abort the operation without destroying your infrastructure.
Use
terraform planto create a destruction plan.You can use the optional
-destroyparameter withterraform planto generate a destruction plan. You can then apply this plan withterraform apply:
$ terraform plan -destroy -out=tf.tfplanTo destroy a specific resource, you can use the optional -target=resource parameter to specify the resource that you want to destroy.
Delete resource configurations.
You can delete the code for one or more resources from your Terraform configuration. When you run
terraform planandterraform apply, Terraform detects that the resources are missing and destroys them.
When destroying resources, Terraform determines the correct destruction order based on dependencies. For example, if a vSwitch contains resources such as ECS or RDS instances, Alibaba Cloud does not allow the vSwitch to be deleted. Terraform destroys the instances before it destroys the vSwitch.
Use terraform destroy with caution because it destroys all managed resources and their associated data. For example, if an OSS bucket contains data, that data cannot be recovered after you run terraform destroy.
Destroying infrastructure in a production environment is rare. However, if you use Terraform to manage multiple environments, such as developer, testing, and pre-release environments, destroying resources is a useful operation. You can use terraform destroy to easily clean up temporary environments.
fmt
Finally, let's discuss some best practices for code formatting:
Separate meta-parameters from resource parameters by placing them at the top of the resource block, separated by a blank line.
Meta-parameters are special parameters in the HashiCorp Configuration Language (HCL), such as
countandfor_each.Indent arguments two spaces within a block.
If a block has two or more arguments, align the equal signs.
If a block contains a nested block, place the nested block after all the arguments.
Separate top-level blocks from each other with a single blank line.
The terraform fmt command automatically formats the configuration files in the current directory to match the canonical style conventions. This improves readability and consistency. terraform fmt helps you maintain a consistent format without manual effort.