Terraform variables support

Summary

Support for Terraform (TF) variables is currently available only in the CLI.
Snyk currently supports:
At this time Snyk does not support Output Values.
The CLI scans all of your directories and handles each directory that includes supported TF files as it own module. Each module that includes variables is dereferenced appropriately.
Supported TF file formats are .tf, .tfvars,.auto.tfvars. Snyk currently does not support variables that were set/defined using environment variables or the --var command.
The scan handles variable definition precedence in the same way that TF handles the precedence.
Loading a variable definitions file outside of the scanned directory
There is also the option to load an external variable definitions file by using the --var-file flag, for example:
snyk iac test myproject/staging/networking --var-file=myproject/vars.tf
This loads the vars.tf definitions file from the myproject directory, dereferences any variables if they exist, and applies them to the context of the scanned path (myproject/staging/networking).
For more information, refer to the IAC test help.

Supported expressions

The following expressions are currently supported:

Supported functions

The following functions are currently supported:
  • Numeric Functions - all functions
  • String Functions - all functions except lower, regex, regexall, replace, substr, title, upper
  • Collection Functions - chunklist, concat, distinct, flatten, length, merge, reverse, sort
  • Encoding Functions - csvdecode, jsondecode, jsonencode
  • Date and Time Functions - formatdate, timeadd

Examples

Variable handling in the correct precedence
In the example below we can see that we configured a new resource and we are using a variable named remote_user_addr to set its cidr_blocks value.
The variable is defined inside the variables.tf file with a default value but the value is being overridden inside the terraform.tfvars file.
At the end the value is set to 0.0.0.0/0 and this causes the CLI to raise an issue.
1
vpc.tf
2
3
resource "aws_security_group_rule" "ssh" {
4
type = "ingress"
5
from_port = 22
6
to_port = 22
7
protocol = "tcp"
8
cidr_blocks = [var.remote_user_addr]
9
security_group_id = aws_security_group.allow.id
10
}
Copied!
1
variables.tf
2
3
variable "remote_user_addr" {
4
type = string
5
default = "11.0.0.0/24"
6
}
Copied!
1
terraform.tfvars
2
3
remote_user_addr = "0.0.0.0/0"
Copied!
Conditional expression using variables
In the following example we are using local and input variables together with conditional expression.
We are checking to see if local.test equals 0 and we are setting the cidr_blocks accordingly.
In our case local.test equals to 0 and the value is set to the value of var.remote_user_addr which causes the CLI to raise an issue.
1
vpc.tf
2
3
resource "aws_security_group_rule" "ssh" {
4
type = "ingress"
5
from_port = 22
6
to_port = 22
7
protocol = "tcp"
8
cidr_blocks = local.test == 0 ? [var.remote_user_addr] : ["11.0.0.0/24"]
9
security_group_id = aws_security_group.allow.id
10
}
11
12
locals {
13
test = 0
14
}
Copied!
1
variables.tf
2
3
variable "remote_user_addr" {
4
type = string
5
default = "0.0.0.0/0"
6
}
Copied!
Export as PDF
Copy link
Edit on GitHub