Search code examples
amazon-web-servicesaws-lambdaaws-cloudformationaws-event-bridge

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.


Solution

  • 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'])
    }