Get list of running instances in AWS emailed to you daily.

Say you share an AWS account with a few people and you’re the bill payer. You’d like to keep an eye on the running instances, but logging in each day to check can get tiring, especially if you are out and about and can’t get near a PC. A handy way to get the instances is to have a script running that gathers the instances and emails them to you. The below script does just that. Before you set up this script you will need set up AWS command line tools on your server. You can see how to to this in the blog post here.

 

 

 

#This is a bash script to get running instances from AWS and mail them on to the  relevent people

 

printf “Hi there,\n I am a bot so please don’t reply to me all I do is list  active EC2 instances in your AWS account! \n if my findings appear wrong please contact Darren In IT and he will ensure that they are corrected \n Running

 

Instances in EU-West-1 \n” > FileToBeMailed.txt

 

aws ec2 describe-instances –query ‘Reservations[*].Instances[*].[InstanceId]’ –filters Name=instance-state-name,Values=running –output text –region eu-west- 1 >> FileToBeMailed.txt

 

printf “\n Running Instance in us-east-1\n” >>FileToBeMailed.txt

 

aws ec2 describe-instances –query ‘Reservations[*].Instances[*].[InstanceId]’ –filters Name=instance-state-name,Values=running –output text –region us-east-1 >> FileToBeMailed.txt

 

printf “\n Running Instance in us-west-2\n” >> FileToBeMailed.txt

 

aws ec2 describe-instances –query ‘Reservations[*].Instances[*].[InstanceId]’ –filters Name=instance-state-name,Values=running –output text –region us-west- 2 >> FileToBeMailed.txt

 

printf “\n Running Instance in ap-northeast-2\n” >> FileToBeMailed.txt

 

aws ec2 describe-instances –query ‘Reservations[*].Instances[*].[InstanceId]’ –filters Name=instance-state-name,Values=running –output text –region ap-northeast-2 >> FileToBeMailed.txt

 

printf “\n Running Instance in ap-southeast-1\n” >> FileToBeMailed.txt

 

aws ec2 describe-instances –query ‘Reservations[*].Instances[*].[InstanceId]’ –filters Name=instance-state-name,Values=running –output text –region ap-southeast-1 >> FileToBeMailed.txt

 

printf “\n Running Instance in ap-southeast-2 \n” >> FileToBeMailed.txt

 

aws ec2 describe-instances –query ‘Reservations[*].Instances[*].[InstanceId]’ –filters Name=instance-state-name,Values=running –output text –region ap-southeast-2 >> FileToBeMailed.txt

 

printf “\n Running Instance in ap-northeast-1\n” >> FileToBeMailed.txt

 

aws ec2 describe-instances –query ‘Reservations[*].Instances[*].[InstanceId]’ –filters Name=instance-state-name,Values=running –output text –region ap-northeast-1 >> FileToBeMailed.txt

 

printf “\n Running Instance in eu-central-1 \n” >> FileToBeMailed.txt

 

aws ec2 describe-instances –query ‘Reservations[*].Instances[*].[InstanceId]’ –filters Name=instance-state-name,Values=running –output text –region eu-central-1 >> FileToBeMailed.txt

 

printf “\n I will mail again tomorrow with am updated list of instances” >> FileToBeMailed.txt

 

 

First we start of by typing the text we want to head our email. Here we state that we are a bot and that if there are any issues contact Darren who created the bot. The  “ \n “ that you see telling the script to start a new line. We then state what will be the first region that we will get details for by writing “\n Running Instances in EU-West-1 \. Once we have finished this paragraph we direct the output to a file called FileToBeMailed.txt

 

Next we start by issuing our AWS commands, basically, what we are saying here is get all the running instances in eu-west-1 and then append them to the end of the FileToBeMailed.txt. We do this for each AWS region, each time appending to the end of the FileToBeMailed.txt. This builds up the list of running instances that we can see. When we have listed all the instances we print that we will mail again tomorrow with the instances.

 

Now that we have the file produced we just need to send the contents of this file to an email account of our choice. To do this we can use send mail and is discussed in the post here that details how to use send mail to send emails to your own account.

 

Leave a Reply

Your email address will not be published. Required fields are marked *