Find all VPC subnets with a specific tag

Using AWS CLI to find all VPC subnets with a specific a tag

The AWS CLI command can find across multiple regions all instances of AWS VPC Subnet that has a specific tag attached

Find all VPC subnets with a specific tag

The following describe-subnets example uses a filter to retrieve the details of those subnets with the tag VPCname=HappyVPC and the --query parameter to display the subnet IDs of the subnets with this tag. You can replace the key pair value to your own tags you to filter in your account.

for region in `aws ec2 describe-regions --query "Regions[].RegionName" --region us-east-1 --output text`
do
    echo "[${region}]"
    aws ec2 describe-subnets --filters "Name=tag:VPCname,Values=HappyVPC" --query "Subnets[*].SubnetId" --output text --region ${region}
done
echo "finished"

                                


Created by:

Xolile Speelman

Xolile Speelman

Xolile Speelam is a Cloud Support Engineer @ AWS

Follow on LinkedIn