AWS Control Tower のカスタマイズ(CfCT)がStackSetの削除に対応しました!

CfCTでStackSet削除ができるようになりました!
2022.10.03

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

AWS Control Tower のカスタマイズ(CfCT)のバージョンv2.5.0が公開されており、なんとStackSetsの削除に対応したと書かれていたので試してみました。

• Once opting into enable_stack_set_deletion by setting its value to true in the manifest, Removing a resource in its entirety from the manifest will delete the StackSet and all owned resources.

引用:Releases · aws-solutions/aws-control-tower-customizations

はじめに

Control Tower環境で独自のカスタマイズを追加する方法の1つとして、AWSから AWS Control Tower のカスタマイズ (CfCT) というソリューションが提供されています。

このソリューションは独自の定義ファイル(manifest.yaml)を定義することでStackSetsやSCPをコード上で管理できるソリューションです。併せてAWS Control Towerのアカウント発行を検知し自動で適用してくれるという機能も持っています。

参考:アーキテクチャの概要 - AWS Control Tower

これまでは、manifest.yamlで定義したStackSetsのリソースを削除する際は、コード上から消すだけでは削除できなかったためコンソール等から手動で対応が必要でした。

これが今回のアップデートで対応したようなので試してみます。

CfCT(v2.5.0)へアップデート

まずはCfCTのアップデートを行います。v2.5.0のテンプレートは以下Github上にありますので、クローンしてダウンロードしましょう。

https://github.com/aws-solutions/aws-control-tower-customizations/blob/main/customizations-for-aws-control-tower.template

私の環境では既存でCfCT(v2.3.0)を展開していたので、CloudFormationスタックから「CustomizationsForCTSolution」を選択して更新します。

既存テンプレートを置き換えて、先ほどダウンロードした新しいテンプレート「customizations-for-aws-control-tower.template」をアップロードします。

そのほかのパラメータは既存と同様にして、スタックの更新をしましょう。念のため、レビュー画面でバージョンがv2.5.0であることは確認しておきます。

数分程度でアップデートが完了します。

StackSetsのスタックを削除してみる

前提ですが、manifest.yamlのバージョンがversion: 2021-03-15となっていることを確認しましょう。古いバージョンの場合はアップデートする必要があります。

manifest.yamlにenable_stack_set_deletionのオプションを追加することで、そのファイル内で定義されているStackSetsのリソースを削除するかコントロールできます。今回は既に展開されているStackSetsにenable_stack_set_deletion: trueを追加して試してみます。

既存のStackSetsは以下のようなスタックが含まれています。

  • ターゲットOU
    • Sandbox
    • Security
  • リージョン
    • 東京(ap-northeast-1)
    • 大阪(ap-northeast-3)

./manifest.yaml

---
region: ap-northeast-1 # Control Tower Home Region
version: 2021-03-15
enable_stack_set_deletion: true #追加

resources:
  - name: add-create-s3bucket
    description: Control Tower Custom CloudFormation Resources - Add S3 Bucket
    resource_file: template/CreateS3Bucket.yaml
    deploy_method: stack_set
    deployment_targets:
      organizational_units:
        - Sandbox
        - Security
    regions:
      - ap-northeast-1
      - ap-northeast-3

StackSetsで展開しているのは単純なS3バケットを作成するCFnテンプレートです。

./template/CreateS3Bucket.yaml

AWSTemplateFormatVersion: "2010-09-09"

Resources:
  S3Bucket:
    Type: "AWS::S3::Bucket"
    Properties:
      BucketName: !Sub aws-cfct-test-bucket-${AWS::AccountId}-${AWS::Region}

私の環境ではCodeCommitでCfCTのコードを管理しているため、manifest.yamlにenable_stack_set_deletionを追加してPushしました。数分でCodePipelineが完了したので、実際にコードから削除したターゲットのリソースが削除されるのか確認してみます。

今展開されているスタックインスタンスは3アカウント×2リージョンで合計6スタックです。

リージョンを削除

まずはリージョンのみを変更して削除されるか確認してみます。manifest.yamlから大阪リージョンの定義を削除しました。

./manifest.yaml

---
region: ap-northeast-1 # Control Tower Home Region
version: 2021-03-15
enable_stack_set_deletion: true #追加

resources:
  - name: add-create-s3bucket
    description: Control Tower Custom CloudFormation Resources - Add S3 Bucket
    resource_file: template/CreateS3Bucket.yaml
    deploy_method: stack_set
    deployment_targets:
      organizational_units:
        - Sandbox
        - Security
    regions:
      - ap-northeast-1
      #- ap-northeast-3 削除

結果大阪リージョンに展開されているスタックインスタンスが、全て削除されていることが確認できました。

OUを削除

次にターゲットOUをコードから削除してみます。Security OUを削除してPushしてみました。

./manifest.yaml

---
region: ap-northeast-1 # Control Tower Home Region
version: 2021-03-15
enable_stack_set_deletion: true

resources:
  - name: add-create-s3bucket
    description: Control Tower Custom CloudFormation Resources - Add S3 Bucket
    resource_file: template/CreateS3Bucket.yaml
    deploy_method: stack_set
    deployment_targets:
      organizational_units:
        - Sandbox
        # - Security 削除
    regions:
      - ap-northeast-1
      # - ap-northeast-3 削除

結果、Security OUに含まれていた2アカウントのスタックが削除されていることが確認できました。

スクリーンショット 2022-10-03 14.35.33.png

StackSetを削除

最後に定義しているStackSetを丸々削除してみます。定義している部分を全てコメントアウトしてPushしてみました。

./manifest.yaml

---
region: ap-northeast-1 # Control Tower Home Region
version: 2021-03-15
enable_stack_set_deletion: true

resources:
  # - name: add-create-s3bucket
  #   description: Control Tower Custom CloudFormation Resources - Add S3 Bucket
  #   resource_file: template/CreateS3Bucket.yaml
  #   deploy_method: stack_set
  #   deployment_targets:
  #     organizational_units:
  #       - Sandbox
  #       # - Security
  #   regions:
  #     - ap-northeast-1
  #     # - ap-northeast-3 削除

結果、StackSets自体が削除されたためスタックも全て削除されていました。

StackSet自体が削除されているので、 削除したStackSet名で検索しても削除されているため当然見つかりません。

3パターン試してみましたが、manifest.yamlのコードで定義した通りリソースが削除されました!

SCPはまだ削除に未対応なので注意

今回のv2.5.0のアップデートでStackSetの削除に対応しましたが、SCPについてはまだ未対応となっています。Issueも作成されていたので、後々対応してくれることを期待しましょう!

CfCTでSCPを扱うときの詳細は以下ブログをご参照ください。

おわりに

待ちに待ったStackSetsの削除機能が追加されて非常に嬉しいアップデートでした。ようやく直感的にCfCTからStackSetsの操作ができるようになったので、これまで以上におすすめできるソリューションになりましたね。

Control Towerを使っている方で、独自にカスタマイズしてリソースを展開したい場合はぜひCfCTを検討してみてください。

参考