Tuesday 2 April 2019

How to do Cost Optimization of our AWS environment by removing under utilized or not used resources by Raj Gupta




This script is useful for reducing the unnecessary cost incurred while using AWS
It checks:-


1.For all the idle load balancers in your account
2.Checks the number of RDS connections in the past 1 week
3.Identifies all the idle EBS Volumes
4.Checks for all the Legacy instances which are being used
5.Checks whether the ec2 instances are being fully utilized or not
6.Checks all the unassociated Elastic IP's


Take any Linux system and run the below command:-

[ec2-user@ip-172-31-81-19 ~]$ sudo yum update   --This command will update the system

[ec2-user@ip-172-31-81-19 ~]$ sudo pip install --upgrade awscli  ---This will install awscli to our Linux server

[ec2-user@ip-172-31-81-19 ~]$ aws configure  ----This will use to 
configure the Access key ID,Secret access key,Region,Output format, you need to give as per your data
  
[ec2-user@ip-172-31-81-19 ~]$ sudo python -m pip install xlwt   ---This package we need to install because we are going to use in our code to send output in excl

[ec2-user@ip-172-31-81-19 ~]$ vi raj2.py    -------then create a file let say raj and copy the below python code and save it. I have kept python code in my github account in below path.

             
[ec2-user@ip-172-31-81-19 ~]$ python raj2.py  ------This will run the your python code and create output in your system

[ec2-user@ip-172-31-81-19 ~]$ sudo yum install mailx -y    ----This will help us to send output to mailbox

Run the below command to send the output to your mail  in my case i am sending to my mail box(rajkumargupta14@gmail.com)

[ec2-user@ip-172-31-81-19 ~]$ echo 'These are contents of my mail' | mailx -s 'This is my email subject' -a /home/ec2-user/cost_optimization.xls rajkumargupta14@gmail.com

/home/ec2-user/cost_optimization.xls      ----this is path in my system where output saved after python code completed 


Now After running the avabe command you will get output as below ,I have kept in my github account:-

https://github.com/rajkumargupta14/raj/blob/master/cost_optimization_Output.xls

Then check the output and delete the under utilized resources to save the cost



1 comment: