File-Scoped Locals: Simplify Stack Configuration with Temporary Variables
· 5 min read
We're introducing file-scoped locals to Atmos stack configurations. Inspired by Terraform and Terragrunt, locals let you define temporary variables within a single file, reducing repetition and making your configurations more readable and maintainable.
The Problem: Repetition in Stack Configurations
Complex stack configurations often contain repeated values. You might have a naming convention that combines namespace, environment, and stage across multiple components:
# Before: Repetitive and error-prone
components:
terraform:
vpc:
vars:
name: acme-prod-us-east-1-vpc
tags:
Environment: prod
Namespace: acme
eks:
vars:
cluster_name: acme-prod-us-east-1-eks
tags:
Environment: prod
Namespace: acme
rds:
vars:
identifier: acme-prod-us-east-1-rds
tags:
Environment: prod
Namespace: acme
This approach has several problems:
- Repetition - Same values copied everywhere
- Inconsistency risk - Easy to mistype or forget to update all occurrences
- Hard to refactor - Changing a naming convention requires updates in many places
The Solution: File-Scoped Locals
Locals let you define variables once and reference them throughout the file:
# After: Clean and DRY
locals:
namespace: acme
environment: prod
stage: us-east-1
name_prefix: "{{ .locals.namespace }}-{{ .locals.environment }}-{{ .locals.stage }}"
tags:
Environment: "{{ .locals.environment }}"
Namespace: "{{ .locals.namespace }}"
components:
terraform:
vpc:
vars:
name: "{{ .locals.name_prefix }}-vpc"
tags: "{{ .locals.tags }}"
eks:
vars:
cluster_name: "{{ .locals.name_prefix }}-eks"
tags: "{{ .locals.tags }}"
rds:
vars:
identifier: "{{ .locals.name_prefix }}-rds"
tags: "{{ .locals.tags }}"
