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

Aws lambda 如何安排lambda函数每30分钟运行一次,aws-lambda,amazon-cloudformation,Aws Lambda,Amazon Cloudformation,我有一个兰姆达,上面有以下签名 func handler(ctx context.Context, cwe events.CloudWatchEvent) error { ... } func main() { lambda.Start(handler) } 。。。我正在尝试将其配置为每30分钟运行一次: AuthGcFunction: Type: AWS::Serverless::Function Properties: CodeUri:

我有一个兰姆达,上面有以下签名

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'})
    });
这段代码生成的云信息是

Resources:
  MyRuleA44AB831:
    Type: AWS::Events::Rule
    Properties:
      ScheduleExpression: cron(30 * * * ? *)
      State: ENABLED
您还可以使用计划表达式中的速率每30分钟运行一次

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

你能试试这个表达式并检查它是否适合你吗。

最后我的原始配置成功了。。。坦率地说,我不知道问题出在哪里。我猜在多次部署和重试之后,服务器卡住了。几个小时后,我成功部署。

您的计划事件及其cron表达式非常完美。可能是您忘了构建或其他什么?请尝试使用单引号:
“cron(0/30***?*)”
?尝试了单引号和双引号。。。但是我得到了同样的错误:-(刚刚尝试过,但不幸的是我得到了同样的错误。