Mastering Terraform: A Comprehensive Guide to 100 Essential Commands🚀

Mastering Terraform: A Comprehensive Guide to 100 Essential Commands🚀

# 1. Initialize Terraform working directory
terraform init

# 2. Generate execution plan
terraform plan

# 3. Apply changes to infrastructure
terraform apply

# 4. Destroy Terraform-managed infrastructure
terraform destroy

# 5. Validate configuration
terraform validate

# 6. Reformat configuration files
terraform fmt

# 7. Create a new Terraform workspace
terraform workspace new

# 8. Select an existing Terraform workspace
terraform workspace select

# 9. List all Terraform workspaces
terraform workspace list

# 10. Show current Terraform workspace
terraform workspace show

# 11. Show outputs from Terraform state
terraform output

# 12. Import existing infrastructure into Terraform
terraform import

# 13. Generate visual representation of Terraform resources
terraform graph

# 14. List all resources in the Terraform state
terraform state list

# 15. Show details about a specific resource in the Terraform state
terraform state show

# 16. Pull the current state from the state storage
terraform state pull

# 17. Push the local state to the remote state storage
terraform state push

# 18. Update the state file against real resources
terraform state refresh

# 19. Mark a resource for recreation
terraform taint

# 20. Remove the tainted state from a resource
terraform untaint

# 21. Release a stuck lock on the state
terraform force-unlock

# 22. Show the current Terraform version
terraform version

# 23. Open an interactive console
terraform console

# 24. Obtain and save credentials for Terraform Cloud
terraform login

# 25. Remove locally saved credentials for Terraform Cloud
terraform logout

# 26. Show the providers used in the configuration
terraform providers

# 27. Update the state file against real resources
terraform refresh

# 28. Remove a resource from the state
terraform state rm

# 29. Move an item in the state
terraform state mv

# 30. Apply changes to a specific resource
terraform apply -target=resource_type.name

# 31. Destroy a specific resource
terraform destroy -target=resource_type.name

# 32. Upgrade modules and plugins to the latest versions
terraform init -upgrade

# 33. Generate a plan using variables from a file
terraform plan -var-file="filename.tfvars"

# 34. Apply changes with a specific variable set inline
terraform apply -var 'key=value'

# 35. Destroy resources without requiring confirmation
terraform destroy -auto-approve

# 36. Generate a plan without acquiring a state lock
terraform plan -lock=false

# 37. Show outputs in JSON format
terraform output -json

# 38. List resources in a specific state file
terraform state list -state=custom.tfstate

# 39. Show details of a resource in a specific state file
terraform state show -state=custom.tfstate

# 40. Move an item in a specific state file
terraform state mv -state=custom.tfstate

# 41. Validate configuration and output results in JSON format
terraform validate -json

# 42. Reformat all configuration files recursively
terraform fmt -recursive

# 43. Import an existing AWS instance into Terraform
terraform import aws_instance.example i-abcd1234

# 44. Lock provider versions to current configurations
terraform providers lock

# 45. Unlock provider versions to allow updates
terraform providers unlock

# 46. Mirror provider plugins locally for a specific configuration
terraform providers mirror

# 47. Show provider schema in JSON format
terraform providers schema -json

# 48. Generate and save a resource graph as SVG
terraform graph | dot -Tsvg > graph.svg

# 49. Refresh state without asking for input
terraform refresh -input=false

# 50. Refresh a specific resource
terraform refresh -target=resource_type.name

# 51. Export state to a local file
terraform state pull > terraform.tfstate

# 52. Push a local state file to remote storage
terraform state push terraform.tfstate

# 53. Replace provider for resources in the state
terraform state replace-provider

# 54. Generate a plan with complex variables
terraform plan -var 'subnet_ids=["subnet-12345678"]'

# 55. Show the current state without colorized output
terraform show -no-color

# 56. Open a console with specific variables defined
terraform console -var 'region=us-west-2'

# 57. Import an AWS instance into Terraform state
terraform import aws_instance.example i-abcd1234

