💨

terraformでaws organizationを編集するときにAlreadyInOrganizationExceptionでハマる

2024/06/24に公開

雑メモ

terraform applyでこんな感じのエラーが出た.

error.log
$ terraform apply
...

aws_organizations_organization.org: Creating...
╷
│ Error: creating Organizations Organization: operation error Organizations: CreateOrganization, https response error StatusCode: 400, RequestID: ff559093-6751-42ef-84ef-c0456a3af507, AlreadyInOrganizationException: The AWS account is already a member of an organization.
│
│   with aws_organizations_organization.org,
│   on 020_organization.tf line 7, in resource "aws_organizations_organization" "org":
│    7: resource "aws_organizations_organization" "org" {
│
╵

terraformでorganizations_organizationの定義をするところで失敗している模様.

resource "aws_organizations_organization" "org" {
  aws_service_access_principals = [
    "config.amazonaws.com",
    "cloudtrail.amazonaws.com",
    "member.org.stacksets.cloudformation.amazonaws.com",
  ]
  enabled_policy_types = [
    "SERVICE_CONTROL_POLICY",
  ]
  feature_set = "ALL"
}

terraformの状態がawsの状態と一致してないのが原因だった。
terraform importで解決した!

$ terraform import aws_organizations_organization.org o-xxxxxxxxxx

aws_organizations_organization.org: Importing from ID "o-xxxxxxxxxx"...
data.aws_caller_identity.this: Reading...
data.aws_region.this: Reading...
data.aws_region.this: Read complete after 0s [id=ap-northeast-1]
data.aws_caller_identity.this: Read complete after 0s [id=xxxxxxx]
aws_organizations_organization.org: Import prepared!
  Prepared aws_organizations_organization for import
aws_organizations_organization.org: Refreshing state... [id=o-xxxxxxxxxx]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

Discussion