API for checking EC2 instance limit?

2020-07-17 06:24发布

问题:

I use extensively EC2 instances for testing distributed system. Unfortunately, sometimes I hit the limit of running instances which cause the whole deployment to fail. I catch the appropriate exception, but I would prefer to avoid the whole deployment rather than failing after launching several instances. To avoid that kind of situation I would like to have a preflight check:

number_of_running_instances + number_of_required_instances <= instance_limit

I could easily check number_of_running_instances and number_of_required_instances, but I couldn't find an API for checking instance_limit. Even worse, AWS support claims that there is no customer visible api for that:

https://forums.aws.amazon.com/message.jspa?messageID=296314

Moreover, I would prefer not to assume that the limit is default, because some of the AWS accounts at our company applied for higher limit.

Is there any workaround to solve that issue?

回答1:

No, there is no way to do this. But there really isn't a need to do it because catching the exception (and gracefully handling it) is a better design. When launching an entire fleet any number of issues can arise: API calls mysteriously disappear in the void, AWS doesn't have the capacity to launch a certain instance type in the requested AZ, instances and EBS volumes can get stuck during creation, etc.

With the amount of unknown problems that can crop up when deploying AWS resources, you should plan on problems happening rather regularly. Your code needs to anticipate that and workaround it.

It's worth noting that you're using a CloudFormation template to do your provisioning, much of the pain associated with this is mitigated. Rollbacks of all resources are automatically executed on error.



回答2:

FYI, it is possible to query your AWS account limits, see: http://alestic.com/2013/12/ec2-account-attributes



回答3:

You can use amazon describe-account-attributes to get the limits on the aws account.

"aws ec2 describe-account-attributes"



回答4:

Example in Boto

from boto.ec2.connection import EC2Connection

__author__ = 'uva'

conn = EC2Connection(aws_secret_access_key="+secret_key",
                     aws_access_key_id="access_key")
attributes = conn.describe_account_attributes()
for attribute in attributes:
    print(attribute.attribute_name + " : " + str(attribute.attribute_values))

Sample Response

vpc-max-security-groups-per-interface : [u'5']
max-instances : [u'20']
supported-platforms : [u'VPC']
default-vpc : [u'vpc-e1e73484']
max-elastic-ips : [u'5']
vpc-max-elastic-ips : [u'5']


回答5:

You can use Trusted Advisor. Here is the link to Boto API. For each Service you can get Limit Name, Region, Limit Amount and Current Usage. You can write a small wrapper and get the required values.

If you are logged in to your AWS account this link should show you all the details about current limits and usage of your services.



回答6:

It looks like AWS has added this, although oddly it's part of the 'gamelift' API, not under 'ec2'.

  • describe-ec2-instance-limits

Even though I've never used GameLift, the output does match what's found in my console.aws.amazon.com -> EC2 -> Limits web page.

Example:

$ aws gamelift describe-ec2-instance-limits --ec2-instance-type t2.micro
{
    "EC2InstanceLimits": [
        {
            "EC2InstanceType": "t2.micro",
            "CurrentInstances": 0,
            "InstanceLimit": 20
        }
    ]
}

(it outputs all Instance Types without the --ec2-instance-type option)