Skip to content

Instantly share code, notes, and snippets.

@duboisf
Last active February 19, 2024 22:33
Show Gist options
  • Save duboisf/fb0a593365650bae8af1500b94949604 to your computer and use it in GitHub Desktop.
Save duboisf/fb0a593365650bae8af1500b94949604 to your computer and use it in GitHub Desktop.
A nushell oneliner to list all AWS VPC, for all accounts, in all regions #shell #oneliner #nushell

Get all AWS VPCs, for all accounts, in all regions, in nushell

The cool thing about this script is that it uses par-each to run the aws commands in parallel and collecting the results. Do you know how hard that is to do in bash?!

Warning

This script assumes that you have 1 profile entry for each AWS account of the form <account alias>/aws-read-only. Adapt as needed.

aws configure list-profiles | grep aws-read-only | lines | par-each {|profile|
    let account = $profile | split row '/' | first
    aws --profile $profile ec2 describe-regions --output json | from json | get Regions | par-each {|elem|
        print --stderr $"Getting VPCs for ($profile) in ($elem.RegionName)"
        aws --profile $profile --region $elem.RegionName ec2 describe-vpcs --output json | from json | get Vpcs
            | where IsDefault == false
            | default [] Tags
            | select CidrBlock Tags
            | insert account $account
            | insert region $elem.RegionName
            | update Tags {where $it.Key == Name | get 0?.Value}
    } | flatten
} | flatten | sort-by CidrBlock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment