|
| 1 | +--- |
| 2 | +name: account-map-migration |
| 3 | +description: >- |
| 4 | + Use when fixing legacy account-map component references or creating new components. Covers migration from dynamic |
| 5 | + account-map lookups to static account_map variable. Use when you see account-map remote-state references or need to |
| 6 | + set up provider configuration for a new component. |
| 7 | +--- |
| 8 | + |
| 9 | +# Account Map Migration |
| 10 | + |
| 11 | +This repository has migrated away from the `account-map` component to use Atmos Auth for authentication. Instead of |
| 12 | +dynamically looking up account IDs via the account-map component's remote state, we use a static `account_map` variable |
| 13 | +defined in `stacks/orgs/acme/_defaults.yaml`. |
| 14 | + |
| 15 | +## Why We Migrated |
| 16 | + |
| 17 | +The legacy `account-map` component pattern required: |
| 18 | + |
| 19 | +1. Deploying account-map component first (chicken-and-egg problem) |
| 20 | +2. Remote state lookups for every component that needed account IDs |
| 21 | +3. Complex `providers.tf` with remote-state module calls |
| 22 | +4. Cross-account state access permissions |
| 23 | + |
| 24 | +The new pattern: |
| 25 | + |
| 26 | +1. Static account map defined once in stack defaults |
| 27 | +2. No remote state dependencies for account lookups |
| 28 | +3. Simpler provider configuration |
| 29 | +4. Works with Atmos Auth for authentication |
| 30 | + |
| 31 | +## Key Configuration |
| 32 | + |
| 33 | +### Stack Defaults |
| 34 | + |
| 35 | +The account map is defined in `stacks/orgs/acme/_defaults.yaml`: |
| 36 | + |
| 37 | +```yaml |
| 38 | +vars: |
| 39 | + account_map_enabled: false |
| 40 | + account_map: |
| 41 | + full_account_map: |
| 42 | + acme-core-root: "111111111111" |
| 43 | + acme-core-audit: "222222222222" |
| 44 | + acme-core-auto: "333333333333" |
| 45 | + acme-plat-dev: "444444444444" |
| 46 | + acme-plat-staging: "555555555555" |
| 47 | + acme-plat-prod: "666666666666" |
| 48 | + # ... all accounts |
| 49 | + iam_role_arn_templates: |
| 50 | + terraform: "arn:aws:iam::%s:role/acme-core-gbl-auto-terraform" |
| 51 | + audit_account_account_name: "acme-core-audit" |
| 52 | + root_account_account_name: "acme-core-root" |
| 53 | +``` |
| 54 | +
|
| 55 | +### Vendored providers.tf |
| 56 | +
|
| 57 | +Components use a vendored `providers.tf` from Atmos mixins that includes: |
| 58 | + |
| 59 | +- `account_map_enabled` and `account_map` variables |
| 60 | +- Provider configuration that uses the static account map |
| 61 | +- Dummy `iam_roles` module for legacy compatibility |
| 62 | + |
| 63 | +**Vendoring is configured in each component's `component.yaml`:** |
| 64 | + |
| 65 | +```yaml |
| 66 | +# components/terraform/<component-name>/component.yaml |
| 67 | +apiVersion: atmos/v1 |
| 68 | +kind: ComponentVendorConfig |
| 69 | +spec: |
| 70 | + source: |
| 71 | + uri: github.com/cloudposse-terraform-components/aws-<component>.git//src?ref={{ .Version }} |
| 72 | + version: v1.x.x |
| 73 | + included_paths: |
| 74 | + - "**/**" |
| 75 | + excluded_paths: |
| 76 | + - "providers.tf" # Exclude upstream providers.tf |
| 77 | + mixins: |
| 78 | + # Vendor the providers.tf with account-map support |
| 79 | + - uri: https://raw.githubusercontent.com/cloudposse-terraform-components/mixins/{{ .Version }}/src/mixins/provider-without-account-map.tf |
| 80 | + version: v0.3.0 |
| 81 | + filename: providers.tf |
| 82 | + - uri: https://raw.githubusercontent.com/cloudposse-terraform-components/mixins/{{ .Version }}/src/mixins/account-verification.mixin.tf |
| 83 | + version: v0.3.0 |
| 84 | + filename: account-verification.mixin.tf |
| 85 | +``` |
| 86 | + |
| 87 | +**Key points:** |
| 88 | + |
| 89 | +- The upstream `providers.tf` is excluded via `excluded_paths` |
| 90 | +- The `provider-without-account-map.tf` mixin is vendored as `providers.tf` |
| 91 | +- This mixin includes the `account_map_enabled` and `account_map` variables |
| 92 | + |
| 93 | +**To vendor (or re-vendor) the component:** |
| 94 | + |
| 95 | +```bash |
| 96 | +atmos vendor pull -c <component-name> |
| 97 | +``` |
| 98 | + |
| 99 | +The vendored `providers.tf` handles all account map logic automatically. You don't need to manually add these variables |
| 100 | +to `variables.tf` - they're included in `providers.tf`. |
| 101 | + |
| 102 | +## Migration Checklist |
| 103 | + |
| 104 | +When migrating a component or creating a new one: |
| 105 | + |
| 106 | +1. **Vendor providers.tf** - Run `atmos vendor pull -c <component-name>` to get the latest providers.tf with account map |
| 107 | + support |
| 108 | +2. **Update remote-state.tf** - If the component has a `remote-state.tf` that references account-map, update it to use |
| 109 | + the bypass pattern (see below) |
| 110 | +3. **Verify catalog** - Ensure `account_map_enabled: false` is set (inherited from `_defaults.yaml`) |
| 111 | +4. **Test** - Run `atmos terraform plan` to verify |
| 112 | + |
| 113 | +### Bypass Pattern for remote-state.tf |
| 114 | + |
| 115 | +If a component has a `remote-state.tf` with an account-map lookup, update it to use `bypass` and `defaults`: |
| 116 | + |
| 117 | +```hcl |
| 118 | +module "account_map" { |
| 119 | + source = "cloudposse/stack-config/yaml//modules/remote-state" |
| 120 | + version = "1.8.0" |
| 121 | +
|
| 122 | + component = "account-map" |
| 123 | + tenant = var.account_map_enabled ? coalesce(var.account_map_tenant, module.this.tenant) : null |
| 124 | + environment = var.account_map_enabled ? var.account_map_environment : null |
| 125 | + stage = var.account_map_enabled ? var.account_map_stage : null |
| 126 | +
|
| 127 | + context = module.this.context |
| 128 | +
|
| 129 | + # When account_map is disabled, bypass remote state and use the static account_map variable |
| 130 | + bypass = !var.account_map_enabled |
| 131 | + defaults = var.account_map |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +**Key points:** |
| 136 | + |
| 137 | +- `bypass = !var.account_map_enabled` - Skips remote state lookup when disabled |
| 138 | +- `defaults = var.account_map` - Uses the static account_map variable instead |
| 139 | +- `module.account_map.outputs` works the same regardless of bypass - returns `defaults` when bypassed |
| 140 | + |
| 141 | +## Identifying Legacy References |
| 142 | + |
| 143 | +Search for components still using the old pattern: |
| 144 | + |
| 145 | +```bash |
| 146 | +# Find remote-state references to account-map |
| 147 | +grep -r "account-map" components/terraform/*/remote-state.tf |
| 148 | +
|
| 149 | +# Find components without account_map_enabled variable |
| 150 | +for dir in components/terraform/*/; do |
| 151 | + if ! grep -q "account_map_enabled" "$dir/variables.tf" 2>/dev/null; then |
| 152 | + echo "Missing: $dir" |
| 153 | + fi |
| 154 | +done |
| 155 | +``` |
| 156 | + |
| 157 | +## Reference Implementations |
| 158 | + |
| 159 | +See these components for the current pattern: |
| 160 | + |
| 161 | +- `components/terraform/vpc/` - Standard component with account_map |
| 162 | +- `components/terraform/ecr/` - Component with cross-account access |
| 163 | + |
| 164 | +## New Components |
| 165 | + |
| 166 | +When creating new components, the migrated pattern is automatic: |
| 167 | + |
| 168 | +1. **Vendor providers.tf** - Run `atmos vendor pull -c <component-name>` to get providers.tf with account map support |
| 169 | +2. **Inherit stack defaults** - `account_map_enabled: false` is inherited from `_defaults.yaml` |
| 170 | +3. **Use Atmos functions** - Use `!terraform.state` for cross-component dependencies instead of remote-state.tf |
| 171 | + |
| 172 | +See the `developing-components` skill for full component creation guidance. |
0 commit comments