Learn Terraform CLI Commands
Contents
Terraform Initialization and Setup
terraform init
- Purpose: Prepares the working directory containing Terraform configuration files, downloading necessary provider plugins and setting up the backend for storing the state.
- Example: To initialize your Terraform project, run
terraform init
in the directory where your configuration files are located.
terraform fmt
- Purpose: Ensures that Terraform configuration files are formatted according to standard conventions.
- Example: Use
terraform fmt
to reformat all.tf
files in your directory.
terraform validate
- Purpose: Checks the Terraform configuration for syntax errors and logical inconsistencies.
- Example: Execute
terraform validate
to confirm that your configuration files are correctly written.
terraform version
- Purpose: Displays the version of Terraform installed, along with versions of any installed provider plugins.
- Example: Run
terraform version
to see the version of Terraform you’re currently using.
Terraform Planning and Execution
terraform plan
- Purpose: Generates an execution plan, outlining the changes Terraform will make to your infrastructure to match the desired state.
- Example: Run
terraform plan
to preview the actions Terraform will take before actually applying them.
terraform apply
- Purpose: Executes the actions required to bring your infrastructure into the desired state as defined in your configuration files.
- Example: Use
terraform apply
to create or modify infrastructure according to the plan.
terraform apply -auto-approve
- Purpose: Applies the configuration changes automatically without requiring manual approval.
- Example: Run
terraform apply -auto-approve
when you need to apply changes in automated scripts without user intervention.
terraform refresh
- Purpose: Synchronizes the state file with the actual state of the infrastructure, ensuring that Terraform has the latest information.
- Example: Run
terraform refresh
to update the state file before making further changes.
terraform destroy
- Purpose: Destroys all resources managed by Terraform, effectively reversing the apply process.
- Example: Use
terraform destroy
to clean up and remove all infrastructure defined in your configuration.
terraform destroy -auto-approve
- Purpose: Destroys resources without asking for confirmation, useful for automation scenarios.
- Example: Run
terraform destroy -auto-approve
when you want to remove resources automatically.
Terraform Statefile Management
terraform state list
- Purpose: Displays a list of all resources currently tracked by Terraform in the state file.
- Example: Use
terraform state list
to see all the infrastructure components managed by Terraform.
terraform state show
- Purpose: Provides detailed information about a specific resource in the state file.
- Example: Run
terraform state show aws_instance.my_instance
to view the properties of a specific resource.
terraform state rm
- Purpose: Removes a resource from the Terraform state file without deleting the actual resource.
- Example: Use
terraform state rm aws_instance.my_instance
to stop tracking a resource.
terraform state mv
- Purpose: Moves a resource to a different name or module within the state file.
- Example: Run
terraform state mv aws_instance.my_instance aws_instance.new_name
to rename a resource in the state.
terraform taint
- Purpose: Marks a resource as needing to be destroyed and recreated on the next
terraform apply
. - Example: Use
terraform taint aws_instance.my_instance
to force the recreation of a specific resource.
- Purpose: Marks a resource as needing to be destroyed and recreated on the next
terraform untaint
- Purpose: Removes the taint from a resource, preventing its recreation.
- Example: Run
terraform untaint aws_instance.my_instance
to cancel the taint on a resource.
terraform import
- Purpose: Brings an existing resource into Terraform management by adding it to the state file.
- Example: Use
terraform import aws_instance.my_instance i-1234567890abcdef0
to import an existing AWS instance.
Terraform Modules and Workspaces
terraform get
- Purpose: Fetches and updates the modules required for the Terraform configuration.
- Example: Run
terraform get
after adding new modules to ensure they are properly downloaded and available.
terraform workspace list
- Purpose: Lists all available workspaces within the current Terraform project.
- Example: Use
terraform workspace list
to see the different workspaces you’ve created.
terraform workspace new
- Purpose: Creates a new workspace, allowing you to manage different states for different environments.
- Example: Run
terraform workspace new production
to create a new workspace calledproduction
.
terraform workspace select
- Purpose: Switches the current Terraform environment to a different workspace.
- Example: Use
terraform workspace select production
to work in theproduction
workspace.
terraform workspace delete
- Purpose: Deletes a workspace that is no longer needed.
- Example: Run
terraform workspace delete production
to remove theproduction
workspace.
Terraform File and Configuration Management
terraform output
- Purpose: Displays the output values defined in the Terraform configuration.
- Example: Use
terraform output
to view the values of outputs like IP addresses or URLs that are configured for retrieval after anapply
operation.