创建 cloudformation 堆栈后触发计划的 lambda 函数

Fire scheduled lambda function after cloudformation stack creation

这将我的 cloudformation lambda 安排为每天 运行 一次,但我希望它在创建时触发一次。

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

看看AWS::CloudFormation::CustomResource调用它

这里有几个选项:

  1. 手动 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。在堆栈 creation/update 上,配置要发送到此 SNS 主题的堆栈事件通知。

  2. 加一个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)。另请注意,该函数也会在堆栈删除时再次调用,这也需要处理。

  3. 将 Lambda 函数添​​加到堆栈输出,然后编写一个简单的脚本来执行堆栈更新并在完成后调用 Lambda 函数。