EventBridge CloudFormation Events

Fabio Gollinucci
1 min readJun 6, 2023

--

It’s possible to use EventBridge event “CloudFormation Resource Status Change” to execute code when the CloudFormation stack itself is created.

AWS Infrastructure schema

This can be done describing the EventBridge event rule for a Lambda function that match the emitted event:

PostScript:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "${AWS::StackName}-post-script"
Runtime: nodejs16.x
Handler: index.handler
InlineCode: !Sub |
exports.handler = async (event) => {
console.log(event);
};
Policies:
- AWSLambdaExecute
Events:
StackCreatedOrUpdated:
Type: EventBridgeRule
Properties:
EventBusName: default
Pattern:
source:
- aws.cloudformation
detail-type:
- CloudFormation Stack Status Change
detail:
stack-id:
- !Ref AWS::StackId
status-details:
status:
- UPDATE_COMPLETE
- CREATE_COMPLETE

The event rule pattern is interpolated with AWS::StackId pseudo parameter reference. So far the only status change that is reported is CREATE_COMPLETE.

This approach can be used for executing a script when a stack resource is create, update or removed:

PostResourceScript:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "${AWS::StackName}-post-resource-script"
Runtime: nodejs16.x
Handler: index.handler
InlineCode: !Sub |
exports.handler = async (event) => {
console.log(event);
};
Policies:
- AWSLambdaExecute
Events:
ResourceUpdate:
Type: EventBridgeRule
Properties:
EventBusName: default
Pattern:
source:
- aws.cloudformation
detail-type:
- CloudFormation Resource Status Change
detail:
stack-id:
- !Ref AWS::StackId
resource-type:
- AWS::EC2::Instance
logical-resource-id:
- Instance

Credits: Cloudcraft.

Originally written on Oct 3, 2022.

--

--