Common commands

Last update:2025-01-14 17:48:36

Detailed explanation of common Terraform commands

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.

1. Initialize the working directory

Order

terraform init

illustrate

The terraform init command is used to initialize the current directory. This command will do the following:

  • Download and install the provider plugins defined in the Terraform configuration.
  • Initialize any referenced modules.
  • Checks whether a previously initialized state exists in the current directory.

Example

terraform init

After executing the above command, Terraform will download all providers defined in the configuration file and initialize the working directory.


2. Format the configuration file

Order

terraform fmt

illustrate

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.

Example

terraform fmt

After executing this command, all .tf files in the current directory and its subdirectories will be formatted.


3. Verify the configuration file

Order

terraform validate

illustrate

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.

Example

terraform validate

After executing this command, Terraform checks the configuration files for syntax errors and logic errors.


4. Generate and display execution plan

Order

terraform plan

illustrate

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.

Example

terraform plan

After executing this command, Terraform displays the resources that will be created, modified, or destroyed.


5. Application configuration changes

Order

terraform apply

illustrate

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.

Example

terraform apply

After executing this command, Terraform prompts the user to confirm the execution plan and applies the changes after the user confirms.


6. Destroy infrastructure

Order

terraform destroy

illustrate

The terraform destroy command is used to destroy the previously created infrastructure. It deletes all resources recorded in the current state file.

Example

terraform destroy

After executing this command, Terraform prompts the user to confirm the destruction plan and destroys all resources after the user confirms.


7. Check the current status

Order

terraform show

illustrate

The terraform show command is used to display the contents of the current Terraform state file, including the current status of all resources.

Example

terraform show

After executing this command, Terraform outputs the contents of the current state file.


8. List current resources

Order

terraform state list

illustrate

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.

Example

terraform state list

After executing this command, Terraform will list all currently managed resources.


9. Output specific values

Order

terraform output

illustrate

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.

Example

terraform output

After executing this command, Terraform displays the values of all output variables.


10. Get Module

Order

terraform get

illustrate

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.

Example

terraform get

After executing this command, Terraform downloads and updates all modules.


11. Refresh status file

Order

terraform refresh

illustrate

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.

Example

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.


Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!