We have a large number of EC2 instances running in AWS for about 1 year. Now we are trying to perform a clean up activity for he unused instances and based on a username using which we have launched instances.
I have tried downloading the cloudtrail logs from S3 Bucket and tried filtering the username and 'RunInstances' event so that i may find the User who launched the instance and also the instance details.
The following is the script i used to download all the cloudtrail logs into a single folder, unzip them and filter the instances by 'RunInstances' event and give a count of instances.
I need help on retrieving the usernames from each log wiht 'RunInstances' event and stopping the instances.
My script:
#!bin/sh
s3url="s3://S3bucket/AWSCloudtrailLogs/<accountno>/CloudTrail/region/2016/"
for (( i=1; i<=12; i++ ))
do
for (( j=1; j<=31; j++ ))
do
if [[ $i -le 9 && $j -le 9 ]]
then
aws s3 cp $s3url/0$i/0$j/ ~/test/ --recursive
elif [[ $i -le 9 && $j -ge 10 ]]
then
aws s3 cp $s3url/0$i/$j/ ~/test/ --recursive
elif [[ $i -ge 10 && $j -le 9 ]]
then
aws s3 cp $s3url/$i/0$j/ ~/test/ --recursive
elif [[ $i -ge 10 && $j -ge 10 ]]
then
aws s3 cp $s3url/$i/$j/ ~/test/ --recursive
fi
done
done
for v in `ls ~/test/` ; do gunzip $v ; done
for v in `ls ~/test/` ; do cat ~/test/$v | grep RunInstances >> ~/test/result.txt; done
grep -o 'RunInstances' ~/test/result.txt | wc -l
Is there anyway i can do it without downloading the zip files and directly get info from s3 bucket itself? Because this is taking a lot of time as we are having about over 1 million log files.
I need a way to figure this out with any programming language or script.
Thanks for your support.