每天安排一次 lambda 函数

sha*_*nuo 3 aws-cloudformation

我有一个按预期工作的云信息模板。它安装 python lambda 函数。

https://github.com/shantanuo/easyboto/blob/master/install_lambda.txt

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

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

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

Ped*_*tes 5

我使用的CloudFormation 模板示例:

  # 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
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看有关 Rate 和 Cron 表达式的信息。

但是,如果您想每天上午 07:00 (UTC) 运行上述作业一次,那么表达式可能应该是:cron(0 7 * * ? *)