Skip to main content
The locals block defines named computed values that can be referenced by resources and outputs within the same stack. Locals are the right place for conditional expressions and other transformations that cannot appear directly in resource properties.

Syntax

locals "name" {
    field_name = expr
}

Example

locals "networking" {
    nat_tier = "standard" if input.deploy_nat else "none"
}
Generated Python:
# ── Locals ────────────────────────────────────────────────────────────────────
nat_tier = ("standard" if deploy_nat else "none")

Referencing locals

Locals fields are accessed via the locals.fieldName qualified form:
stack "s" {
    resource = SomeProvider.Resource {
        tier = locals.nat_tier
    }
}
With multiple locals blocks, use locals.blockName.fieldName:
locals "computed" {
    arn_prefix = f"arn:aws:s3:::{input.bucket_name}"
}

// reference:
output "s" {
    arn = locals.computed.arn_prefix
}

Why locals exist

Resource properties cannot contain match or ternary (if/else) expressions directly — those must go in locals:
// Error — conditional in resource property:
stack "s" {
    bucket = aws.s3.Bucket {
        acl = "public" if input.public else "private"  // not allowed
    }
}

// Correct:
locals "s" {
    acl = "public" if input.public else "private"
}

stack "s" {
    bucket = aws.s3.Bucket {
        acl = locals.acl
    }
}

Supported expressions

Locals can hold any expression:
  • Ternary: "a" if cond else "b"
  • Match: match input.env { "prod": "t3.large" default: "t3.micro" }
  • F-strings: f"prefix-{input.name}-suffix"
  • Arithmetic: input.count * 2
  • Boolean logic: input.enabled && !input.disabled
  • Cross-stack refs: @otherStack.someOutput

Reserved field names

The same reserved names that apply to input also apply to locals: stack, input, output, locals, module, provider, import, space, root, for, when, match, default, count, each

Duplicate field names

Locals field names are unique across all locals blocks in the stack. Duplicates produce a compile error.