Validating the code syntax
When writing a Terraform configuration, it is important to be able to validate the syntax of the configuration we are writing before executing it, or even before archiving it in a Git repository.
We will see in this recipe how, by using the Terraform binary, we can check the validity of Terraform configuration syntax.
Getting ready
For this recipe, we will start with the following Terraform configuration, which is written in a main.tf
file:
Figure 6.6: Bad Terraform configuration
What we notice in the preceding code is that the declaration of the environment
variable is missing.
The source code of this recipe is available here: https://github.com/PacktPublishing/Terraform-Cookbook-Second-Edition/tree/main/CHAP06/validate.
How to do it…
To validate our Terraform configuration syntax, perform the following steps:
- To start, initialize the Terraform context by running the following command:
terraform...