Skip to content

Instantly share code, notes, and snippets.

@stuart-warren
Last active July 23, 2020 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuart-warren/b666059e0c9f0eae6f21fef7d361802e to your computer and use it in GitHub Desktop.
Save stuart-warren/b666059e0c9f0eae6f21fef7d361802e to your computer and use it in GitHub Desktop.
Ensure you have quota to create Elastic IPs in CI to avoid failing test randomly
import boto
import time
ec2 = boto3.Session(region_name="eu-west-1").client('ec2')
eips_to_create = 9
def get_eip_quota():
# get limit of ElasticIps for account
quota = 0
for acc_attr in ec2.describe_account_attributes().get('AccountAttributes'):
if acc_attr.get('AttributeName') == 'vpc-max-elastic-ips':
quota = int(acc_attr.get('AttributeValues')[0].get('AttributeValue'))
break
return quota
def get_eip_usage():
# get number of ElasticIps in use
return len(ec2.describe_addresses().get('Addresses'))
limit = get_eip_quota()
while True:
current = get_eip_usage()
if current + eips_to_create <= limit:
break
print(f'not enough quota (total: {limit}, used: {current}) to create required number of eips, sleeping...')
time.sleep(5)
create_some_eips()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment