AWS Cloudwatch事件规则-调用Lambda-具有参数 [英] AWS Cloudwatch Event Rule - Invoke Lambda - with Parameter
问题描述
我正在使用AWS Clouwatch事件规则来调用基于Cron Schedule的python lambda函数,该函数运行良好,现在我可以使用AWS Cloudformation从Cloudwatch甚至是规则中将参数传递给此lambda函数吗?您能指导一下吗?请在下面查看我的cfn模板:
I am using AWS Clouwatch Event Rule to invoke a python lambda function based on Cron Schedule which is working fine.Now can I pass parameter into this lambda function from cloudwatch even rule using AWS Cloudformation? Could you please guide? Please see below my cfn template :
Step1 : parameter.Schedule=cron(0 21 ? * * *)
Step 2: "Schedule": {
"Description": "Schedule for the Lambda function (cron or rate)",
"Type": "String"
},
Step 3: "funcInvokeRule": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": {"Ref": "Schedule"},
"Targets": [{
"Id": "funcScheduler",
"Arn": {"Fn::GetAtt": ["Function","Arn"]}
}]
}
},
推荐答案
Following the AWS docs, your cloudformation resource could be as simple as:
Resources:
EventRule:
Type: AWS::Events::Rule
Properties:
Name: {EVENTNAME}
Description: "ScheduledRule"
ScheduleExpression: cron(0 21 ? * * *)
State: "ENABLED"
RoleArn: {ROLE}
用您自己的值替换名称和 RoleArn .
注意:名称不是必需的参数,但确实有助于标识您的资源.但是,根据文档,如果您替换cloudformation模板中的资源,则需要指定一个新名称.
If you were then also going to use cloudformation for your lambda using severless, personally I would then attached the rule to the lambda via permissions, that way you can attach up to 5 triggers on the rule without modifying the rule targets every time. e.g.
Lambda:
Type: AWS::Serverless::Function
Properties:
FunctionName:{LAMBDANAME}
Description: {Description}
Role: {Role}
Handler: {FileName}.lambda_handler
Runtime: {x}
CodeUri: {ObjectPath}
MemorySize: {x}
Timeout: {x}
Lambdatrigger:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref Lambda
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !Ref EventRule
这篇关于AWS Cloudwatch事件规则-调用Lambda-具有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!