简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM