docs/guide/coverage/iac/terraform.md
Trivy supports the scanners listed in the table below.
| Scanner | Supported |
|---|---|
| Misconfiguration | ✓ |
| Secret | ✓ |
It supports the following formats:
| Format | Supported |
|---|---|
| JSON | ✓ |
| HCL | ✓ |
| Plan Snapshot | ✓ |
| Plan JSON | ✓ |
Trivy can scan Terraform Plan files (snapshots) or their JSON representations. To create a Terraform Plan and scan it, run the following command:
terraform plan --out tfplan
trivy config tfplan
To scan a Terraform Plan representation in JSON format, run the following command:
terraform show -json tfplan > tfplan.json
trivy config tfplan.json
Trivy recursively searches directories and scans all found Terraform files. It also evaluates variables, imports, and other elements within Terraform files to detect misconfigurations.
You can provide tf-vars files to Trivy to override default values specified in the Terraform HCL code.
trivy config --tf-vars dev.terraform.tfvars ./infrastructure/tf
By default, downloaded modules are also scanned.
If you don't want to scan them, you can use the --tf-exclude-downloaded-modules flag.
trivy config --tf-exclude-downloaded-modules ./configs
The secret scan is performed on plain text files, with no special treatment for Terraform.
Trivy relies on static analysis of Terraform configurations. As a result, data sources and resource attributes whose values are only known after creation or update are not fully supported.
Terraform evaluates data blocks by querying the provider during the plan phase. However,
Trivy does not execute provider calls and therefore cannot resolve values that depend on
external or cloud state. Such values are treated as unknown during static analysis.
The same limitation applies to resource attributes marked as computed, whose values become available only after a resource is created or updated (for example, autogenerated IDs, IP addresses, or DNS names).
Because of this, expressions that depend on data blocks or computed attributes may:
unknown, ortry function),which can lead to false positives or false negatives during misconfiguration detection.
The plan created by Terraform does not provide complete information about references in expressions
that use each or count objects. For this reason, in some situations it is not possible
to establish references between resources that are needed for checks when detecting misconfigurations.
An example of such a configuration is:
locals {
buckets = toset(["test"])
}
resource "aws_s3_bucket" "this" {
for_each = local.buckets
bucket = each.key
}
resource "aws_s3_bucket_acl" "this" {
for_each = local.buckets
bucket = aws_s3_bucket.this[each.key].id
acl = "private"
}
With this configuration, the plan will not contain information about which attribute
of the aws_s3_bucket resource is referenced by the aws_s3_bucket_acl resource.
See more here.