Useful Terraform commands you need to know🔥

Terraform Initialization Commands:

  1. Initialize Terraform: terraform init

    • Skip plugin installation: terraform init -get-plugins=false

    • Force plugin installation from a directory: terraform init -plugin-dir=PATH

    • Upgrade modules and plugins: terraform init -upgrade

  2. Backend Configuration:

    • Update backend configuration: terraform init -migrate-state -force-copy

    • Skip backend configuration: terraform init -backend=false

    • Use local backend configuration: terraform init -backend-config=FILE

    • Change state lock timeout: terraform init -lock-timeout=120s

General Commands:

  1. Terraform Version: terraform version

  2. Download/Update Root Modules: terraform get -update=true

  3. Open Interactive Terminal: terraform console

  4. Create Dependency Diagram: terraform graph | dot -Tpng > graph.png

  5. Format Code to HCL Standards: terraform fmt

  6. Validate Code Syntax: terraform validate

  7. Enable Tab Auto-completion: terraform -install-autocomplete

  8. Provider Requirements: terraform providers

  9. Login and Logout: terraform login / terraform logout

Planning Terraform:

  1. Produce a Plan: terraform plan

    • Output plan for reference: terraform plan -out current.tfplan

    • Plan for destruction: terraform plan -destroy

    • Target a specific resource: terraform plan -target=ADDRESS

Applying Terraform:

  1. Apply Current State: terraform apply

    • Use previously generated plan: terraform apply current.tfplan

    • Enable auto-approval: terraform apply -auto-approve

Managing Workspaces:

  1. List Workspaces: terraform workspace list

  2. Create New Workspace: terraform workspace new development

  3. Select Workspace: terraform workspace select default

Managing Terraform State:

  1. List Resources in State: terraform state list

  2. Import Resource into State: terraform state import ADDRESS ID

  3. Move Resource in State: terraform state mv SOURCE DESTINATION

  4. Pull State: terraform state pull > terraform.tfstate

  5. Push State: terraform state push PATH

  6. Replace Resource Provider: terraform state replace-provider A B

  7. Taint a Resource: terraform taint ADDRESS

  8. Untaint a Resource: terraform untaint ADDRESS

  9. Show Details of a Resource: terraform state show ADDRESS

Destroying Terraform:

  • Destroy Managed Resources: terraform destroy

  • Enable auto-approval: terraform destroy -auto-approve

Outputs:

  1. List Outputs: terraform output

  2. Output a Specific Value: terraform output NAME

Thank you so much for reading❤️.

Catch me up on my socials: https://t.co/M8uECaP62Z

6
Subscribe to my newsletter

Read articles from Harshal Rembhotkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshal Rembhotkar
Harshal Rembhotkar