Skip to content

Instantly share code, notes, and snippets.

@daaru00
Last active October 3, 2022 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daaru00/d5443e5de9b25fd7ee637e6d6a22d137 to your computer and use it in GitHub Desktop.
Save daaru00/d5443e5de9b25fd7ee637e6d6a22d137 to your computer and use it in GitHub Desktop.
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