2025年2月6日に AWS CloudFormation の新機能「Stack Refactoring(スタックリファクタリング)」がリリースされた👏
既に「同じ AWS CloudFormation スタックでリソースの論理 ID を変更する」は試してまとめてある📝 今回は「別の AWS CloudFormation スタックにリソースを移動する」を試す \( 'ω')/
シナリオ
今回は 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 スタックになるため StackName
は cfn-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"}
次は 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"}
最後は aws cloudformation execute-stack-refactor
コマンドを使ってスタックリファクタリングを実行すれば完了👌
$ aws cloudformation execute-stack-refactor \--stack-refactor-id 8f7c7897-1dd9-4c69-9734-38f82e49b691
実行結果を確認する
おおお〜 \( 'ω')/