Skip to content

Instantly share code, notes, and snippets.

@nshores
Last active January 11, 2022 02:25
Show Gist options
  • Save nshores/4cd564c5861fcaf45fad59035ae93321 to your computer and use it in GitHub Desktop.
Save nshores/4cd564c5861fcaf45fad59035ae93321 to your computer and use it in GitHub Desktop.
#!/bin/bash
#This script will generate a list of various subnet attributes from an input value of a list of aws profile's. Each profile will be used to crawl the region for all available subnets, and output details to CSV.
#Example input: ./generate_aws_subnet_list_.sh "autogrid-dev autogrid-prod"
AWS_PROFILE=$1
echo "Current AWS_PROFILE: $AWS_PROFILE"
for profile in $AWS_PROFILE; do
echo "Changing Profile to $profile"
export AWS_PROFILE=$profile
#Get regions if we haven't already
if [ -z "$regions" ]
then
echo "Getting Regions"
regions=$(aws ec2 describe-regions --region us-east-2 --output text | cut -f4 | sort)
echo -e "Region List:\n$regions"
fi
echo "Exporting subnet list for $profile"
for region in $regions; do
#TODO - Figure out how to get the tag formatting correct so that it handles edge cases, it's taken out for now. Name:Tags[?Key=='Name'].Value
aws ec2 describe-subnets --region $region --query "Subnets[*].{CidrBlock:CidrBlock,SubnetId:SubnetId,AvailabilityZone:AvailabilityZone,VpcId:VpcId}" \
--output text | sed 's/\t/,/g' >> ag_subnet_$profile.csv
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment