Quantcast
Channel: kakakakakku blog
Viewing all articles
Browse latest Browse all 920

CloudFormation スタックリファクタリング: リソースを別のスタックに移動しよう

$
0
0

2025年2月6日に AWS CloudFormation の新機能「Stack Refactoring(スタックリファクタリング)」がリリースされた👏

aws.amazon.com

既に「同じ AWS CloudFormation スタックでリソースの論理 ID を変更する」は試してまとめてある📝 今回は「別の AWS CloudFormation スタックにリソースを移動する」を試す \( 'ω')/

kakakakakku.hatenablog.com

シナリオ

今回は app-stack(アプリケーションスタック)に Amazon CloudWatch Logs ロググループと Amazon SNS トピックを定義しておいて,途中から monitoring-stack(モニタリングスタック)に Amazon SNS トピックを移動したくなったというシナリオを試す❗️

準備

👾 app-stack/template.yaml

まずは Amazon CloudWatch Logs ロググループと Amazon SNS トピックを定義する.

AWSTemplateFormatVersion: 2010-09-09

Resources:Logs:Type: AWS::Logs::LogGroup
    Properties:LogGroupName: cfn-stack-refactoring-reorganize-resources
  Topic:Type: AWS::SNS::Topic
    Properties:TopicName: alerts
      Subscription:- Protocol: email
          Endpoint: y.yoshida22@gmail.com

そして aws cloudformation deployコマンドを使って AWS CloudFormation スタックをデプロイする.

$ aws cloudformation deploy \--stack-name cfn-stack-refactoring-reorganize-resources-app \--template-file ./app-stack/template.yaml

リファクタリング

スタックリファクタリングではリファクタリング後の AWS CloudFormation テンプレートも必要になる.デプロイ済の app-stack/template.yamlからは Amazon SNS トピックを削除して,新しく作った monitoring-stack/template.yamlにそのまま移動した👌

👾 app-stack/template.yaml

AWSTemplateFormatVersion: 2010-09-09

Resources:Logs:Type: AWS::Logs::LogGroup
    Properties:LogGroupName: cfn-stack-refactoring-reorganize-resources

👾 monitoring-stack/template.yaml

ちなみに既存の AWS CloudFormation スタックに移動することもできるし,新しく AWS CloudFormation スタックを作ることもできる.今回は新しく作る👌

AWSTemplateFormatVersion: 2010-09-09

Resources:Topic:Type: AWS::SNS::Topic
    Properties:TopicName: alerts
      Subscription:- Protocol: email
          Endpoint: y.yoshida22@gmail.com

👾 refactor.json

次にリファクタリングの対象を指定する refactor.jsonを作る.今回は別の AWS CloudFormation スタックになるため StackNamecfn-stack-refactoring-reorganize-resources-appから cfn-stack-refactoring-reorganize-resources-monitoringにして,LogicalResourceIdを同じ Topicにしている👌

[{"Source": {"StackName": "cfn-stack-refactoring-reorganize-resources-app",
      "LogicalResourceId": "Topic"
    },
    "Destination": {"StackName": "cfn-stack-refactoring-reorganize-resources-monitoring",
      "LogicalResourceId": "Topic"
    }}]

実行する

スタックリファクタリングを実行する前に,まず aws cloudformation create-stack-refactorコマンドを使ってスタックリファクタリング ID を採番する.オプションとしては大きく3つ指定した👌

  • --stack-definitions: app-stack と monitoring-stack のスタック名と AWS CloudFormation テンプレート名を指定する
  • --enable-stack-creation: 新しく AWS CloudFormation スタックを作る
  • --resource-mappings: 作成した refactor.jsonを指定する
$ aws cloudformation create-stack-refactor \--stack-definitions\StackName=cfn-stack-refactoring-reorganize-resources-app,TemplateBody@=file://app-stack/template.yaml  \StackName=cfn-stack-refactoring-reorganize-resources-monitoring,TemplateBody@=file://monitoring-stack/template.yaml  \--enable-stack-creation\--resource-mappings file://refactor.json
{"StackRefactorId": "8f7c7897-1dd9-4c69-9734-38f82e49b691"}

awscli.amazonaws.com

次は aws cloudformation describe-stack-refactorコマンドを使ってステータスを確認する.問題なければ以下のようになる.

$ aws cloudformation describe-stack-refactor \--stack-refactor-id 8f7c7897-1dd9-4c69-9734-38f82e49b691
{"StackRefactorId": "8f7c7897-1dd9-4c69-9734-38f82e49b691",
    "StackIds": ["arn:aws:cloudformation:ap-northeast-1:000000000000:stack/cfn-stack-refactoring-reorganize-resources-app/ad998c10-e8c4-11ef-88f2-0658614ed86d",
        "arn:aws:cloudformation:ap-northeast-1:000000000000:stack/cfn-stack-refactoring-reorganize-resources-monitoring/e9d68980-e8c4-11ef-98cf-0a285e344667"],
    "ExecutionStatus": "AVAILABLE",
    "Status": "CREATE_COMPLETE"}

awscli.amazonaws.com

最後は aws cloudformation execute-stack-refactorコマンドを使ってスタックリファクタリングを実行すれば完了👌

$ aws cloudformation execute-stack-refactor \--stack-refactor-id 8f7c7897-1dd9-4c69-9734-38f82e49b691

awscli.amazonaws.com

実行結果を確認する

おおお〜 \( 'ω')/

関連記事

aws.amazon.com


Viewing all articles
Browse latest Browse all 920