Fire scheduled lambda function after cloudformation stack creation

477 Views Asked by At

This schedules my cloudformation lambda to run once per day but I'd like it to fire upon creation one time.

Transform: AWS::Serverless-2016-10-31
...
EventListFunction:
  Type: 'AWS::Serverless::Function'
  Properties:
    ...
    Events:
      Schedule1:
        Type: Schedule
        Properties:
          Schedule: rate(1 day)
2

There are 2 best solutions below

0
On BEST ANSWER

Here are a few options:

  1. Manually create an SNS Topic. Add an AWS::SNS::Subscription to your stack with the lambda function as the Endpoint and the SNS topic as the TopicArn. On stack creation/update, configure Stack Event Notifications to be sent to this SNS topic.

  2. Add a Custom Resource referencing the newly-created function, which will call the function on creation. In order for the Custom Resource creation to complete and not cause a rollback in your stack, you will need to adapt your Lambda function to support the CloudFormation request/response format (see Custom Resource Reference). Also note that the function will also be called again on stack deletion, and this will also need to be handled.

  3. Add the Lambda function to the Stack Outputs, then write a simple script that performs both the stack update and invokes the Lambda function after completion.

0
On

Look at AWS:: CloudFormation::CustomResource to call it