简体   繁体   中英

Pass parameters to AWS Cloudwatch event target lambda function

I want to pass parameters to my lambda function invoked by AWS Cloudwatch events. The parameter name is alarmActions and my CFT template for the event rule is as follows:

"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. 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.

My lambda function was like this:

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

It worked when i made the following update:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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