以前「TerraformでAWSアカウントを作るのは簡単だけど削除はひと手間かかる」というブログを書きました。
タイトル通り、TerraformでAWS Organizations配下のアカウントを作成するのはaws_organizations_account
リソースを定義してterraform apply
するだけなのでチョー簡単ですが、削除はaws_organizations_account
リソースを削除してterraform apply
するだけではだめで、色々設定が必要になって大変だという内容です。
ですが、少し前に激アツアップデートがありました。
そしてこの簡単削除機能がTerraformでも利用可能になっていることを知ったのでレポートします。
close_on_deletion flag
Terraform AWS Provider v4.9.0でaws_organizations_account
リソースに追加されたclose_on_deletion
がそれです。デフォルト値はfalseで、falseだと従来どおりこのリソースを削除した際にアカウントは削除されず、Oraganizationから当該アカウントを除外(離脱)させるだけです。
一方、この値をtrueにすると、Oraganizationから当該アカウントを除外(離脱)させる代わりに、削除(閉鎖)させることができます。削除(閉鎖)の場合は除外(離脱)の場合とは違い事前に色々面倒な設定は不要です。
それぞれの挙動をマネジメントコンソールのボタンの挙動と対応付けると以下になります。
やってみた
AWS Providerのアップグレード
required_providers {
aws = {
source = "hashicorp/aws"
- version = "~> 3.11"
+ version = "~> 4.9"
}
}
% terraform init
※ Terraform本体のVersionが0.13.5とちょっと古かったので上記コマンドですが、 0.14以降だとterraform init -upgrade
にしてロックファイルの更新が必要なはずです。
close_on_deletion flag追加してapply
resource aws_organizations_account account2 {
name = "account2"
email = "xxxxxxx+account2@gmail.com"
parent_id = aws_organizations_organizational_unit.ou1.id
+ close_on_deletion = true
}
% terraform apply
(略)
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
# module.org.aws_organizations_account.account2 will be updated in-place
~ resource "aws_organizations_account" "account2" {
arn = "arn:aws:organizations::111111111111:account/o-bifgl0yq6w/123456789012"
+ close_on_deletion = true
email = "xxxxxxx+account2@gmail.com"
id = "123456789012"
joined_method = "CREATED"
joined_timestamp = "2021-08-22T07:19:57Z"
name = "account2"
parent_id = "ou-4zwg-abcdefg"
status = "ACTIVE"
tags = {}
tags_all = {}
}
Plan: 0 to add, 1 to change, 0 to destroy.
リソース削除してapply
-resource aws_organizations_account account2 {
- name = "account2"
- email = "xxxxxxx+account2@gmail.com"
- parent_id = aws_organizations_organizational_unit.ou1.id
- close_on_deletion = true
-}
% terraform apply
(略)
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# module.org.aws_organizations_account.account2 will be destroyed
- resource "aws_organizations_account" "account2" {
- arn = "arn:aws:organizations::111111111111:account/o-bifgl0yq6w/123456789012" -> null
- close_on_deletion = true -> null
- email = "xxxxxxx+account2@gmail.com" -> null
- id = "123456789012" -> null
- joined_method = "CREATED" -> null
- joined_timestamp = "2021-08-22T07:19:57Z" -> null
- name = "account2" -> null
- parent_id = "ou-4zwg-abcdefg" -> null
- status = "ACTIVE" -> null
- tags = {} -> null
- tags_all = {} -> null
}
Plan: 0 to add, 0 to change, 1 to destroy.
マネジメントコンソールで確認すると該当アカウントのステータスが「停止」になっていました。(90日以内であればAWSサポート経由で復旧ができます)
以上、TerraformでもAWSアカウントを簡単に削除できるようになったことをお伝えしました。もともとやりたかった「検証アカウントをサクッと作って、検証終わったらサクッと消す」ができるようになってとても嬉しいです。