how to connect a cloudwatch alarm to a lambda function

How do you connect an aws cloud watch alarm to a lambda function invocation?

I am programmatically adding a cloud watch alarm to the ELBs that we create as part of a cloud formation stack via AWS CloudFormation Templates. I want to have the alerts sent to a lambda function that will post the message to Slack. Although the alert works, and the SNS config seems correct to me, the lambda function is never invoked.

The lambda function follows these examples:

https://medium.com/cohealo-engineering/how-set-up-a-slack-channel-to-be-an-aws-sns-subscriber-63b4d57ad3ea#.x2j9apedu

http://inopinatus.org/2015/07/13/hook-aws-notifications-into-slack-with-a-lambda-function/

The lambda function works, and I can send it test data via the aws console resulting in a message posted to Slack.

The load balancer is created with a correct-looking cloud watch alarm:

在这里输入图像描述

The alarm appears to be configured to send alerts to the correct SNS topic:

在这里输入图像描述在这里输入图像描述

There is an SNS subscription to that topic, with the lambda function as it's endpoint:

在这里输入图像描述

Alarms are triggered and messages sent to the correct topic when the alarm fires:

在这里输入图像描述

But the lambda function is never invoked:

在这里输入图像描述

However, if I manually add the SNS topic as an "event source" on the lambda function, it is invoked when the alarm fires and Slack messages are posted.

在这里输入图像描述

Am I misunderstanding how to connect a cloud watch alarm to a lambda function? Or is there a small detail I am missing?

If this approach cannot work, and the only way to connect a lambda function to a cloud watch alarm is to add the SNS topic as an "event source", what is the appropriate way to do that via AWS CloudFormation Templates? I don't see an obvious way to modify an existing resource such as a fixed lambda function.

Here is my CloudFormation Template:

"GenericSlackAlertSNSTopic" : {
    "Type" : "AWS::SNS::Topic",
    "Properties" : {
        "Subscription" : [ {
            "Endpoint" : "arn:aws:lambda:us-east-1:[...]:function:snsToSlack",
            "Protocol" : "lambda"
        } ]
    }
},
"ELBNoTrafficAlarm": {
    "Type": "AWS::CloudWatch::Alarm",
    "Properties": {
        "Namespace" : "AWS/ELB",
        "AlarmDescription": "Alarm for no apparent traffic on an ELB.",
        "AlarmActions": [{
            "Ref": "GenericSlackAlertSNSTopic"
        }],
        "InsufficientDataActions": [{
            "Ref": "GenericSlackAlertSNSTopic"
        }],
        "MetricName": "RequestCount",
        "Statistic": "Sum",
        "Dimensions" : [ {
            "Name" : "LoadBalancerName",
            "Value" : { "Ref" : "ElasticLoadBalancer" }
        } ],
        "Period": "60",
        "EvaluationPeriods": "3",
        "Threshold" : "10",
        "ComparisonOperator": "LessThanOrEqualToThreshold"
    }
}

Thanks!

-neil


AWS released (~3 days ago) a blueprint for the slack integration with AWS Cloudwatch using lambda both in python and nodejs: https://aws.amazon.com/blogs/aws/new-slack-integration-blueprints-for-aws-lambda/

Being said that, I also had the same problem as you, following the steps mentioned in the blueprint, I do not get the alarms until I manually add the SNS topic as an "event source" on the lambda function. Further investigation lead me to this question: Can't create a SNS Event source on a Lambda function using CloudFormation

And finally reading the AWS documentation: 1) http://docs.aws.amazon.com/lambda/latest/dg/intro-core-components.html

Amazon SNS maintains the event source mapping via topic subscription configuration (there is no AWS Lambda API to configure this mapping).

2) http://docs.aws.amazon.com/sns/latest/dg/sns-lambda.html

Configuring Amazon SNS with Lambda Endpoints with the AWS Management Console

Concluded that the subscription at the moment should be done through the AWS Management console

Summary: at the moment the only way to configure Amazon SNS with Lambda Endpoints is through the AWS Management Console

Bonus: similar question with the same answer: AWS Lambda scheduled event source via cloudformation


CloudWatch Scheduled event now has Lambda an a native target. 在这里输入图像描述

Also you can add a scheduled event to cloudformation for lambda

EventListFunction:
  Type: 'AWS::Serverless::Function'
  Properties:
    ...
    Events:
      Schedule1:
        Type: Schedule
        Properties:
          Schedule: rate(1 day)

Make sure you give the SNS topic permission to invoke your Lambda function. The CloudFormation for the permission will look something like this:

"LambdaInvokePermission": {
    "Type": "AWS::Lambda::Permission",
    "Properties": {
        "FunctionName" : "arn:aws:lambda:us-east-1:[...]:function:snsToSlack",
        "Action": "lambda:InvokeFunction",
        "Principal": "sns.amazonaws.com",
        "SourceArn": { "Ref": "GenericSlackAlertSNSTopic" }
    }
}
链接地址: http://www.djcxy.com/p/32248.html

上一篇: AWS移动推送与可能登录到多个设备的用户

下一篇: 如何将cloudwatch警报连接到lambda函数