Terraform Validate
Theterraform validate command checks whether your configuration syntax is correct and internally consistent. When your configuration is valid, you’ll receive a successful validation message. For example, a valid configuration like the one below:
file_permissions instead of the correct file_permission generates an error message:
Always review the error messages carefully—Terraform’s hints help you quickly identify and fix configuration mistakes.
Terraform fmt
Theterraform fmt command scans all configuration files in the current directory and reformats them into a canonical format. This improves readability and consistency across your files. When you run this command, any files that are modified will be displayed in the output.
Terraform Show
Theterraform show command displays the current state of your managed infrastructure. This includes a detailed view of resources and their attributes. To facilitate further processing, you can append the --json flag, which outputs the state in JSON format for easier integration with other tools.
Terraform Providers
Theterraform providers command lists all providers declared in your configuration. For example, if your configuration uses only AWS resources, the command produces an output similar to:
Terraform Output
Theterraform output command retrieves the output variables defined in your configuration. You can display either all outputs or a specific one by providing the output name. Consider the example below:
Terraform Refresh
Terraform automatically refreshes the state during commands liketerraform plan and terraform apply to capture any real-world changes. The terraform refresh command explicitly syncs the state file with the actual infrastructure. For example, if a resource has been manually updated, refreshing the state will capture the latest changes:
Terraform Graph
Theterraform graph command generates a visual representation of resource dependencies in your configuration or execution plan. The output is produced in DOT format and can be visualized with Graphviz. For users who prefer a visual perspective, this helps in understanding complex dependency relationships within your infrastructure.
Terraform State Commands
Terraform includes several commands to manage the state file—allowing you to list, view, move, pull, and remove state entries. It is important to use these commands instead of manually modifying the state file.List Resources
Use theterraform state list command to display all resources recorded in the state file. The output lists resource addresses in the format resource_type.resource_name. For example:
Show Resource Details
Theterraform state show command displays attributes of a single resource from the state file. For example, to see details of an S3 bucket:
Move Resources
Theterraform state mv command is used to rename or move a resource within the state file. This does not automatically update your configuration files, so you must adjust them manually.
For example, suppose you have the following configuration for a DynamoDB table:
state-locking to state-locking-db, execute:
terraform apply will confirm that no changes are pending:
Pulling Remote State
If you are using remote state, you may need to download the state file locally. Theterraform state pull command retrieves the remote state, which can be processed with tools like jq for JSON querying:
jq:
Removing Resources from State
Theterraform state rm command allows you to remove one or more resources from the state file. This is useful when you no longer want Terraform to manage a resource. Remember, removing a resource from the state does not delete the actual infrastructure.
Pushing State Changes
Theterraform state push command updates the remote state by pushing your local state file. Use this command with extreme caution, as it can disrupt your configuration if misused. If the local state differs significantly from the remote state, you may encounter an error message like this:
--force option exists, it is recommended only as a last resort.
That concludes our lesson on basic Terraform commands. By understanding these commands, you will be better equipped to validate your configurations, maintain a clean state, and manage your infrastructure efficiently. For additional resources and more detailed explanations, consider visiting the following links: