首页 文章

如何将cloudwatch警报连接到lambda函数

提问于
浏览
8

如何将aws Cloud 监视警报连接到lambda函数调用?

我正在通过AWS CloudFormation模板以编程方式向我们创建的作为 Cloud 形成堆栈一部分的ELB添加 Cloud 监视警报 . 我希望将警报发送到lambda函数,该函数将消息发布到Slack . 虽然警报有效,并且SNS配置对我来说似乎是正确的,但从不调用lambda函数 .

lambda函数遵循以下示例:

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/

lambda函数有效,我可以通过aws控制台发送测试数据,从而将消息发布到Slack .

使用看起来正确的 Cloud 监控警报创建负载均衡器:

enter image description here

警报似乎配置为将警报发送到正确的SNS主题:

enter image description here

enter image description here

有一个SNS订阅该主题,lambda函数作为它的 endpoints :

enter image description here

警报触发时会触发警报并将消息发送到正确的主题:

enter image description here

但是从不调用lambda函数:

enter image description here

但是,如果我在lambda函数上手动添加SNS主题作为“事件源”,则会在发出警报并释放Slack消息时调用它 .

enter image description here

我是否误解了如何将 Cloud 监视报警器连接到lambda功能?或者我缺少一个小细节?

如果这种方法不起作用,并且将lambda函数连接到 Cloud 监视警报的唯一方法是将SNS主题添加为“事件源”,通过AWS CloudFormation模板执行此操作的适当方法是什么?我没有看到修改现有资源的明显方法,例如固定的lambda函数 .

这是我的CloudFormation模板:

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

谢谢!

-neil

3 回答

  • 4

    确保您授予SNS主题调用Lambda函数的权限 . 权限的CloudFormation将如下所示:

    "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" }
        }
    }
    
  • 0

    AWS(〜3天前)发布了一个蓝图,用于在python和nodejs中使用lambda与AWS Cloudwatch进行松散集成:https://aws.amazon.com/blogs/aws/new-slack-integration-blueprints-for-aws-lambda/

    话虽如此,我也遇到了与你相同的问题,按照蓝图中提到的步骤,我没有得到警报,直到我在lambda函数上手动添加SNS主题为"event source" . 进一步调查引导我提出这个问题:Can't create a SNS Event source on a Lambda function using CloudFormation

    最后阅读AWS文档:1)http://docs.aws.amazon.com/lambda/latest/dg/intro-core-components.html

    Amazon SNS通过主题订阅配置维护事件源映射(没有AWS Lambda API来配置此映射) .

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

    使用AWS管理控制台配置带有Lambda endpoints 的Amazon SNS

    得出结论,此刻的订阅应该通过AWS管理控制台完成

    简介:目前,使用Lambda endpoints 配置Amazon SNS的唯一方法是通过AWS管理控制台

    奖金:类似的问题和相同的答案:AWS Lambda scheduled event source via cloudformation

  • 0

    CloudWatch Scheduled事件现在将Lambda作为本机目标 .
    enter image description here

    您还可以将预定事件添加到lambda的cloudformation中

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

相关问题