Friday, October 27

Amazon Web Service - Start / Stop EC2 Instance on schedule # Save Costs!

Amazon charges for their EC2 instances on per-hour basis (and now per second basis also!).

So you want to save costs be stopping your EC2 Instances during the off-hours (nights, weekends etc.)?

This post is going to describe a fairly easy process for this setup using AWS Lambda and CloudWatch

Setup

We would be triggering a Lambda function from CloudWatch Scheduler which will in turn start/stop your EC2 instances based on the Tags.

Diag 1: EC2 Scheduler Setup

Note: First one million Lambda requests per month are free.

Let's jump to the process now...

Create an IAM Role for Lambda

In order to manage the EC2 Instances using Lambda, we will have to create an IAM Role which we would later attach to the Lambda Function

  1. Under IAM Roles, select Create Role
  2. Select Lambda as the trusted entity for this role
  3. On the permissions page, select Create policy. This would open up a new window
    • Select Create Your Own Policy
    • Enter a suitable policy name and description
    • Enter the following JSON code in the Policy Document
    • {
          "Version": "2012-10-17",
          "Statement": [
              {
                  "Effect": "Allow",
                  "Action": [
                      "logs:CreateLogGroup",
                      "logs:CreateLogStream",
                      "logs:PutLogEvents"
                  ],
                  "Resource": "arn:aws:logs:*:*:*"
              },
              {
                  "Effect": "Allow",
                  "Action": [
                      "ec2:DescribeInstances",
                      "ec2:StartInstances",
                      "ec2:StopInstances"
                  ],
                  "Resource": "*"
              }
          ]
      }
      
      
      Validate and save the policy
  4. Now go back to the previous window (Create Role) and attach the policy you created to the role
    Note: If the policy is not visible yet, try refreshing the list
  5. Provide a suitable Role name and description on the review screen and create the role.

Create the Lambda Function

For this purpose, I would be using Python code inside Lambda using Python Boto3 libraries. Don't worry if you are not familiar with Python. The instruction here should suffice!

  1. Go to AWS Lambda Console and Create function
  2. For our purpose, we will Author from scratch
  3. Provide a suitable name to the Lambda Function and Choose an existing role (the one we have created in the last step)
  4. Diag 2: Lambda Function Definition
  5. Configure the Lambda Function with following details:
    Code entry type: Edit code inline
    Runtime: Python 2.7
    Handler: lambda_function.lambda_handler
  6. In the code block, clear out everything and add the code from the following URLs

    Stop EC2
    Start EC2

    NOTE
    • Python is indentation sensitive. Please be careful with the copy-paste
    • The script will action on the EC2 instances with a specific tag (will setup this in next section)
    
    
  7. In the Basic settings, increase the timeout from 3 sec to 10 sec.
  8. Save the function.
Repeat Steps 1-7 for creating another function to Schedule Start of EC2 Instances.

Setting up CloudWatch Trigger

In the Lambda Function, select Triggers tab and Add a new trigger

Select CloudWatch Events as the Source

  1. Create a new rule
  2. Specify appropriate Rule name and description
  3. You can enter schedule expression as per your need to invoke the trigger

    For Example:
    cron(00 23 ? * MON-FRI *)

    // This will invoke the Lambda function every Monday-Friday at 23:00 PM GMT
Diag 3: CloudWatch Trigger Setup

Tagging EC2 Instance

The last and most simplest part of this setup is to tell Lambda which EC2 Instances to stop/start.
This can simply be done by defining the following Tag in your EC2 Instances

Name: AutoRestart
Value: True

Diag 4: EC2 Tagging


And that's it!!

Cheers!

Note: In the coming week's I'll be posting another blog on scheduling RDS Start/Stop... Stay Tuned!

2 comments:

  1. Do we have any other methods available for these scheduled intervals rather than lambda?

    Thanks
    sai

    ReplyDelete
  2. I chose Lambda because it's easy to setup and free (server less, so doesn't adds up the costs). If you are looking for other options - you can spin up an EC2 instance and setup the Python scripts to run from there on schedule.

    ReplyDelete