从 SNS/SQS Auto Scaling 消息中提取 EC2InstanceId

Extracting EC2InstanceId from SNS/SQS Auto Scaling message

我正在使用 python Boto3 代码,当实例从 Auto Scaling 组终止时,它会通知 SNS,SNS 将消息发布到 SQS。通知SNS时也会触发Lambda,执行boto脚本从SQS抓取消息。

我正在使用来自 Sending and Receiving Messages in Amazon SQS 的参考代码。

这是代码片段:

if messages.get('Messages'):
  m = messages.get('Messages')[0]
  body = m['Body']

  print('Received and deleted message: %s' % body)

结果是:

START RequestId: 1234-xxxxxxxx Version: $LATEST
 {
  "Type" : "Notification",
  "MessageId" : "d1234xxxxxx",
  "TopicArn" : "arn:aws:sns:us-east-1:xxxxxxxxxx:AutoScale-Topic",
  "Subject" : "Auto Scaling: termination for group \"ASG\"",
  "Message" : "{\"Progress\":50,\"AccountId\":\"xxxxxxxxx\",\"Description\":\"Terminating EC2 instance: i-123456\",\"RequestId\":\"db-xxxxx\",\"EndTime\":\"2017-07-13T22:17:19.678Z\",\"AutoScalingGroupARN\":\"arn:aws:autoscaling:us-east-1:360695249386:autoScalingGroup:fef71649-b184xxxxxx:autoScalingGroupName/ASG\",\"ActivityId\":\"db123xx\",\"EC2InstanceId\":\"i-123456\",\"StatusCode\"\"}",
  "Timestamp" : "2017-07-",
  "SignatureVersion" : "1",
  "Signature" : "",
  "SigningCertURL" : "https://sns.us-east-1.amazonaws.com/..",
  "UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/
}

我只需要终止实例的 EC2InstanceId,而不是整个消息。如何提取 ID?

实例ID在消息中。它是原始 JSON,因此您可以使用 json 包解析它并获取信息。

import json
if messages.get('Messages'):
  m = messages.get('Messages')[0]
  body = m['Body']
  notification_message = json.loads(body["Message"])

  print('instance id is: %s' % notification_message["EC2InstanceId"])

如果您的目标是执行 AWS Lambda 函数(将 EC2 实例 ID 作为参数),也不需要将消息发布到 Amazon SQS 队列 .事实上,这是不可靠的,因为您无法保证从 SQS 队列中检索的消息与您的 Lambda 函数的调用相匹配。

幸运的是,当 Auto Scaling 向 SNS 发送事件并且 SNS 然后触发 Lambda 函数时,SNS 将必要的信息 直接传递给 Lambda 函数。

使用此代码(或类似代码)启动您的 Lambda 函数:

def lambda_handler(event, context):

    # Dump the event to the log, for debugging purposes
    print("Received event: " + json.dumps(event, indent=2))

    # Extract the EC2 instance ID from the Auto Scaling event notification
    message = event['Records'][0]['Sns']['Message']
    autoscalingInfo = json.loads(message)
    ec2InstanceId = autoscalingInfo['EC2InstanceId']

您的代码将具有 EC2 实例 ID,而无需使用 Amazon SQS。