Skip to content

Instantly share code, notes, and snippets.

@darktef
Created August 16, 2022 15:17
Show Gist options
  • Save darktef/1de7249ef7f5734d29dcd2ed3e1caa1a to your computer and use it in GitHub Desktop.
Save darktef/1de7249ef7f5734d29dcd2ed3e1caa1a to your computer and use it in GitHub Desktop.
AWS - Get all AZs for the subnet
typeset -A newmap
newmap[111111111111]="IAD-Alpha"
newmap[222222222222]="IAD-Beta"
# echo ${newmap[111111111111]}
# for key ("${(@k)newmap}") printf 'key=%s value=%s\n' "$key" "$newmap[$key]"
for key ("${(@k)newmap}")
do
# use cli command to switch role, check sth here https://stackoverflow.com/questions/593334/how-to-use-multiple-aws-accounts-from-the-command-line
echo "Adding AZ for $newmap[$key] with accountId $key" >> minos_subnet_az_array
aws ec2 describe-subnets --filters "Name=tag:Name,Values=*PublicSubnet*" --query 'Subnets[*].{az:AvailabilityZoneId,name:Tags[?Key==`Name`].Value[]} | sort_by([], &to_string(name))[].az' --output json >> minos_subnet_az_array
done
typeset -A newmap
newmap[333333333333]="DUB-Alpha"
newmap[444444444444]="DUB-Beta"
for key ("${(@k)newmap}")
do
echo "Adding AZ for $newmap[$key] with accountId $key" >> minos_subnet_az_array
aws ec2 describe-subnets --region eu-west-1 --filters "Name=tag:Name,Values=*PublicSubnet*" --query 'Subnets[*].{az:AvailabilityZoneId,name:Tags[?Key==`Name`].Value[]} | sort_by([], &to_string(name))[].az' --output json >> minos_subnet_az_array
done
typeset -A newmap
newmap[555555555555]="PDX-Alpha"
newmap[666666666666]="PDX-Beta"
for key ("${(@k)newmap}")
do
# Command to switch to the account with role
echo "Adding AZ for $newmap[$key] with accountId $key" >> minos_subnet_az_array
aws ec2 describe-subnets --region us-west-2 --filters "Name=tag:Name,Values=*PublicSubnet*" --query 'Subnets[*].{az:AvailabilityZoneId,name:Tags[?Key==`Name`].Value[]} | sort_by([], &to_string(name))[].az' --output json >> minos_subnet_az_array
done
@darktef
Copy link
Author

darktef commented Aug 16, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment