原因:我正在编写一份类似AWS提供的保留使用报告(但不同)。
我有我的例子清单。
我有我的预订单。
我想把它们联系起来。我知道许多情况下可能有同样的保留意见。但是,我应该能够将它们链接起来,但前提是我知道特定实例的reservation_id。但是..。例如如何获得reservation_id?检查了文档,并只能找到cmd行工具来获取此信息。
某些代码
import boto3
client = boto3.client('ec2')
region = "us-east-1"
ec2 = boto3.resource("ec2", region_name=region)
ec2_list = list()
for instance in ec2.instances.all():
name = 'Un-named'
for tag in instance.tags:
if tag['Key'].upper() == 'NAME':
name = tag['Value'] # nothing called tag['reservation_id']
ec2_list.append((name, instance.id, instance.public_dns_name,
instance.placement['AvailabilityZone'],
instance.instance_type))
reservations = client.describe_reserved_instances()发布于 2016-04-08 08:18:11
import boto3
ec2 = boto3.client("ec2")
response = ec2.describe_instances()
for each_reservation in response["Reservation"]:
for each_instance in each_reservation["Instances"])
print("Reserved_id:{}\tinstance_id:{}".format(
each_reservation["ReservationId"],
each_instance["InstanceId"])) (更新)我刚刚意识到OP可能询问有关映射reserved instances信息的问题,并与正在运行的实例相关联。实例ReservationId实际上与保留实例无关。
不幸的是,这是相当复杂的。因为AWS会自动汇集保留实例的使用,这些实例与精确的实例容量和可用性区域相匹配。当保留实例耗尽时,计费将开始于典型的随需应变实例.
因此,有许多基于过渡的混合匹配计费。例如:
正如您注意到的,这是不可能得到这种类型的计费动态关联,在目前的boto3。
由于提前付款,人们希望检查预留未被使用,有人已经创建了一个boto包到:检查任何空闲保留实例。这个包很方便,因为一些用户可能会为一个AZ中的保留实例付费,但不小心将EC2放在另一个AZ中,等等。
(以下部分为历史阅读而保留)
Boto3.client(“ec2”)、.describe_instances()和boto3.resources(“ec2”).instances.filter()将完成这项工作。选一个就行了。不需要预订处理。实例
除非使用"MaxResults“和"NextToken”来控制输出,否则describe_instances()将显示所有实例。
如果你检查boto3文档,他们会给你看这个。http://boto3.readthedocs.org/en/latest/guide/migrationec2.html
注意:我只是在这里列出了所有实例的代码:使用python检查在重新启动后AWS实例是否已启动
https://stackoverflow.com/questions/36488089
复制相似问题