Terraform Installation and Command Reference
Installing Terraform
Installing on Mac
Installing on Linux
- Ubuntu installation
- CentOS installation
Verifying Installation
|
|
Terraform Commands for Resource Management
Initializing Resources
For a Terraform project, I created three basic files: main.tf (entry file), variables.tf (variable definitions), and versions.tf (version information).
|
|
Formatting Terraform Files
fmt automatically reformats .tf files in the current directory to conform to standard Terraform formatting.
|
|
Creating a Resource Plan
terraform plan checks whether the planned changes align with your expectations without modifying actual resources or state.
|
|
Creating Cloud Resources
terraform apply generates a creation plan and executes it, creating resources and generating a terraform.tfstate file in the current directory.
|
|
Viewing Created Resource Information
terraform show displays the resources created in the current project.
terraform show -json displays data in JSON format.
|
|
Marking a Resource as Tainted
terraform taint marks a resource as “tainted.” When apply is run again, the tainted resource will be destroyed first and then recreated—effectively performing a delete-and-recreate operation on that specific resource.
Conversely, terraform untaint removes the tainted status, restoring the resource to normal state.
Destroying Cloud Resources
terraform destroy destroys cloud resources based on the current configuration.
|
|
Importing Cloud Data into Local Project
terraform import imports cloud instances into local state using their instance ID. It generates a terraform.tfstate file in the local directory. Before importing existing data, back up both terraform.tfstate and the .terraform directory. After importing, use terraform show to view the resource in Terraform format, copy it, and further process it to obtain the .tf resource file content.
|
|
Generating Resource Relationship Diagrams
Each template defines resources with varying degrees of interdependencies. terraform graph generates a visual diagram of these relationships:
|
|
This output can also be exported directly as an SVG image using terraform graph | dot -Tsvg > graph.svg (requires prior installation of Graphviz: brew install graphviz).
|
|
Viewing graph.svg reveals the full resource relationship diagram:
Terraform Commands for State Management
Listing All Resources in Current State
Viewing Specific Resource Data
Removing a Specific Resource from State
terraform state rm <resource_type>.<resource_name> removes a resource from state but does not delete it from the cloud. The resource can later be re-imported from the cloud.
|
|
Refreshing Resource State
terraform refresh updates the local state by fetching the latest data from the cloud via API calls.
|
|