Created
August 13, 2019 19:00
-
-
Save narenst/65d7e7941672568a4e03edb0c84b14aa to your computer and use it in GitHub Desktop.
programmatically view and increase aws quota using boto3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import boto3 | |
def get_value_for_quota_name(quotas, quota_name): | |
for quota in quotas: | |
if quota['QuotaName'] == quota_name: | |
return quota['Value'], quota['QuotaCode'] | |
return None, None | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="View and increase instance quotas") | |
parser.add_argument('instance_type', help="AWS instance type") | |
parser.add_argument('--increase-to', type=int, default=0, help="Request increase of instance to this limit") | |
parser.add_argument('--region', type=str, default='us-east-2', help="AWS Region") | |
parser.add_argument('--profile', type=str, default='default', help="AWS profile") | |
args = parser.parse_args() | |
boto_session = boto3.Session(region_name=args.region, profile_name=args.profile) | |
quota_client = boto_session.client('service-quotas') | |
quota_paginator_client = quota_client.get_paginator('list_service_quotas') | |
quota_name = f"Running On-Demand {args.instance_type} instances" | |
response = quota_paginator_client.paginate( | |
ServiceCode='ec2', | |
) | |
for quota_set in response: | |
quota_value, quota_code = get_value_for_quota_name(quota_set['Quotas'], quota_name) | |
if quota_value is not None: | |
quota_value = int(quota_value) | |
break | |
if quota_value is None: | |
print("Cannot find quota for this instance type. Check if the instance type and region are correct") | |
exit(1) | |
print(f"Your quota limit for {args.instance_type} in the {args.region} region is: {quota_value}") | |
# Get any pending increase request for this instance type: | |
response = quota_client.list_requested_service_quota_change_history_by_quota( | |
ServiceCode='ec2', | |
QuotaCode=quota_code, | |
) | |
for request in response['RequestedQuotas']: | |
if request['Status'] in ['PENDING', 'CASE_OPENED']: | |
print(f"You have a pending quota request increase to limit: {request['DesiredValue']}") | |
# Exit if there is an open or pending request, cannot request another one | |
exit(0) | |
# Process quota increase now | |
if args.increase_to: | |
if args.increase_to <= quota_value: | |
print(f"New quota limit {args.increase_to} must be more than the current limit: {quota_value}") | |
exit(1) | |
quota_client.request_service_quota_increase( | |
ServiceCode='ec2', | |
QuotaCode=quota_code, | |
DesiredValue=float(args.increase_to) | |
) | |
print(f"Submitted a quota increase request for {args.instance_type} in " | |
f"{args.region} region to: {args.increase_to}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment