Problem-
You have multiple AWS account, one for Development, another for Testing and another for Production. You have provisioned EC2 servers in each of these accounts in multiple regions depending on your application deployment.
Now you want to list all the instances in all regions across all accounts using awscli. Also you want to export this data into csv format so that you can view it in excel. You want to list following data for each EC2-
-
Instance Name
-
Instance ID
-
VPC
-
Instance OS
-
Private IP
-
Instance Status
There is no way to get this data exported from AWS console.
Solution:
You can export this data into csv file which can be viewed in excel. You have to configure profile to access each of these AWS accounts. Then use the Profile names in the script provided.
for i in dev test prod do for region in `aws ec2 describe-regions --output text | cut -f4` do echo -e "\nListing Instances in region:'$region'..." aws ec2 describe-instances --region $region --profile $i --query "Reservations[*].Instances[*].{Name: Tags[?Key=='Name'] | [0].Value, instance_id: InstanceId, VPC: VpcId, ip_address: PrivateIpAddress, state: State.Name, Platform: Platform}" --output text | sed -E 's/\s+/,/g' > ec2/$i/$region.csv done done
Here is this script, I have 3 profiles configured- 1 each by name dev, test and prod. Using these profile name awscli is able to access EC2 instance data. Also it finds all the regions available in that AWS account and then fetches instances information with in that region. Then this data is saved in csv file in path ec2/<profilename>/<regionname>.csv
This way separate file for each region is created in the AWS account folder.
The output in folder will be like