Environment Variables
The env section in atmos.yaml defines global environment variables that are automatically set for all Atmos operations. Use this to configure tools, inject credentials, or set defaults that apply across your entire project.
You will learn
- Set environment variables once, use everywhere
- Works seamlessly with Atmos profiles for environment-specific configuration
- Supports YAML functions like
!envand!execfor dynamic values - Variables are merged with system environment (global env can override system, component env overrides global)
Configuration
Configure global environment variables in the env section of your atmos.yaml:
atmos.yaml
envA map of environment variable names to values. Values can be literal strings or use YAML functions for dynamic values.
Type:
map[string]stringDefault: Not set
Precedence
Environment variables are merged in the following order (lowest to highest priority):
| Priority | Source | Description |
|---|---|---|
| 1 (lowest) | System environment | Variables from your shell (os.Environ()) |
| 2 | Global env in atmos.yaml | Variables defined in CLI configuration |
| 3 (highest) | Component/stack env | Variables defined in stack manifests |
This means:
- Global
envinatmos.yamlcan override system environment variables - Component-level
envin stacks can override globalenv
Special Handling for Terraform CLI Args
Variables starting with TF_CLI_ARGS_ receive special handling. Instead of replacing, new values are prepended to existing values, allowing you to layer Terraform arguments:
# atmos.yaml
env:
TF_CLI_ARGS_plan: "-compact-warnings"
# If TF_CLI_ARGS_plan is already set to "-lock=false" in your shell,
# the final value becomes: "-compact-warnings -lock=false"
YAML Functions
The env section supports YAML functions for dynamic values:
Reading from Other Environment Variables
Use !env to read values from the current environment:
env:
# Copy GH_TOKEN to GITHUB_TOKEN if it exists
GITHUB_TOKEN: !env GH_TOKEN
# With a default value if not set
AWS_PROFILE: !env MY_AWS_PROFILE default
Executing Commands
Use !exec to set values from command output:
env:
# Get GitHub token from gh CLI
GITHUB_TOKEN: !exec gh auth token
# Get AWS account ID
AWS_ACCOUNT_ID: !exec aws sts get-caller-identity --query Account --output text
Use Cases
GitHub Token for Private Terraform Modules
When using private GitHub repositories as Terraform module sources, set GITHUB_TOKEN:
atmos.yaml
This enables Terraform to authenticate when downloading modules:
module "vpc" {
source = "git::https://github.com/myorg/terraform-aws-vpc.git?ref=v1.0.0"
}
Terraform Plugin Cache
Speed up Terraform operations by caching providers:
atmos.yaml
AWS SDK Configuration
Configure AWS SDK behavior globally:
atmos.yaml
OpenTofu Configuration
Set OpenTofu-specific environment variables:
atmos.yaml
Using with Atmos Profiles
The env section becomes powerful when combined with Atmos profiles. Define environment-specific variables in profile configurations:
Local Development Profile
profiles/developer/atmos.yaml
CI/CD Profile
profiles/ci/atmos.yaml
Usage
# Local development - uses gh CLI for GitHub token
atmos --profile developer terraform plan vpc -s prod
# CI/CD - uses existing GITHUB_TOKEN from CI environment
ATMOS_PROFILE=ci atmos terraform apply vpc -s prod --auto-approve
This pattern allows you to:
- Use
gh auth tokenlocally (no token management) - Use CI-provided
GITHUB_TOKENin pipelines (secure, no secrets in config) - Keep your base
atmos.yamlclean of environment-specific settings