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:
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