如何在创建新 EC2 实例时创建自动 Cloudwatch 警报

Muh*_*ais 7 automation amazon-web-services amazon-cloudwatch aws-lambda

我想创建一个 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')
Run Code Online (Sandbox Code Playgroud)

但我必须将 cloudwatch 规则中的实例 ID 作为输入映射到 Lambda。由于该函数会自动触发,因此无法每次手动输入实例 ID。

Adi*_*iii 4

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

  • 例如,从自动缩放组启动
  • 例如,使用 EC2 启动

另外,我将添加启动终止

  • 启动时(添加警报)
  • 终止时(删除警报)以避免达到最大限制

自动伸缩组CW规则:

{
  "source": [
    "aws.autoscaling"
  ],
  "detail-type": [
    "EC2 Instance Launch Successful",
    "EC2 Instance Terminate Successful"
  ]
}
Run Code Online (Sandbox Code Playgroud)

自动缩放事件:

{
  "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."
  }
}
Run Code Online (Sandbox Code Playgroud)

EC2 CW 规则:

{
  "source": [
    "aws.ec2"
  ],
  "detail-type": [
    "EC2 Instance State-change Notification"
  ],
  "detail": {
    "state": [
      "running",
      "terminated"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

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"
  }
}
Run Code Online (Sandbox Code Playgroud)

所以你可以根据事件完成其余的逻辑,下面的示例基于 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
          }

     }

Run Code Online (Sandbox Code Playgroud)

相同的逻辑可以应用于 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
          }

     }
Run Code Online (Sandbox Code Playgroud)