こんにちは、SCSK石原です。
2022年7月20日にCloudFormationのイベント通知ができるようになったと発信がありました。

今回はこの機能を試してみました。
CFnイベントをメール通知(お試しVer)
EventBridgeでイベントを拾って、SNSにパブリッシュします。

構成手順を説明します。
SNSトピックの作成
Emailに通知するために、まずSNSトピックを作成します。
続いて、メール通知のために宛先がEmailのサブスクリプションを作成します。
サブスクリプション作成後はメールが来ますので、「confirm subscription」をクリックして有効化します。

EventBridgeルールの作成
今回のメインである、CloudFormationイベントを拾ってくるEventBridgeのルールを作成します。
任意のルール名でルールを作成します。
今回はCFnのイベントによってキックされるルールです。ルールタイプは「イベントパターン」を選択してください。
イベントパターンを定義します。CFnはAWSサービスですので、「AWSイベントまたはEventBridgeパートナーイベント」を選択します。
イベントパターンはCloudFormationを選択します。イベントタイプは目的に応じて選択してください。今回選択している「CloudFormation Stack Status Change」では、スタックのステータスが変わったタイミングでイベントがトリガーされます。
Managing events with AWS CloudFormation and Amazon EventBridge – AWS CloudFormation
最後に先ほど作成したSNSにパブリッシュする設定をします。
EventBridgeの設定は以上になります。
動作確認
適当なスタックを作成、削除したところ下記の4通メールが来ました。
| CREATE_IN_PROGRESS |
| CREATE_COMPLETE |
| DELETE_IN_PROGRESS |
| DELETE_COMPLETE |
CFnイベントをメール通知(実用的Ver)
ここまで読んでいただきありがとうございます。
たくさん環境あるし、GUIでポチポチするのはめんどくさいという方向けに、CFnテンプレートを置いておきます。
ご自由にご利用ください。
こちらは、メールの件名、メール内容を人が見やすいように加工する仕掛けも入れています。
AWSTemplateFormatVersion: "2010-09-09"
Description: "CFnEventNotify template"
Parameters:
NotifyEmailAddress:
Type: String
EventType:
Type: String
Default: "CFnEventNotify"
Resources:
# =====================================
# SNS
# =====================================
EventTypeNotifyTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: !Sub "${EventType}-${AWS::AccountId}"
TopicName: !Sub "Topic-${AWS::AccountId}-${EventType}"
EventTypeNotifyTopicPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument:
Statement:
- Effect: "Allow"
Principal:
Service: "events.amazonaws.com"
Action: "sns:Publish"
Resource: "*"
Topics:
- !Ref EventTypeNotifyTopic
Subscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint: !Ref NotifyEmailAddress
Protocol: email
TopicArn: !Ref EventTypeNotifyTopic
# =====================================
# EventBridge
# =====================================
CFnEventRule:
Type: AWS::Events::Rule
Properties:
Description: !Sub "${EventType}"
EventPattern:
detail-type:
- "CloudFormation Stack Status Change"
source:
- "aws.cloudformation"
Name: !Sub "${EventType}-Rule"
State: ENABLED
Targets:
- Arn: !Ref SubjectChangeStateMachine
Id: !Sub "${EventType}-StateMachine-Target"
RoleArn: !GetAtt StateMachineExecuteRole.Arn
InputTransformer:
InputPathsMap:
"stackid": "$.detail.stack-id"
"status": "$.detail.status-details.status"
"account": "$.account"
InputTemplate: |
{
"subject": "CFnEvent \"\"",
"message": " AWSアカウントID: \"\" \n スタック: \"\" \n ステータス: \"\""
}
# =====================================
# StepFunctions
# =====================================
StepFunctionToSNSPublishRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- states.amazonaws.com
Action: "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: StateMachine2SNSPublish
PolicyDocument:
Statement:
- Effect: Allow
Resource:
- !Ref EventTypeNotifyTopic
Action:
- sns:Publish
StateMachineExecuteRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- events.amazonaws.com
Action: "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: StateMachineExecute
PolicyDocument:
Statement:
- Effect: Allow
Resource:
- !Ref SubjectChangeStateMachine
Action:
- states:StartExecution
SubjectChangeStateMachine:
Type: "AWS::StepFunctions::StateMachine"
Properties:
DefinitionString: !Sub |-
{
"StartAt": "PublishSns",
"States": {
"PublishSns": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"TopicArn": "${EventTypeNotifyTopic}",
"Message.$": "$.message",
"Subject.$": "$.subject"
},
"End": true
}
}
}
RoleArn: !GetAtt StepFunctionToSNSPublishRole.Arn
終わりに
検証環境など、不特定多数のスタックが作成されるケースで通知設定しておけば、何か役に立つかもしれないですね。
また、メール通知ではなく何かのトリガーにも使えそうです。
現場からは以上です!







