0

Why: I am working on a report similar to reservation utilization report provided by AWS (but different)

I have my list of instances.
I have my list of reservations.
I would like to link them up. I know that many instances may share the same reservation. But I should be able to link them but only I if know the reservation_id for a specific instance. But... How to get the reservation_id for a instance???? Checked docs and could only find cmd line tool to get this info.

Some Code

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()
user2315423
  • 609
  • 1
  • 6
  • 17

1 Answers1

1
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"])) 

(update) I just realised that the OP may asking about mapping reserved instances information and associate with the running instance. The instances ReservationId actually has nothing to do with reserved instances.

Unfortunately, this is quite complicated. Because AWS automatically pool usage of reserved instance that match the exact instance capacity and availability zone. When reserved instance is exhausted, then the billing will start on the typical on-demand instance.

So there is a lots of transition based mix match billing. For example:

  1. You just start an EC2 c4.large in us-west-1, run for 3 days (0.105 x 3 24)
  2. Satisfy with the capacity, now you want to use it for production. You just create an request for reserved instance for c4.large in us-west-1 , pay for 1 year upfront ($576).
  3. In the mid of the year, another c4.large under us-west-1 is created for 12 hours. you will billed on the on-demands rates.
  4. Then you shutdown the previous EC2. The reserved instance billing will automatically "apply" to to the new EC2 with the exact capacity.

As you notice, this is not possible to get this of kind billing dynamic association with boto3 at the moment.

Because of the prepayment, people want to inspect reserved NOT being utilised, somebody already create a boto package to : check any idle reserved instances . This package is handy as some user might pay for reserved instances in one AZ but accidently put EC2 in another AZ, etc.

(following section are keep for historical reading)

boto3.client("ec2").describe_instances()and boto3.resource("ec2").instances.filter() will do the job. Just choose one of them. No reservation handling required. http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.describe_instances

Unless you use "MaxResults" and "NextToken" to control the output, otherwise describe_instances() will show all the instances.

If you check boto3 doc, they will show you this. http://boto3.readthedocs.org/en/latest/guide/migrationec2.html

Note : I just put up a code to list all instances here : To check whether AWS instance is up after reboot using python

Community
  • 1
  • 1
mootmoot
  • 12,845
  • 5
  • 47
  • 44