是否可以用一个AWS Lambda函数附加多个schedule/cron事件



目前,我的lambda函数成功地工作,并附带了一个调度事件

Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: HelloWorldFunction
Handler: helloworld.App::handleRequest
Runtime: java11
MemorySize: 512
Environment:
Variables:
PARAM1: VALUE
Events:
CronHourlyEvent: # This already works
Type: Schedule
Properties:
Description: Send John Doe
Enabled: True
Schedule: "cron(0 0/1 * * ? *)"
Input: !Sub '{"name": "John Doe"}'

Lambda在这里每一小时触发一次,它运行良好。

现在,我想添加另一个时间表事件,它每天中午12点触发一次相同的lambda。一种方法是创建一个单独的lambda,并将每日日程事件附加到它上,但我不想为此创建一个新的lambda。我希望我能在同一个lambda上附加两个时间表事件。

我在网上找不到任何一个将多个日程表事件附加到lambda的例子,但我认为需要在template.yaml文件中添加以下内容:

Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: HelloWorldFunction
Handler: helloworld.App::handleRequest
Runtime: java11
MemorySize: 512
Environment:
Variables:
PARAM1: VALUE
Events:
CronHourlyEvent: # this was already present
Type: Schedule
Properties:
Description: Send John Doe
Enabled: True
Schedule: "cron(0 0/1 * * ? *)"
Input: !Sub '{"name": "John Doe"}'
CronDailyEvent: # added this
Type: Schedule
Properties:
Description: Send Jane Doe
Enabled: True
Schedule: "cron(0 12 1/1 * ? *)"
Input: !Sub '{"name": "Jane Doe"}'

我想在本地测试它,所以我下载并配置了sam-sdk。但我不认为这支持在本地运行cron作业。我能够手动触发事件,但找不到任何基于提供的cron表达式自动运行调度作业的设置。

所以我想知道:

  1. 我们是否可以将2个或更多schedule/cron类型的事件附加到AWS lambda函数
  2. 如果是,我所做的代码更改是否正确

这将直接投入生产,我似乎无法找到在本地测试它的方法。

我可能会迟到一点,但对于任何寻找类似解决方案的人来说。只需为触发器使用不同的名称。

Events:
Serverless:
Type: Api
Properties:
Path: /serverless
Method: get
FirstConfigScheduledEvent:
Type: Schedule
Properties:
Schedule: cron(30 21 * * ? *)
SecondConfigScheduledEvent:
Type: Schedule
Properties:
Schedule: cron(30 23 * * ? *)

我使用的配置类似于下面的配置,它完美地工作并满足了我的需求。

Resources:
SayHelloWorldRule:
Type: AWS::Events::Rule
Properties:
Description: The rule for greeting the world
Name: ${self:provider.stage}-say-hello-world-rule
ScheduleExpression: 'cron(0 7 * * ? *)'
State: ENABLED
Targets:
-
Arn:
!Join [ ':', [ 'arn:aws:lambda', Ref: AWS::Region, Ref: AWS::AccountId, 'function', 'HelloWorldFunction' ] ]
Id: "GreetTheWorldDaily"
Input: '{"user": "Sudo user", "message": "Hello world!!!"}'
SayGoodbyeWorldRule:
Type: AWS::Events::Rule
Properties:
Description: "It's time to say goodbye"
Name: ${self:provider.stage}-say-goodbye-rule
ScheduleExpression: 'cron(30 22 * * ? *)'
State: ENABLED
Targets:
-
Arn:
!Join [ ':', [ 'arn:aws:lambda', Ref: AWS::Region, Ref: AWS::AccountId, 'function', 'HelloWorldFunction' ] ]
Id: "SayGoodbyeDaily"
Input: '{"user": "Sudo user", "message": "Auf Wiedersehen!!!"}'
PermissionForEventsToInvokeHelloWorldLambda:
Type: AWS::Lambda::Permission
Properties:
FunctionName: HelloWorldFunction
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"

无服务器文档确实提供了以下方面来创建多个cron计划-

# the example from our serverless.yml that runs every Monday at 03:15AM UTC
- schedule: cron(15 3 ? * MON *)
# run in 10-minute increments from the start of the hour on all working days
- schedule: cron(1/10 * ? * W *)
# run every day at 6:00PM UTC
- schedule: cron(0 18 * * ? *)

您可以为每个函数指定多个计划事件,以防您希望组合这些计划。在同一个函数上也可以组合rate和cron事件。

Source-无服务器

Serverless提供了实现这一目标的选项:

functions:
aggregate:
handler: statistics.handler
events:
- schedule:
rate: rate(10 minutes)
enabled: false
input:
key1: value1
key2: value2
stageParams:
stage: dev
- schedule:
rate: cron(0 12 * * ? *)
enabled: false
inputPath: '$.stageVariables'
- schedule:
rate: rate(2 hours)
enabled: true
inputTransformer:
inputPathsMap:
eventTime: '$.time'
inputTemplate: '{"time": <eventTime>, "key1": "value1"}'

参考:-https://www.serverless.com/framework/docs/providers/aws/events/schedule

相关内容

  • 没有找到相关文章

最新更新