CloudFormationの削除保護はNested Stackも保護します

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

ウィスキー、シガー、パイプをこよなく愛する大栗です。

先程CloudFormationの削除保護機能がリリースされましたが、Nested Stackの場合も対応しているので試してみました。

AWS CloudFormation Now Provides Stack Termination Protection

Protecting a Stack From Being Deleted

[新機能] CloudFormationのStackに削除保護が設定可能になりました

Nested Stackの場合

Nested Stackとは、あるCloudFormationのテンプレートの中で別のCloudFormationテンプレートを呼出して構成するものを指します。つまりCloudFormationの入れ子構造(Nested)になっているStackです。

Nested Stackでは、呼び出される方のNested Stackでは削除保護は設定できずに、呼び出す方のRoot Stackの削除保護設定を受け継ぎます。つまりRoot Stackの設定で全体の削除保護が行われます。

やってみた

以下のテンプレートを例としてNested Stackとして構築します。

Stack01.yamlがRoot Stack(呼ぶ側)で、Server01.yamlがNested Stack(呼ばれる側)です。

Stack01.yaml

AWSTemplateFormatVersion: 2010-09-09
Description: Linux Stack
Parameters:
  ImageId:
    Type: String
    Default: ImageId
  KeyPairName:
    Type: String
    Default: KeyName
Resources:
  Stack01:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://s3-ap-northeast-1.amazonaws.com/cf-templates-a1b2c3d4e5f6-region/Server01.yaml
      Parameters:
        ImageId: !Ref ImageId
        KeyPairName: !Ref KeyPairName

Server01.yaml

AWSTemplateFormatVersion: 2010-09-09
Description: Linux Stack
Parameters:
  ImageId:
    Type: String
    Default: ImageId
  KeyPairName:
    Type: String
    Default: KeyName
Resources:
  Server01:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref ImageId
      InstanceType: c4.large
      KeyName: !Ref KeyPairName

ここでは、Stack構築時は削除保護をせずに普通に起動します。

この様に2個のStackができます。

CloudFormation_マネジメントコンソール

Nested Stackの概要を確認すると、削除保護は『ルートスタックで無効』となっています。

CloudFormation_マネジメントコンソール

メニューから削除保護の変更をクリックしても、Root Stackで変更する旨の表示が出ます。

CloudFormation_マネジメントコンソール

Root Stackで削除保護を有効化します。

CloudFormation_マネジメントコンソール

Root Stackの削除保護が有効になります。

CloudFormation_マネジメントコンソール

Nested Stackの削除保護を確認するとルートスタックで有効と表示されます。

CloudFormation_マネジメントコンソール

Nested Stackを削除しようとしても削除できません。

CloudFormation_マネジメントコンソール

CLIで試しても同様に削除できません。

$ aws cloudformation delete-stack --stack-name DeleteProtection-Stack01-1771JGVWU2FD6

An error occurred (ValidationError) when calling the DeleteStack operation: Stack [DeleteProtection-Stack01-1771JGVWU2FD6] cannot be deleted while TerminationProtection is enabled

補足

AWS CLIで削除保護の設定を行うにはaws cloudformation update-termination-protectionコマンドを使用します。

削除保護の有効時には--enable-termination-protectionオプションを設定します。

$ aws cloudformation update-termination-protection --enable-termination-protection --stack-name DeleteProtection
{
    "StackId": "arn:aws:cloudformation:ap-northeast-1:123456789012:stack/DeleteProtection/a1b2c3d4-a1b2-11e7-b29c-50a68669988e"
}

削除保護の無効時には--no-enable-termination-protectionオプションを設定します。

$ aws cloudformation update-termination-protection --no-enable-termination-protection --stack-name DeleteProtection
{
    "StackId": "arn:aws:cloudformation:ap-northeast-1:123456789012:stack/DeleteProtection/a1b2c3d4-a1b2-11e7-b29c-50a68669988e"
}

さいごに

大規模な構成をCloudFormationで構築する場合は、Nested Stackを使う場合があると思います。Root Stackで削除保護を設定すれば環境全ての削除保護が行えるので作業ミスなどによる環境破壊などを抑制できるようになりました。