Skip to content

Instantly share code, notes, and snippets.

@daaru00
Last active Oct 3, 2022
Embed
What would you like to do?
Use EventBridge events to execute code when the CloudFormation stack itself is created
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Resources:
LogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${StackCreatedFunction}
RetentionInDays: 7
StackCreatedFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "${AWS::StackName}-created"
Handler: index.handler
Runtime: nodejs16.x
InlineCode: !Sub |-
exports.handler = async (event) => {
console.log(`Stack ${AWS::StackName} created!`)
}
Events:
StackEvent:
Type: EventBridgeRule
Properties:
EventBusName: default
Pattern:
source:
- aws.cloudformation
detail-type:
- CloudFormation Stack Status Change
detail:
stack-id:
- !Ref AWS::StackId
status-details:
status:
- CREATE_COMPLETE
Outputs:
LogGroupName:
Description: "The name of the lambda function log group"
Value: !Ref LogGroup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment