<commit_before>---
layout: post
title: Save cost on AWS using Roham
subtitle: Automate stop/start/termination of your EC2 Instances based on schedule tags
category: dev
tags: [howto, cloud, devops]
author: <NAME>
author_email: <EMAIL>
header-img: "images/new/Exportiert_42.jpg"
---

<p align="center">
  <img width="200" height="200" src="https://github.com/esarabadani/Roham/blob/master/Images/Roham_Logo.png">
</p>
<p align="center"> Roham GitHub Repo: https://github.com/esarabadani/Roham </p> 

# What is Roham?
Roham saves you cost on AWS by stopping/terminating/starting Instances on schedules defined by you. Roham gives you these benefits:

  - It helps you to stop/start/terminate EC2 Instances based on schedule tags
  - Schedule tags follow cron expressions in Linux which are simple to learn
  - It can be implemented across different AWS accounts to be manage centrally
  - Roham can create tags on your EC2 Instances automatically in case people forget to
  - It is a free open-source project and costs you almost nothing on AWS
  - It is simple to implement by just following the implementation guide

> The word 'Roham' refers to a well-known hero in Persian legends. It also literally means 'undefeatable'. 

# Why did we develop Roham?
Like many other enterprises we also have the problem of having too many unneeded EC2 Instances which are simply forgotten to be stopped/terminated in different AWS Accounts and simply account for nearly 30% (if not more) of the total cost we pay in the cloud. So it is clear that we need a way to automatically find such Instances and terminate/stop them.

If you simply search for such tools, you will probably come across many, but the problem is they are either too complicated to understand/implement, or they simply do not fit into our environment. So we decided to create our own tool to fix the problem. It has been tried to provide a complete documentation on how it works and should be implemented. In case you still have questions about it, please do not hesitate to leave a comment and simply ask. 

# Architecture and Components
Roham consists of the following Lambda functions which are written in Python:

  - **Roham Tagger**: It automatically tags EC2 Instances to stop/terminate on a schedule in case they are not properly tagged. 
  - **Roham Terminator**: It terminates EC2 Instances which are tagged to terminate. 
  - **Roham Stopper**: It stops (shuts down) EC2 instances which are tagged to stop at a specified time. 
  - **Roham Starter**: It starts EC2 instances which are tagged to start at a specified time. 
  
> These four Lambda functions above are independent of one another and could be independently enabled/disabled, even though it is best to enable all of them together to get the best cost-saving results.  

Our recommendation is to create a central Shared Services AWS Account and create/import these Lambda functions there and then control tagging/termination/stop/start of your EC2 Instances in all your other AWS project Accounts. The diagram below shows the big picture:

<p align="center">
  <img width="450" height="652" src="https://github.com/esarabadani/Roham/blob/master/Images/big-picture.png">
</p>

The diagram and the steps below show how Roham (in this case Roham Stopper) works across AWS Accounts and takes actions on EC2 Instances:

<p align="center">
  <img width="824" height="363" src="https://github.com/esarabadani/Roham/blob/master/Images/small-picture.png">
</p>

  1. CloudWatch Event triggers the SNS Topic and passes the IAM Role ARN to it. 
  2. SNS Topic publishes a message to Roham Stopper Lambda function (in the Shared Services AWS Account) and passes the IAM Role ARN as a message to it.
  3. Roham Stopper Lambda function assumes the given IAM Role (in the Project1 Account)
  4. Request for Role assumption is accepted and Rohan Stopper is allowed to take actions on EC2 Instances.
  5. Roham Stopper reads and interprets the tags on the Instances and takes actions accordingly. 
  
> The same concept in the steps above applies to the other three Lambda functions. This means every project AWS Account will have a separate CloudWatch Rule and a separate SNS Topic for each Lambda function. Please see the diagram below:

<p align="center">
  <img width="824" height="363" src="https://github.com/esarabadani/Roham/blob/master/Images/Account_Resources_View.png">
</p>

