我有一个具有以下签名的 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/