When writing a Terraform configuration, it is important to be able to validate the syntax of the code 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 client tool, we can check the syntax of a Terraform configuration.
Getting ready
For this recipe, we will start with the following Terraform configuration, which is written in a main.tf file:
What we notice in the preceding code is that the declaration of the environment variable is missing.
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 init
- Then, validate the code by executing the validate command:
terraform validate
How it works…
In step 1, we initialize the Terraform context by executing the terraform init command.
Then, we perform a check of the code validity by executing...