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
Lambda automatically monitors Lambda functions on your behalf and reports metrics through Amazon CloudWatch. To help you monitor your code when it runs, Lambda automatically tracks the number of requests, the invocation duration per request, and the number of requests that result in an error.
On the Manage CloudWatch alarms detail page, under Add or edit alarm, select Create an alarm. For Alarm notification, choose whether to turn the toggle on or off to configure Amazon Simple Notification Service (Amazon SNS) notifications. Enter an existing Amazon SNS topic or enter a name to create a new topic.
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
You can now use AWS EventBridge (a renamed and extended version of CloudWatch events) to do this with a little less faff! See here for information on how.
It's similar to SNS, but seems to be a bit simpler to work with.
It also has some built in options on filtering of which events trigger your lambda.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With