aws-lambda - 如何安排 lambda 函数每 30 分钟运行一次

标签 aws-lambda aws-cloudformation

我有一个具有以下签名的 lambda...

func handler(ctx context.Context, cwe events.CloudWatchEvent) error {

    ...
}

func main() {
    lambda.Start(handler)
}

...我正在尝试将其配置为每 30 分钟运行一次:

  AuthGcFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/auth/gc
      Handler: gc
      Runtime: go1.x
      Tracing: Active
      Policies:
      - DynamoDBCrudPolicy:
        TableName: !Ref AuthInfoTable
      Events:
        ScheduledEvent:
          Type: Schedule
          Properties:
            Schedule: cron(0/30 * * * ? *)
            Description: "Collects unconfirmed, expired auth entries"
            Enabled: true
      Environment:
        Variables:
          DYNAMODB_AUTH_INFO_TABLE: !Ref AuthInfoTable
          AUTH_EXPIRES_IN: !Ref AuthExpiresIn

当我尝试部署它(使用 sam)时,我总是收到以下错误:

CREATE_FAILED             AWS::Events::Rule         AuthGcFunctionScheduled   Parameter
                                                    Event                     ScheduleExpression is  not valid. (Service:
                                                                              AmazonCloudWatchEvents;  Status Code: 400; Error
Code:
                                                                              ValidationException;
                                                                              Request ID: f32770c3-be
                                                                              cd-48a8-943f-f339103869
                                                                              3d; Proxy: null)

阅读 AWS 文档后,我的 cron 表达式似乎是正确的...所以我想知道是否还有其他问题。

最佳答案

我从下面的 CDK 代码创建了事件桥规则

new Rule(this, 'MyRule', {
    schedule: Schedule.cron({minute: '30'})
});

这段代码生成的cloudformation是

Resources:
  MyRuleA44AB831:
    Type: AWS::Events::Rule
    Properties:
      ScheduleExpression: cron(0/30 * * * ? *)
      State: ENABLED

您还可以在计划表达式中使用速率每 30 分钟运行一次。

Resources:
  MyRuleA44AB831:
    Type: AWS::Events::Rule
    Properties:
      ScheduleExpression: rate(30 minutes)
      State: ENABLED

您可以尝试一下这个表达式并检查它是否适合您。

关于aws-lambda - 如何安排 lambda 函数每 30 分钟运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66663272/

相关文章:

java - 如何使用 aws cli 创建 java aws lambda 函数?

python-3.x - Vault 用 python 读取 secret

amazon-web-services - 我们如何检索 ec2 客户端标签

aws-cloudformation - 使用cloudformation安装fail2ban

amazon-web-services - AWS 云信息

node.js - Amazon Lex 机器人 - 回复您好

java - 使用 AWS Java Sdk 的 AWS Lambda 和 S3 集成

node.js - nodejs Lambda函数中的FFMPEG意外行为

json - 需要使用一个 AWS CloudFormation 模板为不同环境启动多个实例

aws-lambda - CloudFormation 自定义资源未完成删除