locals
Terraform policy supports locals blocks in test files. These blocks behave like locals blocks in policy files, allowing you to define reusable values within your tests.
Configuration model
The locals block supports the following configuration:
Complete configuration example
The following example demonstrates a locals block that sets local values, and a resource block that references local values:
tests/example.policytest.hcl
locals {
common_tags = {
ManagedBy = "terraform"
Environment = "production"
}
approved_regions = ["us-east-1", "us-west-2"]
test_vpc_id = "vpc-12345678"
}
resource "aws_subnet" "public" {
attrs = {
vpc_id = local.test_vpc_id
cidr_block = "10.0.1.0/24"
availability_zone = local.approved_regions[0]
}
}
Specification
A locals block supports the following configuration.
name
Provide a value for the local with the given name. Refer to local values in your policy tests with local.<name>.
- Data type: Any
- Default: None
- Repeatable
Scope
Local values defined in test files are available throughout the test file and can be referenced using the local.<name> syntax.
Examples
The following examples demonstrate common locals block configuration patterns for specific use cases.
Common test data
In the following example, the locals block defines shared test values that multiple resource test cases reference to maintain consistency.
locals {
test_az = "us-east-1a"
test_ami = "ami-12345678"
}
resource "aws_instance" "example1" {
attrs = {
instance_type = "t3.micro"
ami = local.test_ami
availability_zone = local.test_az
}
}
resource "aws_instance" "example2" {
attrs = {
instance_type = "t3.small"
ami = local.test_ami
availability_zone = local.test_az
}
}
Shared tags
In the following example, the locals block defines common tags that multiple resource test cases apply to ensure consistent tagging across resources.
locals {
common_tags = {
ManagedBy = "terraform"
Environment = "test"
Owner = "platform-team"
}
}
resource "aws_instance" "web" {
attrs = {
instance_type = "t3.micro"
ami = "ami-12345678"
tags = local.common_tags
}
}
resource "aws_s3_bucket" "data" {
attrs = {
bucket = "test-bucket"
tags = local.common_tags
}
}
Complex data structures
In the following example, the locals block defines nested objects and lists that resource test cases use to configure VPC and subnet relationships.
locals {
vpc_config = {
id = "vpc-12345678"
cidr_block = "10.0.0.0/16"
}
subnet_configs = [
{
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
},
{
cidr_block = "10.0.2.0/24"
availability_zone = "us-east-1b"
}
]
}
resource "aws_subnet" "subnet1" {
attrs = {
vpc_id = local.vpc_config.id
cidr_block = local.subnet_configs[0].cidr_block
availability_zone = local.subnet_configs[0].availability_zone
}
}
resource "aws_subnet" "subnet2" {
attrs = {
vpc_id = local.vpc_config.id
cidr_block = local.subnet_configs[1].cidr_block
availability_zone = local.subnet_configs[1].availability_zone
}
}
Computed values
In the following example, the locals block uses string interpolation and list comprehensions to generate computed values for resource configurations.
locals {
base_name = "test"
vpc_name = "${local.base_name}-vpc"
sg_name = "${local.base_name}-sg"
instance_count = 3
instance_names = [for i in range(local.instance_count) : "${local.base_name}-instance-${i}"]
}
resource "aws_vpc" "main" {
attrs = {
cidr_block = "10.0.0.0/16"
tags = {
Name = local.vpc_name
}
}
}
resource "aws_security_group" "main" {
attrs = {
name = local.sg_name
description = "Security group for ${local.base_name}"
}
}
Reference data sources
In the following example, the locals block references mock data sources to create derived values that resource test cases use for their configurations.
locals {
vpc_id = data.aws_vpc.main.id
subnet_ids = [
data.aws_subnet.private1.id,
data.aws_subnet.private2.id
]
}
data "aws_vpc" "main" {
attrs = {
id = "vpc-12345678"
cidr_block = "10.0.0.0/16"
}
}
data "aws_subnet" "private1" {
attrs = {
id = "subnet-12345678"
vpc_id = local.vpc_id
}
}
data "aws_subnet" "private2" {
attrs = {
id = "subnet-87654321"
vpc_id = local.vpc_id
}
}
resource "aws_instance" "app" {
attrs = {
instance_type = "t3.micro"
ami = "ami-12345678"
subnet_id = local.subnet_ids[0]
}
}
Use locals in mock attributes
In the following example, the locals block uses conditional expressions to determine resource configurations based on the environment.
locals {
environment = "production"
env_prefix = local.environment
vpc_name = "${local.env_prefix}-vpc"
instance_type = local.environment == "production" ? "t3.medium" : "t3.micro"
}
resource "aws_vpc" "main" {
attrs = {
cidr_block = "10.0.0.0/16"
tags = {
Name = local.vpc_name
Environment = local.environment
}
}
}
resource "aws_instance" "app" {
attrs = {
instance_type = local.instance_type
ami = "ami-12345678"
}
}
Test data for multiple scenarios
In the following example, the locals block defines a map of test scenarios with different configurations that resource test cases reference to test various size requirements.
locals {
test_scenarios = {
small = {
instance_type = "t3.micro"
volume_size = 10
}
medium = {
instance_type = "t3.medium"
volume_size = 50
}
large = {
instance_type = "t3.large"
volume_size = 100
}
}
}
resource "aws_instance" "small" {
attrs = {
instance_type = local.test_scenarios.small.instance_type
ami = "ami-12345678"
}
}
resource "aws_ebs_volume" "small" {
attrs = {
availability_zone = "us-east-1a"
size = local.test_scenarios.small.volume_size
}
}
resource "aws_instance" "large" {
attrs = {
instance_type = local.test_scenarios.large.instance_type
ami = "ami-12345678"
}
}