【问题标题】:Create CloudWatch alarm that sets an instance to standby via SNS/Lambda创建 CloudWatch 警报,通过 SNS/Lambda 将实例设置为备用
【发布时间】:2019-09-12 21:06:26
【问题描述】:

我要做的是在实例达到警报状态时将其设置为待机模式。我已经设置了一个警报来检测我的实例何时达到 90% CPU 一段时间。警报当前通过调用 Lambda 函数的 SNS 发送 Slack 和文本消息。我想补充的是让实例进入待机模式。实例位于自动缩放组中。

我发现您可以通过 CLI 使用以下命令执行此操作:

aws autoscaling enter-standby --instance-ids i-66b4f7d5be234234234 --auto-scaling-group-name my-asg --should-decrement-desired-capacity

你也可以用 boto3 做到这一点:

    response = client.enter_standby(
        InstanceIds=[
            'string',
        ],
        AutoScalingGroupName='string',
        ShouldDecrementDesiredCapacity=True|False
    )

我假设我需要编写另一个将由 SNS 触发的 Lambda 函数,该函数将使用 boto3 代码来执行此操作?

在我开始之前有更好/更简单的方法吗?

我已经将事件中的 InstanceId 传递给 Lambda,因此我必须在事件中添加 ASG 名称。

当我已经拥有实例 ID 时,是否有办法在 Lambda 函数中获取 ASG 名称?那么我就不必将它与事件一起传递。

谢谢!

【问题讨论】:

    标签: aws-lambda amazon-cloudwatch


    【解决方案1】:

    你的问题有几个子部分,所以我会尝试按顺序回答:

    我假设我需要编写另一个将由 SNS 触发的 Lambda 函数,该函数将使用 boto3 代码来执行此操作?

    您不需要需要,您可以重载现有功能。对于单独的函数(关注点分离)或一个函数(因为“对 CPU 达到 90% 的反应”基本上是“一件事”),我可以看到一个有效的论点。

    在我开始之前有更好/更简单的方法吗?

    除了 Cloudwatch -> SNS -> Lambda,我不知道还有什么其他方法可以做到这一点。

    如果我已经有了实例 ID,是否有办法在 Lambda 函数中获取 ASG 名称?

    是的,see this question for an example。看起来像是在 Lambda 中执行此操作还是传递附加参数是更简洁的选项,这取决于您。

    【讨论】:

      【解决方案2】:

      对于任何感兴趣的人,这是我为 Lambda 函数(在 Python 中)提出的:

      # Puts the instance in the standby mode which takes it off the load balancer
      #   and a replacement unit is spun up to take its place
      #
      
      import json
      import boto3
      
      ec2_client = boto3.client('ec2')
      asg_client = boto3.client('autoscaling')
      
      def lambda_handler(event, context):
          # Get the id from the event JSON
          msg = event['Records'][0]['Sns']['Message']
          msg_json = json.loads(msg)
          id = msg_json['Trigger']['Dimensions'][0]['value']
          print("Instance id is " + str(id))
      
          # Capture all the info about the instance so we can extract the ASG name later
          response = ec2_client.describe_instances(
              Filters=[
                  {
                      'Name': 'instance-id',
                      'Values': [str(id)]
                  },
              ],
          )
      
          # Get the ASG name from the response JSON
          #autoscaling_name = response['Reservations'][0]['Instances'][0]['Tags'][1]['Value']
          tags = response['Reservations'][0]['Instances'][0]['Tags']
          autoscaling_name = next(t["Value"] for t in tags if t["Key"] == "aws:autoscaling:groupName")
          print("Autoscaling name is - " + str(autoscaling_name))
      
          # Put the instance in standby
          response = asg_client.enter_standby(
              InstanceIds=[
                  str(id),
              ],
              AutoScalingGroupName=str(autoscaling_name),
              ShouldDecrementDesiredCapacity=False
          )
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-15
        • 2019-04-07
        • 2018-11-06
        • 2018-01-08
        相关资源
        最近更新 更多