Last update:2025-01-14 17:48:36
Terraform is an open source tool developed by HashiCorp for building, changing, and versioning infrastructure securely and efficiently. This article will introduce some common commands of Terraform in detail to help you better understand and use Terraform.
terraform init
The terraform init
command is used to initialize the current directory. This command will do the following:
terraform init
After executing the above command, Terraform will download all providers defined in the configuration file and initialize the working directory.
terraform fmt
The terraform fmt
command is used to format the Terraform configuration file to make it more readable. It automatically adjusts the spacing, alignment, and indentation in the file.
terraform fmt
After executing this command, all .tf
files in the current directory and its subdirectories will be formatted.
terraform validate
The terraform validate
command is used to verify whether the syntax and logic of the Terraform configuration file are correct. It does not communicate with any remote service, but only checks the configuration file itself.
terraform validate
After executing this command, Terraform checks the configuration files for syntax errors and logic errors.
terraform plan
The terraform plan
command is used to generate and display an execution plan that describes the operations that Terraform will perform on the infrastructure. This command does not actually perform any changes, but displays the changes in the plan for easy review by the user.
terraform plan
After executing this command, Terraform displays the resources that will be created, modified, or destroyed.
terraform apply
The terraform apply
command is used to apply configuration changes and actually perform operations to create, update, or destroy infrastructure. The command prompts the user to confirm the execution plan before the operation is performed.
terraform apply
After executing this command, Terraform prompts the user to confirm the execution plan and applies the changes after the user confirms.
terraform destroy
The terraform destroy
command is used to destroy the previously created infrastructure. It deletes all resources recorded in the current state file.
terraform destroy
After executing this command, Terraform prompts the user to confirm the destruction plan and destroys all resources after the user confirms.
terraform show
The terraform show
command is used to display the contents of the current Terraform state file, including the current status of all resources.
terraform show
After executing this command, Terraform outputs the contents of the current state file.
terraform state list
The terraform state list
command is used to list all resources recorded in the current state file. This command is very useful for viewing the list of currently managed resources.
terraform state list
After executing this command, Terraform will list all currently managed resources.
terraform output
The terraform output
command is used to display the output variables defined in the configuration. Output variables are often used to pass information to other systems or as part of command line output.
terraform output
After executing this command, Terraform displays the values of all output variables.
terraform get
The terraform get
command is used to download and update the modules referenced in the configuration. This command is very useful when using modules to ensure that all modules are downloaded correctly.
terraform get
After executing this command, Terraform downloads and updates all modules.
terraform refresh
The terraform refresh
command is used to refresh the Terraform state file to reflect the actual infrastructure state. This command compares it with the current infrastructure and updates the state file.
terraform refresh
After executing this command, Terraform updates the state file to match the actual infrastructure state.
With the above commands, you can efficiently manage and operate your infrastructure. Each command has more options and parameters, and you can view detailed help information through terraform -help
. I hope this article will help you and help you better master the use of Terraform.