# 58. Initialize without asking for input
terraform init -input=false

# 59. Generate a plan with compact warning messages
terraform plan -compact-warnings

# 60. Rename a resource in the state
terraform state mv aws_instance.example aws_instance.new_name

# 61. Remove a resource from the state
terraform state rm aws_instance.example

# 62. Delete a Terraform workspace
terraform workspace delete

# 63. Select the default Terraform workspace
terraform workspace select default

# 64. Refresh state with a specific lock timeout
terraform refresh -lock-timeout=5m

# 65. Generate a plan without refreshing state
terraform plan -refresh=false

# 66. Generate a plan with detailed exit codes
terraform plan -detailed-exitcode

# 67. Import a module into Terraform state
terraform import module.vpc_module vpc-12345678

# 68. Destroy resources without refreshing state
terraform destroy -refresh=false

# 69. Show outputs from a specific state file
terraform output -state=custom.tfstate

# 70. Show resource names using `jq` JSON processor
terraform show -json | jq .resources[].name

# 71. Pull state without acquiring a state lock
terraform state pull -lock=false

# 72. List AWS instances in the state
terraform state list | grep aws_instance

# 73. Import an AWS instance using specific IDs
terraform import aws_instance.example i-abcd1234

# 74. Destroy resources within a specific module
terraform destroy -target=module.network

# 75. Open a console with variables from a file
terraform console -var-file="variables.tfvars"

# 76. Import with a custom configuration file
terraform import -config=custom_config.tf aws_instance.example i-abcd1234

# 77. Create a new workspace named "dev"
terraform workspace new dev

# 78. Apply changes without asking for input
terraform apply -input=false

# 79. Show details of resources within a module
terraform state show module.vpc_module

# 80. Generate a plan and save it to a plan file
terraform plan -out=tfplan

# 81. Apply changes using a pre-generated plan file
terraform apply tfplan

# 82. Destroy resources using variables from a file
terraform destroy -var-file="terraform.tfvars"

# 83. Open a console with a specific state
terraform console -state=custom.tfstate

# 84. Lock provider versions for a specific platform
terraform providers lock -platform=darwin_amd64

# 85. Unlock provider versions for a specific platform
terraform providers unlock -platform=darwin_amd64

# 86. Mirror providers from a registry to a local mirror
terraform providers mirror -from=registry.terraform.io -to=local_mirror

# 87. Show the schema for the AWS provider in JSON format
terraform providers schema -provider=aws -json

# 88. Show outputs specific to a named module
terraform output -module=module_name

# 89. Show resources up to a specified depth of module nesting
terraform show -module-depth=2

# 90. Refresh state with specific variable set inline
terraform refresh -var 'key=value'

# 91. Move an item in the state with a backup file created
terraform state mv -backup=path/to/backup

# 92. Import infrastructure with a specific configuration file
terraform import -config=path/to/config

# 93. Pull state and save it to a custom state file
terraform state pull -state-out=custom.tfstate

# 94. List resources and save state to a custom state file
terraform state list -state-out=custom.tfstate

# 95. Import infrastructure with a custom configuration file and specific IDs
terraform import -config=path/to/config aws_instance.example i-abcd1234

# 96. Destroy resources within a specific module without requiring approval
terraform destroy -target=module.network -auto-approve

# 97. Remove a resource from the state and save the updated state to a custom file
terraform state rm -state-out=custom.tfstate aws_instance.example

# 98. Delete a specific Terraform workspace named "dev"
terraform workspace delete dev

# 99. Select a Terraform workspace named "dev" using the `-name` flag
terraform workspace select -name=dev

# 100. List all Terraform workspaces without colorized output
terraform workspace list -no-color
        
Anshul Ganvir

Cloud/DevOps Engineer | Cloud Architect | GCP | AWS | Azure | Automation | Ansible | CI/CD | Terraform | Docker | Kubernetes

3mo

Very useful

To view or add a comment, sign in

Explore topics