Terraform Strings and Conditionals: The Complete Guide

Terraform has no if statement. It has no for loop in the sense most languages mean. What it has is expressions, and once you know the handful that matter, most of the "how do I do X in Terraform" questions collapse into the same few answers.
This covers building strings, testing them, and every flavour of conditional: values, attributes, resources and data sources.
TL;DR
- Build strings with interpolation
"${var.a}-${var.b}", join lists withjoin(",", list), split them back withsplit(). - Substring test is
strcontains(str, sub)on Terraform 1.5 and later,can(regex(...))before that.contains()is for list membership, not substrings, and mixing them up is the most common mistake here. - There is no if/else. There is a ternary:
condition ? a : b. Chain them for else-if. &&,||and!are the boolean operators. Since Terraform 1.12 they short-circuit, solength(var.l) > 0 && var.l[0] == "x"is safe. On older versions, keep both sides valid.- Make a resource conditional with
count = var.enabled ? 1 : 0, and remember it becomes a list, so reference it asresource[0]or withone(). - Make an attribute conditional with
dynamicblocks, or set it tonullto leave it unset. - Handle a value that might not exist with
try(),coalesce()orlookup(), not with a conditional.
Prerequisites
- Terraform 1.x installed
- Familiarity with
variable,locals,resourceandoutputblocks
Building strings
Interpolation
The everyday case. Anything inside ${} is evaluated and its result inserted:
variable "environment" {
type = string
default = "dev"
}
variable "app_name" {
type = string
default = "checkout"
}
locals {
bucket_name = "${var.app_name}-${var.environment}-assets"
# checkout-dev-assets
}
You do not need interpolation when the whole value is a single expression. This is redundant:
name = "${var.app_name}" # don't
name = var.app_name # do
Current Terraform accepts it without a warning (terraform validate on 1.15.8 prints nothing), so use TFLint's terraform_deprecated_interpolation rule to find it. It is the single most common thing to clean up in an inherited codebase.
To run TFLint on every commit, together with terraform fmt, terraform validate and generated docs, see Day 7 of Advent of DevOps: Improve the Terraform Module. It adds pre-commit hooks, a README and CI checks to a module.
Build an Endpoint URL From Variables
A URL is interpolation with a little cleanup, so that trailing or leading slashes in the input do not produce //. The validation block below only checks for an https:// prefix, which catches the common mistake of a plain http:// or a missing scheme. It is not full URL validation. This example sets the backend URL of an API Gateway HTTP integration:
variable "backend_base_url" {
type = string
description = "Base URL of the backend service, for example https://api.example.com"
validation {
condition = startswith(var.backend_base_url, "https://")
error_message = "backend_base_url must start with https://."
}
}
variable "resource_path" {
type = string
default = "v1/orders"
}
locals {
backend_url = "${trim(var.backend_base_url, "/")}/${trim(var.resource_path, "/")}"
# https://api.example.com/v1/orders, with or without the extra slashes
}
resource "aws_api_gateway_integration" "orders" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.orders.id
http_method = aws_api_gateway_method.get_orders.http_method
type = "HTTP_PROXY"
integration_http_method = "GET"
uri = local.backend_url
}
HTTP and HTTP_PROXY integrations need integration_http_method, the method API Gateway uses to call the backend. A changed integration is not live until a new deployment exists, so give aws_api_gateway_deployment a triggers map with a hash of the integration (for example redeployment = sha1(jsonencode(aws_api_gateway_integration.orders))) and create_before_destroy. The public URL of a stage is in aws_api_gateway_stage.<name>.invoke_url.
format() for anything with structure
When you are padding numbers or repeating a value, format() is clearer than a wall of interpolation:
locals {
# web-001, web-002, web-003
instance_names = [for i in range(1, 4) : format("web-%03d", i)]
arn = format("arn:aws:s3:::%s-%s", var.app_name, var.environment)
}
formatlist() does the same across a list, which saves a for expression:
locals {
urls = formatlist("https://%s.example.com", ["api", "web", "admin"])
# ["https://api.example.com", "https://web.example.com", "https://admin.example.com"]
}
join() and split()
join() turns a list into a string. It is the answer to most "convert a list to a string" questions:
locals {
azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
az_csv = join(",", local.azs) # eu-west-1a,eu-west-1b,eu-west-1c
az_lines = join("\n", local.azs) # one per line
}
split() goes the other way, which is how you accept a comma-separated variable from CI and turn it into a real list:
variable "subnet_ids_csv" {
type = string
default = "subnet-aaa,subnet-bbb"
}
locals {
subnet_ids = split(",", var.subnet_ids_csv)
}
split(",", "") returns [""], a list with one empty string, not an empty list. If the variable might be empty, guard it:
subnet_ids = var.subnet_ids_csv == "" ? [] : split(",", var.subnet_ids_csv)
For machine-readable output, jsonencode() beats hand-built strings every time:
policy = jsonencode({
Version = "2012-10-17"
Statement = [{ Effect = "Allow", Action = "s3:GetObject", Resource = "${local.bucket_arn}/*" }]
})
Testing strings
Does this string contain that one
On Terraform 1.5 and later there is a function for it:
locals {
is_prod = strcontains(var.environment, "prod")
}
Before 1.5, the idiom was a regex wrapped so a non-match does not error:
locals {
is_prod = can(regex("prod", var.environment))
}
Or counting matches, which reads badly but works everywhere:
locals {
is_prod = length(regexall("prod", var.environment)) > 0
}
contains() is not the function you want here. contains(list, value) tests whether a list holds an exact element:
contains(["dev", "staging"], var.environment) # list membership, correct
contains("production", "prod") # error, not a substring test
This trips people up constantly because the names are so close.
Prefixes, suffixes and case
locals {
is_internal = startswith(var.hostname, "internal-")
is_backup = endswith(var.filename, ".bak")
normalised = lower(trimspace(var.user_input))
}
startswith and endswith also arrived in 1.5. Before that: substr(s, 0, length(prefix)) == prefix.
Conditionals
There is no if, there is a ternary
locals {
instance_type = var.environment == "production" ? "m6i.xlarge" : "t3.micro"
}
Both branches must return the same type. This fails, because one branch is a string and the other a number:
value = var.enabled ? "yes" : 0 # error
Else-if is a chain
There is no elsif. Nest the ternaries, and format them one per line or nobody will read it:
locals {
instance_type = (
var.environment == "production" ? "m6i.xlarge" :
var.environment == "staging" ? "t3.large" :
"t3.micro"
)
}
Past three branches, a map lookup is clearer and easier to extend:
locals {
sizes = {
production = "m6i.xlarge"
staging = "t3.large"
dev = "t3.micro"
}
instance_type = lookup(local.sizes, var.environment, "t3.micro")
}
The third argument to lookup() is the default, and it is what stops an unknown environment blowing up the plan.
and, or, not
locals {
needs_backup = var.environment == "production" && var.data_tier
is_lower_env = var.environment == "dev" || var.environment == "staging"
skip_approval = !var.require_approval
}
Since Terraform 1.12, && and || short-circuit: when the left side decides the result, Terraform does not evaluate the right side. On Terraform 1.15.8 this is safe with an empty list:
locals {
# false for an empty list, no error
first_is_x = length(var.items) > 0 && var.items[0] == "x"
}
Two things still go wrong. On Terraform 1.11 and older, both sides are evaluated, so use a ternary there: length(var.items) > 0 ? var.items[0] == "x" : false. And on any version, do not test for an empty list with var.items != []: [] is an empty tuple, a different type from list(string), so the comparison is always true. Use length(var.items) > 0.
When the value might not exist
This is where people reach for a conditional and should not. Three better tools:
locals {
# first non-null, non-empty value
region = coalesce(var.region, var.default_region, "eu-west-1")
# map key with a fallback
owner = lookup(var.tags, "Owner", "unassigned")
# swallow the error from an expression that might not resolve
vpc_id = try(data.aws_vpc.selected.id, null)
}
try() takes expressions and returns the first that evaluates without error. It is the right answer for optional nested structures:
port = try(var.config.network.port, 8080)
Conditional attributes
Setting an attribute to null unsets it
An attribute set to null behaves as though you never wrote it, which means you get the provider default:
resource "aws_instance" "app" {
ami = var.ami_id
instance_type = var.instance_type
# only set when the caller supplied one, otherwise provider default
key_name = var.ssh_key_name != "" ? var.ssh_key_name : null
}
This is much cleaner than duplicating the whole resource behind a conditional.
dynamic blocks for optional nested blocks
You cannot put a ternary around a block. You can generate zero or more of them:
resource "aws_security_group" "app" {
name = "${var.app_name}-sg"
vpc_id = var.vpc_id
# zero blocks when the list is empty, one per entry otherwise
dynamic "ingress" {
for_each = var.allowed_cidrs
content {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [ingress.value]
}
}
}
For a single optional block, iterate over a list that is either empty or has one element:
dynamic "logging" {
for_each = var.enable_logging ? [1] : []
content {
target_bucket = var.log_bucket
target_prefix = "logs/"
}
}
That ? [1] : [] pattern is worth committing to memory. It is how you say "this block, but only sometimes".
Conditional resources
count for on/off
resource "aws_cloudwatch_log_group" "app" {
count = var.enable_logging ? 1 : 0
name = "/aws/app/${var.app_name}"
retention_in_days = 30
}
The catch: the resource is now a list, so every reference changes:
# wrong once count is present
log_group = aws_cloudwatch_log_group.app.name
# correct, but blows up when count is 0
log_group = aws_cloudwatch_log_group.app[0].name
# safe either way, returns null when the list is empty
log_group = one(aws_cloudwatch_log_group.app[*].name)
one() takes a list of zero or one element and returns the element or null. It is the cleanest way to reference an optionally created resource.
for_each when there are several
count gets fragile when the set changes, because resources are addressed by index and removing the middle one re-indexes everything after it. for_each addresses by key instead:
resource "aws_s3_bucket" "data" {
for_each = toset(var.bucket_names)
bucket = "${var.app_name}-${each.key}"
}
Remove a name from the middle of the list and only that bucket is destroyed. With count, you would have destroyed and recreated everything after it.
for_each keys must be known at plan time. If you build them from an attribute of another resource that does not exist yet, you get "Invalid for_each argument: the for_each value depends on resource attributes that cannot be determined until apply". Key off your input variables instead of computed attributes.
Conditional data sources
Same count trick, and the same list access on the way out:
data "aws_ami" "custom" {
count = var.custom_ami_id == "" ? 1 : 0
most_recent = true
owners = ["self"]
filter {
name = "name"
values = ["${var.app_name}-*"]
}
}
locals {
ami_id = var.custom_ami_id != "" ? var.custom_ami_id : one(data.aws_ami.custom[*].id)
}
This is the standard shape for "look it up only if the caller did not tell me".
The mistakes worth knowing about
Type mismatch across ternary branches. Both sides must agree. var.x ? "a" : null is fine because null fits any type; var.x ? "a" : 1 is not.
Forgetting the list after adding count. Adding count to an existing resource changes its address from aws_instance.app to aws_instance.app[0], so every reference must change. The object itself is safe: Terraform proposes to move it to [0] on its own (terraform_data.app has moved to terraform_data.app[0] in our 1.15.8 run). Switching to for_each with string keys needs moved blocks, which Terraform 1.1 added for explicit moves.
Using contains() for substrings. Covered above, still the most common one.
Comparing with []. var.items != [] is always true for a typed list. Use length().
split() on an empty string. Returns [""], not [].
Building JSON by hand. Use jsonencode(). Hand-built JSON breaks the first time a value contains a quote.
Wrapping up
Almost every Terraform expression question reduces to one of these: interpolate or format() to build a string, join/split to move between strings and lists, strcontains or can(regex(...)) to test one, a ternary or a map lookup to choose a value, null or a dynamic block to make an attribute optional, and count/for_each with one() to make a resource optional.
The two that save the most time in practice are try() for values that might not exist and one() for resources that might not exist. Both replace a conditional that would otherwise be wrong in some edge case.
For more Terraform, we have written about running Terraform for a specific resource only, removing a resource from state and Terraform best practices.
Try it hands-on
Run the commands from this article in the browser. Nothing to install.
Terraform Basics Simulator
Learn Terraform basics in an interactive browser lab. Read and edit the HCL config, run init, validate, plan, apply, and destroy, and watch state react to your edits. A free, hands-on way to learn infrastructure as code with no AWS account.
Terraform State Puzzle
Fix broken Terraform state without destroying production. Eight puzzles with real Terraform output: a rename that would replace a database, an import that forces replacement, a stale lock, drift, count index shifts and more. Choose between moved, import and removed blocks and the state commands.
We earn commissions when you shop through the links below.
Bitrise
Managed runners for GitHub Actions
Bitrise Build Hub runs your GitHub Actions jobs on M4 Macs and AMD Linux machines. Change runs-on and keep your workflows, secrets and logs on GitHub.
Atomsized
AWS platform engineering and GitOps
Design and automation for reliable AWS and Kubernetes platforms, safer delivery workflows, and preview and UAT environments your engineers can understand and own.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
Terraform Module Outputs, Inputs and Sources: How Modules Pass Data to Each Other
How to get a value out of a Terraform module, pass a resource into another module, chain modules with for_each, pin a Git branch or tag as a module source, share providers and variables across modules, and fix the "Provider configuration not present" error that shows up when you refactor. The terminal examples were run on Terraform 1.15.
Terraform State Management
What is Terraform state, why is it important, and how do you manage state in a team environment?
mid
Terraform Repository Structure Checklist
Best practices for organizing and structuring your Terraform projects for maintainability and scalability.
30-45 minutes