Terraform Validate
After crafting your Terraform configuration files, you can verify their syntax without running a full plan or apply. Theterraform validate command checks your configuration for errors, ensuring that your HCL (HashiCorp Configuration Language) is correct.
For example, consider the following configuration:
file_permissions instead of file_permission may lead to an error. The corrected configuration should be:
Terraform fmt
Theterraform fmt command scans your configuration files in the current directory and reformats them into a standard style. This canonical formatting greatly improves code readability and consistency. Running the command will list any files that were modified during the formatting process.
Terraform Show
Theterraform show command is used to display the current Terraform state, detailing all attributes of your managed resources. This output helps you verify the actual state of your infrastructure.
For example, if you have a local file resource, running:
-json flag:
Terraform Providers
Theterraform providers command lists all providers required by your configuration along with those used in your state. It also supports mirroring provider plugins to a specified directory.
For example:
Mirroring providers can accelerate deployments in environments with restricted internet access, ensuring that all necessary plugins are available locally.
Terraform Output Variables
Terraform output variables allow you to extract and display configuration values once your infrastructure has been applied. This is particularly useful for showing key results or passing values between modules. Consider the following configuration:Terraform Refresh
Theterraform refresh command synchronizes your Terraform state with the actual state of your infrastructure. This is particularly useful when external changes have occurred outside of Terraform.
Consider the following configuration:
-refresh-only flag with terraform apply:
-refresh=false option.
Terraform Graph
Theterraform graph command creates a DOT format representation of resource dependencies in your configuration. The graph visually outlines how resources are connected.
For example, in this configuration with dependency references:
The DOT format may be challenging to interpret directly. Use a visualization tool like Graphviz to render a graphical version of the dependency graph.
Visualizing the Graph with Graphviz
Graphviz is a widely-used tool for converting DOT files into visual images. On Ubuntu, you can install Graphviz as follows:terraform graph command into Graphviz’s dot command to create an SVG:
graph.svg file in your browser to see a visual representation of your resource dependencies. This helps in understanding how resources like the local file and random pet interact through dependency expressions.
That concludes our comprehensive look at essential Terraform commands. Use these tools to validate, format, and manage your infrastructure configurations effectively. Now, proceed to the hands-on labs to further explore and practice Terraform’s capabilities.