validate
Usage
terraform validate
command validates the syntax and internal consistency of Terraform configuration files in a directory. It does not access remote services. This command is useful for verifying reusable modules and attribute names and value types. It can be used locally or as a test step in a CI system.
Different use cases of terraform validate
command are:
- Verifying the syntax and format of Terraform configuration files.
- Example: Running
terraform validate
on a directory containing Terraform files to ensure they are written correctly and are free from syntax errors.
- Example: Running
- Checking the validity of attribute values, providers, and modules.
- Example: Running
terraform validate
on a module to ensure that all required input variables have been defined and that their types match the module's expectations.
- Example: Running
- Using it as a test step in a CI/CD pipeline to check for configuration errors before deploying infrastructure.
- Example: Running
terraform validate
as part of a Jenkins pipeline to ensure that Terraform configurations are valid and free from errors before being deployed to production.
- Example: Running
Options
-json
Produce output in a machine-readable JSON format, suitable for use in text editor integrations and other automated systems. Always disables color.
-no-color
If specified, output won't contain any color.
Result
validation success
Example of what the JSON output looks like when running terraform validate -json
:
{
"valid": true,
"error_count": 0,
"warning_count": 0,
"diagnostics": [],
"format_version": "1.0"
}
validation failed
Here is an example of a complete sample output in JSON format when you run the terraform validate
command on an invalid Terraform configuration:
{
"valid": false,
"error_count": 2,
"warning_count": 0,
"diagnostics": [
{
"severity": "error",
"summary": "Missing required argument",
"detail": "The argument \\"region\\" is required, but no definition was found.",
"range": {
"filename": "main.tf",
"start": {
"line": 1,
"column": 1,
"byte": 0
},
"end": {
"line": 1,
"column": 16,
"byte": 15
}
}
},
{
"severity": "error",
"summary": "Unsupported argument",
"detail": "An argument named \\"access_key\\" is not expected here.",
"range": {
"filename": "main.tf",
"start": {
"line": 2,
"column": 3,
"byte": 18
},
"end": {
"line": 2,
"column": 22,
"byte": 37
}
}
}
],
"format_version": "1.0"
}
In this example, the valid
property indicates that the configuration is not valid, and the error_count
property indicates that there are two errors detected by the terraform validate
command. The warning_count
property indicates that there are no warnings, and the diagnostics
property contains an array of diagnostic messages describing the errors