# Implementation
There is a complete step-by-step guide on how to implement Roham in your AWS environment here [on this page](http://thebluenode.com/roham).

# Tagging Scheme
Once you follow the steps in the implementation guide and everything is done, now it is time to tag your EC2 Instances. Please follow the tagging scheme in the table below:
  
| Tag Key | Tag Value | Definition |<commit_msg>Quick fixes with images, centering and some minor other things (with Esmaeil)<commit_after>---
layout: post
title: Save cost on AWS using Roham
subtitle: Automate stop/start/termination of your EC2 Instances based on schedule tags
category: dev
tags: [howto, cloud, devops]
author: <NAME>
author_email: <EMAIL>
header-img: "images/new/Exportiert_42.jpg"
---

{:.center}
![Roham Logo](https://github.com/esarabadani/Roham/blob/master/Images/Roham_Logo.png?raw=true){:style="margin:auto" width="200px" height="200px"}

# What is Roham?
Roham saves you cost on AWS by stopping/terminating/starting Instances on schedules defined by you. Roham gives you these benefits:

  - It helps you to stop/start/terminate EC2 Instances based on schedule tags
  - Schedule tags follow cron expressions in Linux which are simple to learn
  - It can be implemented across different AWS accounts to be manage centrally
  - Roham can create tags on your EC2 Instances automatically in case people forget to
  - It is a free open-source project and costs you almost nothing on AWS
  - It is simple to implement by just following the implementation guide

> The word 'Roham' refers to a well-known hero in Persian legends. It also literally means 'undefeatable'. 

Roham GitHub Repo: [github.com/esarabadani/Roham](https://github.com/esarabadani/Roham)

# Why did we develop Roham?
Like many other enterprises we also have the problem of having too many unneeded EC2 Instances which are simply forgotten to be stopped/terminated in different AWS Accounts and simply account for nearly 30% (if not more) of the total cost we pay in the cloud. So it is clear that we need a way to automatically find such Instances and terminate/stop them.

If you simply search for such tools, you will probably come across many, but the problem is they are either too complicated to understand/implement, or they simply do not fit into our environment. So we decided to create our own tool to fix the problem. It has been tried to provide a complete documentation on how it works and should be implemented. In case you still have questions about it, please do not hesitate to leave a comment and simply ask. 

# Architecture and Components
Roham consists of the following Lambda functions which are written in Python:

  - **Roham Tagger**: It automatically tags EC2 Instances to stop/terminate on a schedule in case they are not properly tagged. 
  - **Roham Terminator**: It terminates EC2 Instances which are tagged to terminate. 
  - **Roham Stopper**: It stops (shuts down) EC2 instances which are tagged to stop at a specified time. 
  - **Roham Starter**: It starts EC2 instances which are tagged to start at a specified time. 
  
> These four Lambda functions above are independent of one another and could be independently enabled/disabled, even though it is best to enable all of them together to get the best cost-saving results.  

Our recommendation is to create a central Shared Services AWS Account and create/import these Lambda functions there and then control tagging/termination/stop/start of your EC2 Instances in all your other AWS project Accounts. The diagram below shows the big picture:

![Roham Big Picture](https://github.com/esarabadani/Roham/blob/master/Images/big-picture.png?raw=true)

The diagram and the steps below show how Roham (in this case Roham Stopper) works across AWS Accounts and takes actions on EC2 Instances:

![Roham Small Picture](https://github.com/esarabadani/Roham/blob/master/Images/small-picture.png?raw=true)

  1. CloudWatch Event triggers the SNS Topic and passes the IAM Role ARN to it. 
  2. SNS Topic publishes a message to Roham Stopper Lambda function (in the Shared Services AWS Account) and passes the IAM Role ARN as a message to it.
  3. Roham Stopper Lambda function assumes the given IAM Role (in the Project1 Account)
  4. Request for Role assumption is accepted and Rohan Stopper is allowed to take actions on EC2 Instances.
  5. Roham Stopper reads and interprets the tags on the Instances and takes actions accordingly. 
  
> The same concept in the steps above applies to the other three Lambda functions. This means every project AWS Account will have a separate CloudWatch Rule and a separate SNS Topic for each Lambda function. Please see the diagram below:

![Roham Account Resources](https://github.com/esarabadani/Roham/blob/master/Images/Account_Resources_View.png?raw=true)

# Implementation
There is a complete step-by-step guide on how to implement Roham in your AWS environment here [on this page](http://thebluenode.com/roham).

# Tagging Scheme
Once you follow the steps in the implementation guide and everything is done, now it is time to tag your EC2 Instances. Please follow the tagging scheme in the table below:
  
| Tag Key | Tag Value | Definition |
{'fraction_non_alphanumeric': 0.05863972502864285, 'fraction_numerical': 0.0077075304655765025, 'mean_word_length': 4.334444444444444, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1884222', 'n_tokens_mistral': 2601, 'n_tokens_neox': 2492, 'n_words': 1368}