Skip to content

Instantly share code, notes, and snippets.

@omkensey
Last active March 22, 2022 16:30
Show Gist options
  • Save omkensey/8163f045801d22356d6cd1a6f2699aa2 to your computer and use it in GitHub Desktop.
Save omkensey/8163f045801d22356d6cd1a6f2699aa2 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Dependencies: AWS CLI, GNU sort, bc
# Tested on Fedora 35.
# Requires valid AWS creds be available to the AWS CLI.
# Example usage:
# find-free-vpcs.sh
# -- query all regions you have access to
# (this will be very slow as it has to make multiple queries for every AWS region)
# find-free-vpcs.sh "string"
# -- query only the regions that begin with a match to "string", e.g. "us" or "us-east"
# You can also use an egrep-compatible regex, e.g.:
# find-free-vpcs.sh 'us-east|eu-west'
REGION_LIST=$(aws --output text --no-paginate --query 'Regions[*].RegionName' ec2 describe-regions)
AWS_GEO=$1
OUTPUT=""
for region in $REGION_LIST; do
if [[ ! -z $AWS_GEO && $(echo $region | grep -E ^$AWS_GEO) ]] || [[ -z $AWS_GEO ]]; then
VPC_QUOTA=$(echo "scale=0; $(aws service-quotas list-service-quotas --output text --service-code vpc --query "Quotas[?QuotaName=='VPCs per Region'].Value" --region $region)/1" | bc)
VPC_USAGE=$(aws ec2 describe-vpcs --output text --no-paginate --query 'length(Vpcs[].VpcId)' --region $region)
VPC_QUOTA_FREE=$(echo "scale=0; ($VPC_QUOTA - $VPC_USAGE)/1" | bc)
OUTPUT+="$region:\tQuota = $VPC_QUOTA\tUsed = $VPC_USAGE\tFree = $VPC_QUOTA_FREE\n"
fi
done
echo -e "$OUTPUT" | sort -n -r -k 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment