bmoreira18
bmoreira18

Reputation: 143

List all EC2 Instances - AWSCLI

I want to list all ec2 instances for each account we have on ~/.aws/credentials, but the aws ec2 describe-instances doesn't return anything.

ACC=$(cat ~/.aws/credentials | egrep "\[.*\]" | cut -d '[' -f2 | cut -d ']' -f1 | grep -v default | sort)
FILE="${HOME}/ec2.csv"

echo -e "ACCOUNT;EC2" > ${FILE}

for account in ${ACC}
do
    for region in $(aws ec2 describe-regions --profile default --region sa-east-1 --output text | cut -f3)
    do
        EC2=$(aws ec2 describe-instances --profile ${account} --region ${region} --query "Reservations[*].Instances[*].{PrivateIP:PrivateDnsName,Name:Tags[?Key=='Name']|[0].Value,Status:State.Name}" --output text)
        echo -e "${account};${EC2}" >> ${FILE}
    done
done

Upvotes: 0

Views: 845

Answers (1)

bmoreira18
bmoreira18

Reputation: 143

i solved this issue replacing the default profile on the second for.

for region in $(aws ec2 describe-regions --profile ${account} --region sa-east-1 --output text | cut -f3)

This script works, i hope this script can help's.

Upvotes: 1

Related Questions