简体   繁体   中英

Issue in Unused AWS Reserved Instances

In checking unutilized reserved instances, I retrieve those using python script from Github called ec2-check-reserved-instances . It contains:

            running_instances = {}
            for reservation in reservations:
            for instance in reservation.instances:
            if instance.state != "running":
                  sys.stderr.write("Disqualifying instance %s: not running\n" % ( instance.id ) )
            elif instance.spot_instance_request_id:
                sys.stderr.write("Disqualifying instance %s: spot\n" % ( instance.id ) )
            else:
                 **if instance.vpc_id:**
                       print "Does not support vpc yet, please be careful when trusting these results"
                else:
                      az = instance.placement
                      instance_type = instance.instance_type
                      running_instances[ (instance_type, az ) ] = running_instances.get( (instance_type, az ) , 0 ) + 1


   # pprint( running_instances )

It gives me running instances as empty and not reserved instance also empty, but printing unused reserved instances.

When I use the same code without checking if instance.vpc_id , like this:

            running_instances = {}
            for reservation in reservations:
            for instance in reservation.instances:
            if instance.state != "running":
                sys.stderr.write("Disqualifying instance %s: not running\n" % ( instance.id ) )
            elif instance.spot_instance_request_id:
                   sys.stderr.write("Disqualifying instance %s: spot\n" % ( instance.id ) )
            else:
                 az = instance.placement
                 instance_type = instance.instance_type
                 running_instances[ (instance_type, az ) ] = running_instances.get( (instance_type, az ) , 0 ) + 1


           # pprint( running_instances )

It gives me a Running instances list and not created instances. But it does not shows unused reserved instances. Just displays "you have no unused reserved instances".

Why am I getting a different result when I am checking with and without instance.vpc_id ? Which one is right?

Actually it is checking out unutilized instances by comparing running and reserved instances part.

Also what is meant by spot instances, why we have to ignore spot instances too?

I'm not going to go through the code (it is your choice to use that code), but I will explain a few concepts that might make things easier to understand.

An Amazon EC2 Reserved Instance is a pre-purchase of certain Amazon EC2 capacity. By purchasing the Reserved Instance, you are entitled to use a matching Amazon EC2 instance at no hourly cost (because the Reserved Instance is charged Annually or Monthly).

There is no need to nominate which instance is 'reserved'. Rather, the AWS billing system looks at the EC2 instances used every hour and compares it with purchased Reserved Instance capacity. If you own a Reserved Instance that matches an instance that was used, then there is no hourly charge for that instance. The match is done by Instance Type, Operating System and optionally Availability Zone.

If you own one Reserved Instance but run two matching EC2 instances, only one EC2 instance for that hour is matched with the Reserved Instance. The other EC2 instance is charged at normal On-Demand rates.

Therefore, an unused Reserved Instance simply means that you have purchased Reserved Instance(s) and you are not currently running as many EC2 instances as the number of Reserved Instances. You could, therefore, run additional EC2 instances at no additional charge (since it has been pre-paid via a Reserved Instance).

Amazon EC2 instances launched as Spot Instances have their own pricing method, based upon the current Spot Price for the instance based on its Instance Type and Availability Zone. Spot Instances will never consume Reserved Instance capacity, so they should be excluded from any calculation of "unused" Reserved Instances.

Similarly, an EC2 instance that is not running should also be ignored for calculating unused Reserved Instances.

As to the instance.vpc_id line in the script, it appears that this script was written in 2012 and does not correctly handle situations where an Amazon EC2 instance is launched into an Amazon VPC. These days, the default is to use an Amazon VPC so I would not recommend using this script since it is out-of-date and does not handle this use-case.

Instead, I would recommend using Reserved Instance Utilization Reports provided within the AWS Management Console, which can provide the information you seek:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM