Skip to content

Instantly share code, notes, and snippets.

@pizofreude
Last active April 17, 2025 17:31
Show Gist options
  • Save pizofreude/df82d35cb02088aecd5bd1008b62f2f1 to your computer and use it in GitHub Desktop.
Save pizofreude/df82d35cb02088aecd5bd1008b62f2f1 to your computer and use it in GitHub Desktop.
All things Terraform commands

terraform init

Consider that under terraform folder, there are dev/ prod/ global/. So how many times do we need to run terraform init ?

Answer

Based on the transcript, you have the following folders under your terraform folder:

  • dev/
  • prod/
  • global/

You also have a V2 folder that represents a single Git repository, which contains subfolders for environments (dev, prod, global) and modules.

To answer your question, you would need to run terraform init in each environment folder (dev, prod, global) because each environment folder has its own Terraform state file.

However, in the V2 folder, you have a more advanced structure where each module is in its own Git repository. In this case, you would need to run terraform init in each environment folder (dev, prod, global) as well as in each module repository.

So, in total, you would need to run terraform init at least 5 times:

  • 3 times in the environment folders (dev, prod, global)
  • 2 times in the module repositories (VPC and subnet)

But, if you have more modules, the number of terraform init commands would increase accordingly.

@pizofreude
Copy link
Author

terraform fmt

Considering in each subfolders there are many terraform code artifacts, how do we ran terraform fmt?

Answer

To run terraform fmt on all the Terraform code artifacts in your subfolders, you can use the following command:

terraform fmt -recursive

This will recursively apply the terraform fmt command to all the Terraform files in your current directory and its subdirectories.

Alternatively, you can also use the following command to format only the Terraform files in the specific subfolders you mentioned:

terraform fmt -recursive dev/ prod/ global/

This will format only the Terraform files in the dev, prod, and global subfolders.

Note that the -recursive flag is optional, but it's recommended to use it to ensure that all Terraform files in the subfolders are formatted correctly.

@pizofreude
Copy link
Author

Essential Terraform Commands for Senior Data Engineers

  1. terraform init
    Description: Initializes a Terraform configuration by setting up the backend and downloading required provider plugins.
    Use Case: Run this command when starting a new Terraform project or re-initializing an existing one after changes to provider or backend configurations.
    Example:

    terraform init
  2. terraform validate
    Description: Validates the syntax and structure of Terraform configuration files.
    Use Case: Use this command to check for syntax errors before applying changes.
    Example:

    terraform validate
  3. terraform plan
    Description: Generates an execution plan, showing the resources to be added, changed, or destroyed.
    Use Case: Use this to preview changes before applying them to ensure correctness and avoid unintended resource modifications.
    Example:

    terraform plan -var-file="prod.tfvars"
  4. terraform apply
    Description: Applies the changes required to reach the desired state of the configuration.
    Use Case: Use this after reviewing the execution plan to create or modify infrastructure.
    Example:

    terraform apply -var-file="prod.tfvars"
  5. terraform destroy
    Description: Destroys all resources managed by the Terraform configuration.
    Use Case: Use this to clean up resources at the end of a project or when decommissioning infrastructure.
    Example:

    terraform destroy -var-file="prod.tfvars"
  6. terraform refresh
    Description: Updates the state file with the real-world status of resources.
    Use Case: Use this to ensure the state file is in sync with the actual infrastructure.
    Example:

    terraform refresh
  7. terraform output
    Description: Displays the output values from the state file.
    Use Case: Use this to view specific resource properties or pass outputs to other tools/scripts.
    Example:

    terraform output instance_ip
  8. terraform show
    Description: Displays the current state or a saved execution plan.
    Use Case: Use this to inspect resources in the state file or review a saved plan.
    Example:

    terraform show
  9. terraform state
    Description: A suite of subcommands for managing the Terraform state file.
    Use Case: Use this to move, remove, or inspect resources in the state file.
    Examples:

    • Move a resource:
      terraform state mv aws_instance.old_name aws_instance.new_name
    • Remove a resource:
      terraform state rm aws_instance.example
  10. terraform import
    Description: Imports existing infrastructure into Terraform's management.
    Use Case: Use this to bring manually-created or pre-existing resources under Terraform management.
    Example:

    terraform import aws_instance.my_instance i-1234567890abcdef0
  11. terraform taint
    Description: Marks a resource for recreation during the next terraform apply.
    Use Case: Use this to force the recreation of a resource after detecting a configuration drift or issue.
    Example:

    terraform taint aws_instance.example
  12. terraform untaint
    Description: Removes the taint from a resource, preventing its recreation.
    Use Case: Use this if you marked a resource as tainted but decide not to recreate it.
    Example:

    terraform untaint aws_instance.example
  13. terraform fmt
    Description: Formats Terraform configuration files to follow the standard style.
    Use Case: Use this to clean up and standardize configuration file formatting.
    Example:

    terraform fmt
  14. terraform workspace
    Description: Manages multiple workspaces (environments) in a single Terraform configuration.
    Use Case: Use this to manage environments like dev, staging, and prod.
    Examples:

    • List workspaces:
      terraform workspace list
    • Create a new workspace:
      terraform workspace new staging
  15. terraform graph
    Description: Generates a visual representation of the Terraform configuration.
    Use Case: Use this to visualize resource dependencies in the infrastructure.
    Example:

    terraform graph | dot -Tpng > graph.png
  16. terraform login
    Description: Authenticates Terraform with Terraform Cloud or Enterprise.
    Use Case: Use this to set up authentication for remote state management and collaboration.
    Example:

    terraform login
  17. terraform providers
    Description: Displays the providers used in the current configuration.
    Use Case: Use this to inspect provider dependencies and versions.
    Example:

    terraform providers
  18. terraform state pull and terraform state push
    Description: Pulls or pushes the Terraform state file from or to the remote backend.
    Use Case: Use pull to debug the latest state, and push to overwrite the state in the backend.
    Examples:

    • Pull state:
      terraform state pull > state.tfstate
    • Push state:
      terraform state push state.tfstate
  19. terraform debug
    Description: Enables detailed logging for troubleshooting Terraform issues.
    Use Case: Use this to diagnose errors in Terraform runs and configurations.
    Example:

    TF_LOG=DEBUG terraform apply
  20. terraform force-unlock
    Description: Manually removes a lock on the state file.
    Use Case: Use this to unlock the state file if a Terraform run is interrupted or stuck.
    Example:

    terraform force-unlock <LOCK_ID>

These commands cover the most critical aspects of Terraform usage for managing infrastructure, debugging issues, and maintaining best practices.

@pizofreude
Copy link
Author

pizofreude commented Apr 17, 2025

Common Terraform Execution Steps

  1. terraform init:
    • Initializes & configures the backend, installs plugins/providers, & checks out an existing configuration from a version control
  2. terraform plan:
    • Matches/previews local changes against a remote state, and proposes an Execution Plan.
  3. terraform apply:
    • Asks for approval to the proposed plan, and applies changes to cloud
  4. terraform destroy
    • Removes your stack from the Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment