PureDevOps Community

AWS: Script to list all the vpc for a aws account in all the regions

To list all the VPCs for an AWS account in all the regions, you can use the AWS Command Line Interface (CLI) with a script that iterates through each region and retrieves the VPC information.

Here’s an example script using Bash:

bashCopy code

#!/bin/bash

# Retrieve all AWS regions
regions=$(aws ec2 describe-regions --query "Regions[].RegionName" --output text)

# Iterate over each region
for region in $regions; do
  echo "Region: $region"
  
  # Retrieve VPC information for the current region
  vpcs=$(aws ec2 describe-vpcs --region $region --query "Vpcs[].VpcId" --output text)
  
  # Check if VPCs exist in the region
  if [ -n "$vpcs" ]; then
    echo "VPCs:"
    
    # Iterate over each VPC and display its ID
    for vpc in $vpcs; do
      echo "  $vpc"
    done
  else
    echo "No VPCs found in this region."
  fi
  
  echo
done

Here’s how you can use this script:

  1. Ensure you have the AWS CLI installed and configured with your AWS account credentials.
  2. Create a new file with a .sh extension, e.g., list_vpcs.sh.
  3. Copy and paste the script into the file.
  4. Save the file and exit.
  5. Open a terminal or command prompt.
  6. Navigate to the directory where you saved the script.
  7. Run the script using the following command: bash list_vpcs.sh.

The script will iterate over each AWS region, retrieve the VPC information for each region, and display the VPC IDs.