
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 initin 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 fmtto reformat all.tffiles in your directory.
terraform validate- Purpose: Checks the Terraform configuration for syntax errors and logical inconsistencies.
- Example: Execute
terraform validateto 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 versionto 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 planto 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 applyto 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-approvewhen 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 refreshto 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 destroyto 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-approvewhen 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 listto 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_instanceto 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_instanceto 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_nameto 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_instanceto 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_instanceto 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-1234567890abcdef0to 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 getafter 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 listto 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 productionto create a new workspace calledproduction.
terraform workspace select- Purpose: Switches the current Terraform environment to a different workspace.
- Example: Use
terraform workspace select productionto work in theproductionworkspace.
terraform workspace delete- Purpose: Deletes a workspace that is no longer needed.
- Example: Run
terraform workspace delete productionto remove theproductionworkspace.
Terraform File and Configuration Management
terraform output- Purpose: Displays the output values defined in the Terraform configuration.
- Example: Use
terraform outputto view the values of outputs like IP addresses or URLs that are configured for retrieval after anapplyoperation.
