|
| 1 | +--- |
| 2 | +title: Stack Names |
| 3 | +sidebar_position: 2 |
| 4 | +sidebar_label: name |
| 5 | +sidebar_class_name: command |
| 6 | +description: Use the name field to explicitly set a stack's logical name, or use name_template for programmatic naming. |
| 7 | +id: name |
| 8 | +--- |
| 9 | +import File from '@site/src/components/File' |
| 10 | +import Intro from '@site/src/components/Intro' |
| 11 | + |
| 12 | +<Intro> |
| 13 | +Every stack in Atmos has a logical name used for identification, the `-s` flag, dependencies, and Terraform workspaces. |
| 14 | +You can either set this name explicitly using the `name` field in your stack manifest, or compute it programmatically |
| 15 | +using `name_template` in `atmos.yaml`. |
| 16 | +</Intro> |
| 17 | + |
| 18 | +## Choosing Your Approach |
| 19 | + |
| 20 | +Atmos offers two approaches for stack naming. Choose based on your infrastructure's consistency: |
| 21 | + |
| 22 | +| Approach | Where | When to Use | |
| 23 | +|----------|-------|-------------| |
| 24 | +| `name_template` | `atmos.yaml` | Consistent context variables across all stacks | |
| 25 | +| `name` | Stack manifest | Inconsistent naming, migrations, or legacy infrastructure | |
| 26 | + |
| 27 | +### Use `name_template` When |
| 28 | + |
| 29 | +You should use `name_template` in `atmos.yaml` when: |
| 30 | + |
| 31 | +- You have consistent context variables (like `namespace`, `tenant`, `environment`, `stage`) across all stacks |
| 32 | +- All your root modules use these variables deterministically |
| 33 | +- You want programmatic, convention-based naming (e.g., following the [null-label](https://github.com/cloudposse/terraform-null-label) pattern) |
| 34 | + |
| 35 | +<File title="atmos.yaml"> |
| 36 | +```yaml |
| 37 | +stacks: |
| 38 | + name_template: "{{ .vars.tenant }}-{{ .vars.environment }}-{{ .vars.stage }}" |
| 39 | +``` |
| 40 | +</File> |
| 41 | +
|
| 42 | +With this configuration, a stack with `vars.tenant: acme`, `vars.environment: ue1`, and `vars.stage: prod` will automatically be named `acme-ue1-prod`. |
| 43 | + |
| 44 | +### Use `name` When |
| 45 | + |
| 46 | +You should use the `name` field in your stack manifest when: |
| 47 | + |
| 48 | +- You're migrating from Terragrunt or another tool and need to preserve existing workspace names |
| 49 | +- Your infrastructure has inconsistent naming that doesn't follow a pattern |
| 50 | +- You have legacy stacks that predate your naming conventions |
| 51 | +- You need to match existing Terraform workspace names exactly |
| 52 | +- Any inconsistency in your vars would break a deterministic template |
| 53 | + |
| 54 | +<File title="stacks/legacy-prod.yaml"> |
| 55 | +```yaml |
| 56 | +name: "my-legacy-prod-stack" |
| 57 | +
|
| 58 | +import: |
| 59 | + - catalog/defaults |
| 60 | +
|
| 61 | +components: |
| 62 | + terraform: |
| 63 | + vpc: |
| 64 | + vars: |
| 65 | + cidr: "10.0.0.0/16" |
| 66 | +``` |
| 67 | +</File> |
| 68 | + |
| 69 | +With this configuration, the stack is identified as `my-legacy-prod-stack` regardless of the filename or any `name_template` in `atmos.yaml`. |
| 70 | + |
| 71 | +## Precedence Order |
| 72 | + |
| 73 | +When determining a stack's name, Atmos uses this precedence order: |
| 74 | + |
| 75 | +1. **`name`** (highest) - Explicit name from stack manifest |
| 76 | +2. **`name_template`** - Go template from `atmos.yaml` |
| 77 | +3. **`name_pattern`** - Token-based pattern from `atmos.yaml` |
| 78 | +4. **Default** - Basename of the stack file (e.g., `prod.yaml` → `prod`) |
| 79 | + |
| 80 | +This means the `name` field always wins, allowing you to override programmatic naming for specific stacks. |
| 81 | + |
| 82 | +## Configuration |
| 83 | + |
| 84 | +The `name` field is a top-level key in stack manifests, at the same level as `import`, `vars`, and `components`: |
| 85 | + |
| 86 | +```yaml |
| 87 | +name: "explicit-stack-name" |
| 88 | +
|
| 89 | +import: |
| 90 | + - catalog/base |
| 91 | +
|
| 92 | +vars: |
| 93 | + environment: prod |
| 94 | +
|
| 95 | +components: |
| 96 | + terraform: |
| 97 | + vpc: |
| 98 | + vars: |
| 99 | + cidr: "10.0.0.0/16" |
| 100 | +``` |
| 101 | + |
| 102 | +## Examples |
| 103 | + |
| 104 | +### Migrating from Terragrunt |
| 105 | + |
| 106 | +When migrating from Terragrunt, your existing Terraform state is tied to specific workspace names. Use `name` to preserve these: |
| 107 | + |
| 108 | +<File title="stacks/us-east-1/prod/vpc.yaml"> |
| 109 | +```yaml |
| 110 | +name: "prod-us-east-1-vpc" # Matches existing Terraform workspace |
| 111 | +
|
| 112 | +import: |
| 113 | + - catalog/vpc |
| 114 | +
|
| 115 | +components: |
| 116 | + terraform: |
| 117 | + vpc: |
| 118 | + vars: |
| 119 | + cidr: "10.0.0.0/16" |
| 120 | +``` |
| 121 | +</File> |
| 122 | + |
| 123 | +This ensures `atmos terraform apply vpc -s prod-us-east-1-vpc` uses the existing state without requiring state migrations. |
| 124 | + |
| 125 | +### Legacy Infrastructure |
| 126 | + |
| 127 | +For infrastructure that predates your naming standards: |
| 128 | + |
| 129 | +<File title="stacks/old-datacenter.yaml"> |
| 130 | +```yaml |
| 131 | +name: "dc1-legacy-infra" # Historical name that must be preserved |
| 132 | +
|
| 133 | +components: |
| 134 | + terraform: |
| 135 | + network: |
| 136 | + vars: |
| 137 | + vpc_id: "vpc-abc123" |
| 138 | +``` |
| 139 | +</File> |
| 140 | + |
| 141 | +### Mixed Approach |
| 142 | + |
| 143 | +You can use `name_template` for most stacks while overriding specific ones with `name`: |
| 144 | + |
| 145 | +<File title="atmos.yaml"> |
| 146 | +```yaml |
| 147 | +stacks: |
| 148 | + name_template: "{{ .vars.tenant }}-{{ .vars.environment }}-{{ .vars.stage }}" |
| 149 | +``` |
| 150 | +</File> |
| 151 | + |
| 152 | +<File title="stacks/acme/prod/us-east-1.yaml"> |
| 153 | +```yaml |
| 154 | +# Uses name_template: acme-prod-ue1 |
| 155 | +vars: |
| 156 | + tenant: acme |
| 157 | + environment: prod |
| 158 | + stage: ue1 |
| 159 | +
|
| 160 | +components: |
| 161 | + terraform: |
| 162 | + vpc: |
| 163 | + vars: |
| 164 | + cidr: "10.0.0.0/16" |
| 165 | +``` |
| 166 | +</File> |
| 167 | + |
| 168 | +<File title="stacks/legacy-acquisition.yaml"> |
| 169 | +```yaml |
| 170 | +name: "acquired-company-prod" # Overrides name_template for this stack |
| 171 | +
|
| 172 | +vars: |
| 173 | + tenant: acquired |
| 174 | + environment: prod |
| 175 | + stage: main |
| 176 | +
|
| 177 | +components: |
| 178 | + terraform: |
| 179 | + vpc: |
| 180 | + vars: |
| 181 | + cidr: "10.1.0.0/16" |
| 182 | +``` |
| 183 | +</File> |
| 184 | + |
| 185 | +## How Stack Names Are Used |
| 186 | + |
| 187 | +Stack names appear throughout Atmos: |
| 188 | + |
| 189 | +- **CLI**: `atmos terraform apply vpc -s my-stack-name` |
| 190 | +- **Listing**: `atmos list stacks` shows stack names |
| 191 | +- **Dependencies**: Reference stacks by name in `settings.depends_on` |
| 192 | +- **Terraform Workspaces**: Workspace names derive from stack names |
| 193 | +- **Describe Commands**: `atmos describe stacks` uses stack names as keys |
| 194 | + |
| 195 | +## Best Practices |
| 196 | + |
| 197 | +1. **Prefer `name_template` for new projects** - Programmatic naming ensures consistency and reduces manual effort. |
| 198 | + |
| 199 | +2. **Use `name` as an escape hatch** - Reserve explicit names for migrations, legacy systems, or acquisitions. |
| 200 | + |
| 201 | +3. **Document your naming convention** - Whether using `name_template` or explicit names, document the pattern for your team. |
| 202 | + |
| 203 | +4. **Consider Terraform state implications** - Changing a stack's name changes its workspace, which affects state file location. |
| 204 | + |
| 205 | +5. **Keep names meaningful** - Stack names should convey environment, region, and purpose at a glance. |
| 206 | + |
| 207 | +## Related |
| 208 | + |
| 209 | +- [Stack Configuration](/stacks) |
| 210 | +- [Imports](/stacks/imports) |
| 211 | +- [Variables (vars)](/stacks/vars) |
| 212 | +- [Terraform Workspaces](/components/terraform/workspaces) |
| 213 | +- [Migrating from Terragrunt](/migration/terragrunt) |
0 commit comments