Terraform
Introduction
Basic Cmd

Basic Terraform Commands

  1. Initialize a Terraform Configuration

    terraform init
    • Downloads provider plugins and initializes the working directory.
  2. Validate Terraform Configuration

    terraform validate
    • Validates the syntax and configuration of your Terraform files.
  3. Format Terraform Configuration

    terraform fmt
    • Formats Terraform configuration files to a canonical format.
  4. Show the Current State

    terraform show
    • Displays the current state or a state file.
  5. Generate an Execution Plan

    terraform plan
    • Shows what actions Terraform will take based on the current configuration.
  6. Apply the Configuration

    terraform apply
    • Applies the changes required to reach the desired state of the configuration.
  7. Destroy Infrastructure

    terraform destroy
    terraform destroy -target aws_instance.myec2 # destroy a specific resource
     
    • Destroys all resources managed by the Terraform configuration.
  8. Refresh the State

    terraform refresh
    • Updates the state file with the latest information from the provider.
  9. Output Information from State

    terraform output
    • Displays the values of output variables defined in the configuration.
  10. Get Provider Plugins

    terraform get
    • Downloads provider plugins specified in the configuration.

Intermediate Terraform Commands

  1. Upgrade Providers

    terraform providers lock -upgrade
    • Upgrades provider versions to match constraints in the configuration.
  2. List All Resources

    terraform state list
    • Lists all resources in the state file.
  3. Show Detailed Information About a Resource

    terraform state show <resource_address>
    • Shows detailed state information for a specific resource.
  4. Remove Resource from State

    terraform state rm <resource_address>
    • Removes a resource from the Terraform state without destroying it.
  5. Import Existing Infrastructure

    terraform import <resource_address> <resource_id>
    • Imports existing infrastructure into Terraform management.
  6. Create a Plan File

    terraform plan -out=<plan_file>
    • Saves the execution plan to a file for review or application later.
  7. Apply a Specific Plan File

    terraform apply <plan_file>
    • Applies the changes described in a saved plan file.
  8. View the Resource Graph

    terraform graph
    • Outputs a visual representation of dependencies among resources in DOT format.
  9. List Available Providers

    terraform providers
    • Lists all providers used in the configuration.
  10. Workspace Management

    • Create a New Workspace:
      terraform workspace new <workspace_name>
    • Select a Workspace:
      terraform workspace select <workspace_name>
    • List Workspaces:
      terraform workspace list
    • Delete a Workspace:
      terraform workspace delete <workspace_name>
  11. Configure Backend

    terraform init -backend-config="path/to/backend-config"
    • Configures the backend settings for state storage.

Tips

  • Use Aliases for Providers: Manage multiple provider versions by defining aliases in your configuration.
  • Remote State Management: Consider using remote state storage (e.g., S3 with DynamoDB locking) for collaborative environments.
  • Version Control: Always use version control for your Terraform configuration files and state files.

These commands should cover the majority of your needs for managing infrastructure with Terraform, from initial setup to advanced state management. 🚀


🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!