Monday, September 3, 2018

Stop AWS EC2 with automated Lambda script



Most frequently used resources or services in AWS cloud stands to be S3 buckets and EC2 instances

We always create, use and connect to EC2 instances and leave it there which costs us, if not free-tier or if we are beyond the free tier range


One of the solution option as a CloudAdmin or Sysopsadmin, we can create a lambda function using python 

How to use this script:
  1. Login to your AWS account
  2. Open EC2 console screen and open and add the tag value 'Stop' as key and 'Value:yes' if EC2 instances has to be stopped automatically; Script will respond only to this key-value tags, and if you need, you can amend the script to read other tag[key-values]
  3. Download the script from my github public account[https://github.com/gvnirmalaa/utilities/blob/master/l_stopEc2_UseTags.py]
  4. Create new lambda function in AWS console and place this code after selecting script as Pythonxx
  5. Now tag the cloud watch event 
  6. Under cloud watch -> create rule -> Event source(scheduler) -> here set the time lines to stop the EC2 in your account, according to your requirement
  7. Under 'Add Target' -> Lambda function -> select the new lambda function you just created in step 2
  8. Click on configure details and yes, an automated EC2 stop instance action is automated
Note: My requirement was to only stop the instances and the respective team or developer was supposed to start once they start work(to stay most optimum :-))



2 comments: