Amazon cloudformation 每天安排一次lambda功能

Amazon cloudformation 每天安排一次lambda功能,amazon-cloudformation,Amazon Cloudformation,我有一个cloudformation模板,可以按预期工作。它安装python lambda函数 但是我如何每天运行一次函数呢?我知道yaml代码看起来像这样 NotifierLambdaScheduledRule: Type: AWS::Events::Rule Properties: Name: 'notifier-scheduled-rule' Description: 'Triggers notifier lambda once per day

我有一个cloudformation模板,可以按预期工作。它安装python lambda函数

但是我如何每天运行一次函数呢?我知道yaml代码看起来像这样

  NotifierLambdaScheduledRule:
    Type: AWS::Events::Rule
    Properties:
      Name: 'notifier-scheduled-rule'
      Description: 'Triggers notifier lambda once per day'
      ScheduleExpression: cron(0 7 ? * * *)
      State: ENABLED

换句话说,我如何将cron设置集成到我的cloudformation模板中?

其他人可以为您提供一个使用Lambda而不使用无服务器的工作示例。但是,如果您将无服务器转换与AWS Cloudformation(基本上是SAM-Serverless应用程序模型)结合使用,则可以非常轻松地调度lambda

例如:

  ServerlessTestLambda:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src
      Handler: test-env-var.handler
      Role: !GetAtt BasicLambdaRole.Arn
      Environment:
        Variables:
          Var1: "{{resolve:ssm:/test/ssmparam:3}}"
          Var2: "Whatever You want"
      Events:
        LambdaSchedule:
          Type: Schedule
          Properties:
            Schedule: rate(3 minutes)
这个lambda会每3分钟触发一次

更多信息:

我使用的一个示例:

  # Cronjobs
  ## Create your Lambda
  CronjobsFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: FUNCTION_NAME
      Handler: index.handler
      Role: !GetAtt LambdaExecutionRole.Arn
      Code:
        S3Bucket: !Sub ${S3BucketName}
        S3Key: !Sub ${LambdasFileName}
      Runtime: nodejs8.10
      MemorySize: 512
      Timeout: 300

  ## Create schedule
  CronjobsScheduledRule:
    Type: AWS::Events::Rule
    Properties:
      Description: Scheduled Rule
      ScheduleExpression: cron(0 7 ? * * *)
      # ScheduleExpression: rate(1 day)
      State: ENABLED
      Targets:
        - Arn: !GetAtt CronjobsFunction.Arn
          Id: TargetFunctionV1

  ## Grant permission to Events trigger Lambda
  PermissionForEventsToInvokeCronjobsFunction:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref CronjobsFunction
      Action: lambda:InvokeFunction
      Principal: events.amazonaws.com
      SourceArn: !GetAtt CronjobsScheduledRule.Arn

  ## Create Logs to check if events are working
  CronjobsFunctionLogsGroup:
    Type: AWS::Logs::LogGroup
    DependsOn: CronjobsFunction
    DeletionPolicy: Delete
    Properties:
      LogGroupName: !Join ['/', ['/aws/lambda', !Ref CronjobsFunction]]
      RetentionInDays: 14
您可以查看速率和Cron表达式。

您可以在中找到它。请参见
目标