简体   繁体   English

将参数传递给 AWS Cloudwatch 事件目标 lambda function

[英]Pass parameters to AWS Cloudwatch event target lambda function

I want to pass parameters to my lambda function invoked by AWS Cloudwatch events.我想将参数传递给由 AWS Cloudwatch 事件调用的 lambda function。 The parameter name is alarmActions and my CFT template for the event rule is as follows:参数名称是alarmActions ,我的事件规则 CFT 模板如下:

"LambdaInvokeScheduler": {
            "Type": "AWS::Events::Rule",
            "Properties": {
              "Description": "Scheduled Rule for invoking lambda function",
              "EventPattern": {
                "source": [
                  "aws.ecs"
                ],
                "detail-type": [
                  "ECS Container Instance State Change"
                ],
                "detail": {
                  "clusterArn": [
                    { "Fn::GetAtt": ["WindowsCluster", "Arn"] }
                  ]
                }
              },
              "State": "ENABLED",
              "Targets": [{
                "Arn": { "Fn::GetAtt": ["AlarmCreationLambdaFunction", "Arn"] },
                "Id": "AlarmCreationLambdaFunction",
                "Input": { "Fn::Join" : ["", [ "{ \"alarmActions\": \"", { "Fn::Join" : [":", [ "arn:aws:sns", { "Ref" : "AWS::Region" }, { "Ref" : "AWS::AccountId" }, "CloudWatch"]] }, "\" }"]] }
              }]
            }
          }

I have used the Input parameter to pass a JSON text.我使用Input参数传递了 JSON 文本。 There is not much documentation around it.没有太多关于它的文档。 I just wanted to find the right way to do it.我只是想找到正确的方法来做到这一点。

I found the solution.我找到了解决方案。 I was referring the parameter in lambda in a wrong way.我以错误的方式引用了 lambda 中的参数。

My lambda function was like this:我的 lambda function 是这样的:

def func(event, context, alarmActions)
{
   print(alarmActions)
}

It worked when i made the following update:当我进行以下更新时它起作用了:

def func(event, context)
{
   print(event['alarmActions'])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM