amazon-web-services - 如何在创建新 EC2 实例时创建自动 Cloudwatch 警报

标签 amazon-web-services automation aws-lambda amazon-cloudwatch

我想创建一个 lambda 函数,该函数在创建新 EC2 实例时触发,此 Lambda 函数应自动在此新实例上配置 StatusCheck 警报。这样我就不必在每次创建新实例时手动配置 cloudwatch 警报。有人可以帮助完成此操作的 lambda 函数的代码吗?
我有这样的事情:

   response = client.put_metric_alarm(
    AlarmName='StatusCheckFailed-Alarm-for-i-1234567890abcdef0',
    AlarmActions=[
        'arn:aws:sns:us-west-2:111122223333:my-sns-topic',
    ],
    MetricName='StatusCheckFailed',
    Namespace='AWS/EC2',
    Statistic='Maximum',
    Dimensions=[
        {
            'Name': 'InstanceId',
            'Value': 'i-1234567890abcdef0'
        },
    ],
    Period=300,
    Unit='Count',
    EvaluationPeriods=2,
    Threshold=1,
    ComparisonOperator='GreaterThanOrEqualToThreshold')
但是我必须将 cloudwatch 规则中的实例 ID 作为输入映射到 Lambda。因为该函数会自动触发,所以无法每次都手动放置实例 ID。

最佳答案

您将需要两个云监视规则来处理此问题

  • 例如从自动缩放组启动的一个
  • 例如使用 EC2 启动的一个

  • 另外,我将添加 Launch Terminatioin
  • 启动时(添加警报)
  • 终止(删除警报)以避免达到最大限制

  • 自动缩放组 CW 规则:
    {
      "source": [
        "aws.autoscaling"
      ],
      "detail-type": [
        "EC2 Instance Launch Successful",
        "EC2 Instance Terminate Successful"
      ]
    }
    
    自动缩放事件:
    {
      "version": "0",
      "id": "3e3c153a-8339-4e30-8c35-687ebef853fe",
      "detail-type": "EC2 Instance Launch Successful",
      "source": "aws.autoscaling",
      "account": "123456789012",
      "time": "2015-11-11T21:31:47Z",
      "region": "us-east-1",
      "resources": [
        "arn:aws:autoscaling:us-east-1:123456789012:autoScalingGroup:eb56d16b-bbf0-401d-b893-d5978ed4a025:autoScalingGroupName/sampleLuanchSucASG",
        "arn:aws:ec2:us-east-1:123456789012:instance/i-b188560f"
      ],
      "detail": {
        "StatusCode": "InProgress",
        "AutoScalingGroupName": "sampleLuanchSucASG",
        "ActivityId": "9cabb81f-42de-417d-8aa7-ce16bf026590",
        "Details": {
          "Availability Zone": "us-east-1b",
          "Subnet ID": "subnet-95bfcebe"
        },
        "RequestId": "9cabb81f-42de-417d-8aa7-ce16bf026590",
        "EndTime": "2015-11-11T21:31:47.208Z",
        "EC2InstanceId": "i-b188560f",
        "StartTime": "2015-11-11T21:31:13.671Z",
        "Cause": "At 2015-11-11T21:31:10Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 1.  At 2015-11-11T21:31:11Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 1."
      }
    }
    
    EC2 CW 规则:
    {
      "source": [
        "aws.ec2"
      ],
      "detail-type": [
        "EC2 Instance State-change Notification"
      ],
      "detail": {
        "state": [
          "running",
          "terminated"
        ]
      }
    }
    
    EC2 事件:
    {
      "version": "0",
      "id": "ee376907-2647-4179-9203-343cfb3017a4",
      "detail-type": "EC2 Instance State-change Notification",
      "source": "aws.ec2",
      "account": "123456789012",
      "time": "2015-11-11T21:30:34Z",
      "region": "us-east-1",
      "resources": [
        "arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111"
      ],
      "detail": {
        "instance-id": "i-abcd1111",
        "state": "running"
      }
    }
    
    所以你可以根据事件做其余的逻辑,下面的例子是基于 javascript
    如果来自自动缩放组的事件
    
         if (event["source"] == "aws.autoscaling") {
              
              if (event["detail-type"] === "EC2 Instance Launch Successful"){
                    let EC2_ID=event.detail.EC2InstanceId
                   // Add alarm here
                  // use EC2 instance ID
              }
    
         }
    
    
    相同的逻辑可以应用于 EC2 事件,您可以在其中检查状态
         if (event["source"] == "aws.ec2") {
              
              if (event.detail === "running"){
                    let EC2_ID=event.detail.EC2InstanceId
                   // Add alarm here
                  // use EC2 instance ID
              }
             // same can be check for termination
              if (event.detail === "terminated"){
                    let EC2_ID=event.detail.EC2InstanceId
                   // remove alarm for this instance
                  // use EC2 instance ID here to remove/delete alaram
              }
    
         }
    

    关于amazon-web-services - 如何在创建新 EC2 实例时创建自动 Cloudwatch 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62655992/

    相关文章:

    linux - 在多台机器上的oracle JDK上执行tzupdater工具

    amazon-web-services - AWS Lambda 目标 Lambda 未触发

    mongodb - 使用无服务器框架的 Lambda 函数环境设置中的逗号分隔符

    amazon-web-services - 每个 Fn::And 对象需要一个至少 2 个、最多 10 个 bool 参数的列表

    amazon-web-services - AWS Lambda 意外无限循环后的令人担忧的行为

    ruby - 在 OSX 上的 Ruby 中同时上传文件夹中的文件

    automation - 是否可以 100% 以编程方式创建、更新和删除(管理)Dialogflow 代理?

    node.js - 如何在 AWS lambda 上安装 phantomjs?

    amazon-web-services - 只能从 API Gateway 访问的公共(public) ELB

    amazon-web-services - 获取 CDK 文档中定义的 key 的 'extraneous key is not permitted'