我想按照固定的时间表运行 Lambda 任务,并通过触发该任务的 CloudWatch 计划事件将一些配置传递给它。查看文档这里,我可以看到 Cloudwatch Scheduled Events 如下所示:
{
"id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa",
"detail-type": "Scheduled Event",
"source": "aws.events",
"account": "123456789012",
"time": "2015-10-08T16:53:06Z",
"region": "us-east-1",
"resources": [ "arn:aws:events:us-east-1:123456789012:rule/MyScheduledRule" ],
"detail": {}
}
我的 Lambda 任务是用 Go 编写的,我可以看到它event.CloudWatchEvent
允许我访问事件detail
字段中的 JSON,但我不知道如何使用一些自定义详细信息创建 Cloudwatch 计划事件规则。有什么想法吗?
答案1
# This is for AWS Sam CF
MyScheduledRule:
Type: "AWS::Events::Rule"
Properties:
Description: "ScheduledRule"
ScheduleExpression: "rate(6 minutes)"
State: "ENABLED"
Targets:
- Arn:
!GetAtt MyLambdaFunction.Arn
Id: "IdForAlert"
Input: "{\"name\": \"value\"}"
答案2
这不是一个完美的答案,但我最终使用环境变量与 Lambda 任务进行了沟通。
https://docs.aws.amazon.com/lambda/latest/dg/env_variables.html