为什么我的 AWS Lambda 脚本 return 'Instances' 而不是实例 ID?
Why does my AWS Lambda script return 'Instances' instead of the instance ID?
下面是我的 Lambda 脚本,该脚本正在进行中,用于备份我的一些 EC2 实例。我在分配后立即打印出 instanceId 的值,令我惊讶的是,它返回了字符串 'Instances' 而不是实例 ID。我在这里检查了响应的预期格式:http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.describe_instances,我相信我正在正确地进行调用。我首先只从列表 (schedule_instances = schedulers[[=16=]]) 中获取实例项,然后尝试从该新列表中获取实例 ID。这个对吗?我对获取VolumeId也有类似的疑惑。
from __future__ import print_function
import json
import boto3
import datetime
import time
ec2 = boto3.client('ec2')
def lambda_handler(event, context):
try:
print("Creating snapshots on " + str(datetime.datetime.today()) + ".")
schedulers = ec2.describe_instances(MaxResults=50, Filters=[{'Name':'tag:GL-sub-purpose', 'Values':['Schedule']}])
print("Performing backup on " + str(len(schedulers)) + " schedules.")
successful = []
failed = []
schedule_instances = schedulers['Instances']
for s in schedulers:
try:
instanceId=s['InstanceId']
print (instanceId)
snapshotDescription = instanceId + "-" + str(datetime.date.today().strftime('%Y-%m-%d')) + "-46130e7ac954-automated"
ec2.create_snapshot(
VolumeId=s['VolumeId'],
Description=snapshotDescription
)
successful.append(instanceId)
except Exception as e:
print(e)
failed.append(instanceId + " :\t" + str(e))
print("Performed backup on " + str(len(successful)) + " schedulers. Failed backup on " + str(len(failed)) + " schedulers. ")
sendEmail(successful, failed)
return "Success"
except Exception as e:
print(e)
return "Failed"
您的 for 循环部分似乎没有遍历 Json 键值。
使用以下代码通过 Boto3 检索实例 ID
import boto3
ec2 = boto3.client('ec2')
schedulers = ec2.describe_instances(InstanceIds=['i-xxxxxxxx'])
for i in schedulers['Reservations']:
print i['Instances'][0]['InstanceId']
您可以在代码中实现相同的 for 循环(如果需要多个实例,请使用循环)
希望这对您有所帮助。
下面是我的 Lambda 脚本,该脚本正在进行中,用于备份我的一些 EC2 实例。我在分配后立即打印出 instanceId 的值,令我惊讶的是,它返回了字符串 'Instances' 而不是实例 ID。我在这里检查了响应的预期格式:http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.describe_instances,我相信我正在正确地进行调用。我首先只从列表 (schedule_instances = schedulers[[=16=]]) 中获取实例项,然后尝试从该新列表中获取实例 ID。这个对吗?我对获取VolumeId也有类似的疑惑。
from __future__ import print_function
import json
import boto3
import datetime
import time
ec2 = boto3.client('ec2')
def lambda_handler(event, context):
try:
print("Creating snapshots on " + str(datetime.datetime.today()) + ".")
schedulers = ec2.describe_instances(MaxResults=50, Filters=[{'Name':'tag:GL-sub-purpose', 'Values':['Schedule']}])
print("Performing backup on " + str(len(schedulers)) + " schedules.")
successful = []
failed = []
schedule_instances = schedulers['Instances']
for s in schedulers:
try:
instanceId=s['InstanceId']
print (instanceId)
snapshotDescription = instanceId + "-" + str(datetime.date.today().strftime('%Y-%m-%d')) + "-46130e7ac954-automated"
ec2.create_snapshot(
VolumeId=s['VolumeId'],
Description=snapshotDescription
)
successful.append(instanceId)
except Exception as e:
print(e)
failed.append(instanceId + " :\t" + str(e))
print("Performed backup on " + str(len(successful)) + " schedulers. Failed backup on " + str(len(failed)) + " schedulers. ")
sendEmail(successful, failed)
return "Success"
except Exception as e:
print(e)
return "Failed"
您的 for 循环部分似乎没有遍历 Json 键值。
使用以下代码通过 Boto3 检索实例 ID
import boto3
ec2 = boto3.client('ec2')
schedulers = ec2.describe_instances(InstanceIds=['i-xxxxxxxx'])
for i in schedulers['Reservations']:
print i['Instances'][0]['InstanceId']
您可以在代码中实现相同的 for 循环(如果需要多个实例,请使用循环)
希望这对您有所帮助。