Joe Gilmore

1 min read

AWS - List All EC2s in All Regions

Do you own an AWS account and want to easily list all EC2 instances in all regions? This is a simple NodeJS script that will do just that.

AWS - List All EC2s in All Regions

Pre-Requisites

Make sure you have Node installed, also make sure you have the AWS CLI installed and configured. Finally you need to make sure you have an IAM user with the correct permissions to list EC2 instances in all regions and saved into your ~/.aws/credentials file.

The NodeJS Script

This script is super simple, but also super useful if you want to be able to hunt down any running instances in your AWS account.

const {exec} = require('child_process');
const PROFILE = '--profile EDIT_YOUR_PROFILE_HERE'; // <-- Edit this from your ~/.aws/credentials file - or leave empty to use [default]

function getRegions(){
    return new Promise((resolve, reject) => {
        exec(`aws ec2 describe-regions ${PROFILE}`, (err, stdout, stderr) => {
            if (err) { reject(`Error: ${err}`); }
            resolve(JSON.parse(stdout).Regions.map( (region) => region.RegionName ));
        })
    });
}

function listInstancesInRegion(region){
    return new Promise((resolve, reject) => {
        exec(`aws ec2 describe-instances ${PROFILE} --region ${region}`, (err, stdout, stderr) => {
            if (err) { reject(`Error: ${err}`); }
            resolve(JSON.parse(stdout));
        })
    });
}

async function begin(){
    const regions = await getRegions();
    for (const region of regions){
        const instances = await listInstancesInRegion(region);
        console.log(region, instances);
    }
}

begin();

Running the Script

Save this file as something like list-ec2s.js and then run it with NodeJS:

node list-ec2s.js

Output

You will then see the output in your console with something like this:

ap-south-1 { Reservations: [] }
eu-north-1 { Reservations: [] }
eu-west-3 { Reservations: [] }
eu-west-2 { Reservations: [] }
eu-west-1 { Reservations: [] }
ap-northeast-3 { Reservations: [] }
ap-northeast-2 { Reservations: [] }
ap-northeast-1 { Reservations: [] }
ca-central-1 { Reservations: [] }
sa-east-1 { Reservations: [] }
ap-southeast-1 { Reservations: [] }
ap-southeast-2 { Reservations: [] }
eu-central-1 { Reservations: [] }
us-east-1 { Reservations: [] }
us-east-2 { Reservations: [] }
us-west-1 { Reservations: [] }
us-west-2 { Reservations: [] }

I am running this script in a brand new account, and have zero running instances so my output arrays are empty... but if you have running instances you will see them in the output.

Going further...

You can then take this script and do whatever you want with it. You could easily add a filter to only show instances that are running, or you could add a filter to only show instances that have a specific tag.

Or do you use Secrets Manager? Then try adding another function like this one:

function listSecretsInRegion(region){
    return new Promise((resolve, reject) => {
        exec(`aws secretsmanager list-secrets ${PROFILE} --region ${region}`, (err, stdout, stderr) => {
            if (err) { reject(`Error: ${err}`); }
            resolve(JSON.parse(stdout));
        })
    });
}

...or how about if you use CloudFormation? Then you could add a function like this one:

function listStacksInRegion(region){
    return new Promise((resolve, reject) => {
        exec(`aws cloudformation list-stacks ${PROFILE} --region ${region}`, (err, stdout, stderr) => {
            if (err) { reject(`Error: ${err}`); }
            resolve(JSON.parse(stdout));
        })
    });
}

Conclusion

As you can see this really simple script can allow us to easily see whats going on in our AWS accounts from a simple NodeJS script... you could then make it part of your morning startup routine to check for any rogue instances, stacks, secrets or any resource you can think of that might have been left on accidentally and could be charging you!