DEV Community

Cover image for Useful Terraform commands you need to know🔥
Harshal Rembhotkar
Harshal Rembhotkar

Posted on

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:

Terraform Version: terraform version
Download/Update Root Modules: terraform get -update=true
open Interactive Terminal: terraform console
Create Dependency Diagram: terraform graph | dot -Tpng > graph.png
Format Code to HCL Standards: terraform fmt
Validate Code Syntax: terraform validate
Enable Tab Auto-completion: terraform -install-autocomplete
Provider Requirements: terraform providers
Login and Logout: terraform login / terraform logout

Planning Terraform:

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:

Apply Current State: terraform apply
Use previously generated plan: terraform apply current.tfplan
Enable auto-approval: terraform apply -auto-approve

Managing Workspaces:

List Workspaces: terraform workspace list
Create New Workspace: terraform workspace new development
Select Workspace: terraform workspace select default

Managing Terraform State:

List Resources in State: terraform state list
Import Resource into State: terraform state import ADDRESS ID
Move Resource in State: terraform state mv SOURCE DESTINATION
Pull State: terraform state pull > terraform.tfstate
Push State: terraform state push PATH
Replace Resource Provider: terraform state replace-provider A B
Taint a Resource: terraform taint ADDRESS
Untaint a Resource: terraform untaint ADDRESS
Show Details of a Resource: terraform state show ADDRESS

Destroying Terraform:

Destroy Managed Resources: terraform destroy
Enable auto-approval: terraform destroy -auto-approve

Outputs:

List Outputs: terraform output
Output a Specific Value: terraform output NAME

Thank you so much for reading❤️.

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

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay