docs/tutorials/misconfiguration/terraform.md
This tutorial is focused on ways Trivy can scan Terraform IaC configuration files.
A video tutorial on Terraform Misconfiguration scans can be found on the Aqua Open Source YouTube account.
A note to tfsec users We have been consolidating all of our scanning-related efforts in one place, and that is Trivy. You can read more on the decision in the tfsec discussions.
Terraform configuration scanning is available as part of the trivy config command. This command scans all configuration files for misconfiguration issues. You can find the details within misconfiguration scans in the Trivy documentation.
Command structure:
trivy config <any flags you want to use> <file or directory that you would like to scan>
The trivy config command can scan Terraform configuration, CloudFormation, Dockerfile, Kubernetes manifests, and Helm Charts for misconfiguration. Trivy will compare the configuration found in the file with a set of best practices.
Install Trivy on your local machines. The documentation provides several different installation options. This tutorial will use this example Terraform tutorial for terraform misconfiguration scanning with Trivy.
Git clone the tutorial and cd into the directory:
git clone [email protected]:Cloud-Native-Security/trivy-demo.git
cd bad_iac/terraform
In this case, the folder only contains Terraform configuration files. However, you could scan a directory that contains several different configurations e.g. Kubernetes YAML manifests, Dockerfile, and Terraform. Trivy will then detect the different configuration files and apply the right rules automatically.
trivy config scansBelow are several examples of how the trivy config scan can be used.
General Terraform scan with trivy:
trivy config <specify the directory>
So if we are already in the directory that we want to scan:
trivy config ./
The --format flag changes the way that Trivy displays the scan result:
JSON:
trivy config -f json terraform-infra
Sarif:
trivy config -f sarif terraform-infra
The --output flag specifies the file location in which the scan result should be saved:
JSON:
trivy config -f json -o example.json terraform-infra
Sarif:
trivy config -f sarif -o example.sarif terraform-infra
If you are presented with lots and lots of misconfiguration across different files, you might want to filter or the misconfiguration with the highest severity:
trivy config --severity CRITICAL, MEDIUM terraform-infra
trivy config scansYou can pass terraform values to Trivy to override default values found in the Terraform HCL code. More information are provided in the documentation.
trivy config --tf-vars terraform.tfvars ./
We have lots of examples in the documentation on how you can write and pass custom Rego checks into terraform misconfiguration scans.
The trivy config command does not perform secret and vulnerability checks out of the box. However, you can specify as part of your trivy fs scan that you would like to scan you terraform files for exposed secrets and misconfiguraction through the following flags:
trivy fs --scanners secret,misconfig ./
The trivy config command is a sub-command of the trivy fs command. You can learn more about this command in the documentation.
Instead of scanning your different Terraform resources individually, you could also scan your Terraform Plan file before it is deployed for misconfiguration. This will give you insights into any misconfiguration of your resources as they would become deployed. Here is the link to the documentation.
Note that you need to be able to create a terraform init and plan without any errors.
Similar to tfsec, Trivy can be used either on local developer machines or integrated into your CI/CD pipeline. There are several steps available for different pipelines, including GitHub Actions, Circle CI, GitLab, Travis and more in the tutorials section of the documentation: https://trivy.dev/docs/latest/tutorials/integrations/