Cost Optimization In AWS

One of the salient features of the cloud is cost saving. It has successfully proven to offer reduced IT costs and is one of the reasons for revolutionary cloud adoption. Apart from saving the capital expense of buying the servers and managing them, one can also save the operational expense in the cloud which a lot of enterprises are not aware of. This can be achieved using automation.
 
In this article, I will be covering one use case to save the cost of your infrastructure in the cloud. There are many use cases for cost saving. Meanwhile, if you are interested in creating AWS accounts (MultiAccount Strategy) and infrastructure using CLI, you can view my following article.
 
To reduce your AWS bills, you can start with stopping your development servers on weekends and non-business hours, and restarting them again automatically. Doing this manually is an overhead and if you have multiple servers, this will be cumbersome. This can be automated by using the Boto3 library of Python.
 
Boto3 is the AWS SDK for Python. It has functions for various API calls for AWS Services. If you are a Python developer, Boto3 is very easy to learn. The AWS documentation for Boto3 is very rich. To get started with Boto3, visit here.
 
We can write a Python code to automatically start and stop the server. To run your code, AWS Lambda is the best option, as it is serverless (Run code without provisioning). Below is the sample code to automatically stop the instance. It's not about the code, it's about the approach that can be used for various AWS services to reduce dollars in the bill.
  1. import json  
  2. import boto3  
  3.   
  4. def lambda_handler(event, context):  
  5.   
  6.     ec2 = boto3.resource('ec2')  
  7.     Running_instances = ec2.instances.filter(  
  8.     Filters=[{'Name''instance-state-name''Values': ['running']},  
  9.     {  
  10.     'Name''tag:Schedule',  
  11.     'Values': ['true']  
  12.     }  
  13.     ])  
  14.               
  15.            for instance in Running_instances:  
  16.              ec2.instances.filter(InstanceIds=[instance.id]).stop()  
To summarize, we have filtered all the instances that are running, and have a particular tag. This tag is useful to differentiate that particular instance from other running instances and then, stopping all the filtered instances. Just create a lambda function, attach the required read-only policy to the lambda role (Avoid giving full access roles). In this case, it is ‘AmazonEC2ReadOnlyAccess’.
 
But this is half of the story. To stop the instance in non-business hours, we need a cron job. A cloudwatch rule can be used to trigger the lambda function when we need to stop the function.
 
Under Services, go to cloudwatch and create a rule. Click "schedule" and then cron expression.
 
30 9 * * ? *
 
This cron gets triggered every day at 9:30 GMT. Set this to fit your requirements.
 
Now, the last step is to add a trigger to your lambda function. Add a trigger to Lambda, select event as CloudWatch events, select the rule.
 
We are done here. The lambda will be triggered every day at 9:30 GMT and will stop the required instance. Similarly, the servers can be started automatically.
 
AWS+Python+Boto3 is your combination to save significant costs. This combination can also be used to achieve various objectives.


Similar Articles