Static Code testing
Linting
Linting is the process of checking source code for syntax and style errors.
Terraform fmt: This is a built-in Terraform command that should be the first port of call. The command formats your Terraform code based on a set of standard formatting rules.
tflint: This is a popular open-source tool that checks for syntax errors, best practices, and code style consistency. Once installed, simply run it using the command:
Checkov: This is an open-source static analysis tool for Terraform that checks for security and compliance issues in your Terraform code. Install it using the python package manager pip and run it using the command below:
Checkov will identify security issues and provides recommendations for how to fix the issue, along with the location of the relevant code, such as publically accessible storage accounts.
Terrascan: This open-source tool performs static code analysis to identify security vulnerabilities and compliance violations in Terraform code. Example output is shown below for a publically accessible storage account:
Check out the list of other popular tools used in Terraform-managed deployments.
Compliance Testing
terraform-compliance enables you to write a set of conditions in YAML files that must be met and test your code against them.
It can easily be installed using pip and run using the command shown below:
For example, the YAML file below specifies that Azure Storage Account should not be publicly accessible:
Drift Testing
Terraform will natively test for drift between your code and the real infrastructure when terraform plan
is run. Terraform will compare the current state of your infrastructure to the state saved in the state file.
If there are any differences, Terraform will display an execution plan that shows how to update your infrastructure to match your configuration.
You can also make use of driftctl
which is a free open-source tool that can report on infrastructure drift. Example output from the tool is shown below:
Periodic monitoring of the IaC-managed infrastructure to proactively check for drifts is a challenge.
Last updated