Less used Terraform commands

   |   3 minute read   |   Using 454 words

List of less used Terraform commands.

1. terraform console

The terraform console command launches an interactive console for experimenting with Terraform interpolations. This can be particularly useful for debugging complex expressions and modules. Within the console, you can type expressions to see how Terraform will interpret them or how it will compute them using your current state or configuration. This command is invaluable when developing new configurations or learning Terraform’s syntax and functions.

2. terraform import

Terraform is designed to manage all facets of your infrastructure, but there might be cases where resources already exist and were created outside of Terraform. The terraform import command allows you to bring these pre-existing resources into Terraform management without needing to recreate them. This command maps a real world resource to a Terraform resource in your configuration. While commonly known among seasoned users, newer users often overlook the power of this command.

3. terraform taint

Sometimes, you might need to force Terraform to recreate a resource during the next apply, perhaps to recover from a manual change or error. The terraform taint command marks a Terraform-managed resource as tainted, instructing Terraform to destroy and recreate the resource on the next apply. This command is handy for those occasions when simply applying changes doesn’t fix an issue with an existing resource.

4. terraform graph

Understanding how Terraform constructs and manages your infrastructure can sometimes be daunting due to the dependencies among resources. The terraform graph command generates a visual representation of these dependencies in the DOT format, which can be visualized using Graphviz or similar tools. This is particularly useful for large projects to understand resource relationships and troubleshoot dependency issues.

5. terraform workspace

When managing environments (such as staging, production, etc.) that require similar infrastructure with slight variations, terraform workspace provides an efficient way to handle this without duplicating code. This command allows you to create, list, and select workspaces, which store their state separately under the same configuration. Utilizing workspaces can help manage environment-specific states without clutter.

6. terraform fmt

Keeping code readable and consistent is vital for teamwork and maintenance. The terraform fmt command automatically updates configurations in the current directory for easy readability and consistency. It adjusts spacing, alignment, and other stylistic details to make your configuration conform to Terraform’s canonical format. Regularly running terraform fmt helps keep your code base clean and standardized.

7. terraform validate

While this command might not be completely under the radar, it’s often underutilized. terraform validate checks whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state. It’s a quick way to verify configurations locally before committing them, making it a crucial tool in a developer’s toolkit for catching errors early.



denis256 at denis256.dev