So I have (thanks to Constructing resource names within Terraform), managed to remove a pre-processing of a static map to resources using for-each.
What I now have is a map that has, as one of the properties the name/reference to a resource.
(I’ve removed nearly all of the map to the only element that references a pre-existing resource).
organisation_accounts = {
devops = {
organization_unit = "aws_organizations_organizational_unit.devops"
}
old-production = {
organization_unit = "aws_organizations_organizational_unit.production"
},
old-staging = {
organization_unit = "aws_organizations_organizational_unit.staging"
},
production-compute = {
organization_unit = "aws_organizations_organizational_unit.production"
},
staging-compute = {
organization_unit = "aws_organizations_organizational_unit.staging"
},
production-store = {
organization_unit = "aws_organizations_organizational_unit.production",
}
staging-store = {
organization_unit = "aws_organizations_organizational_unit.staging",
}
production-testing = {
organization_unit = "aws_organizations_organizational_unit.testing"
}
staging-testing = {
organization_unit = "aws_organizations_organizational_unit.testing"
}
}
What I want to do is use the value as a reference to the actual resource, but I can’t find the appropriate syntax for this.
resource "aws_organizations_account" "organization_account" {
for_each = var.organisation_accounts
email = each.value.email
name = "digitickets-${each.key}"
parent_id = "${each.value.organization_unit}.id"
role_name = "OrganizationAccountAccessRole"
lifecycle {
ignore_changes = [role_name]
}
tags = merge(local.base_tags, {
"Name" = "${var.environment}-account-${each.key}"
})
}
The above produces the error
Error: invalid value for parent_id (see https://docs.aws.amazon.com/organizations/latest/APIReference/API_MoveAccount.html#organizations-MoveAccount-request-DestinationParentId)
on organization_accounts.tf line 1, in resource "aws_organizations_account" "organization_account":
1: resource "aws_organizations_account" "organization_account" {
for each of the accounts.
I can’t work out how to “lookup” resources.
Any help on this would be appreciated.