Aws Ec2 Lambda Start Stop

What is AWS and EC2? Amazon states on their website: “Amazon Web Services (AWS) is a secure cloud services platform, offering compute power, database storage, content delivery and other functionality to help businesses scale and grow. Explore how millions of customers are currently leveraging AWS cloud products and solutions to build sophisticated applications with increased flexibility, scalability and reliability.

What this means to you: If you currently run a website on a shared hosting server or run it on a physical hardware server you could be selling yourself short. If you require a scalable enterprise solution then AWS is the right tool for you. When your site goes down frequently due to poor service or bandwidth then again AWS is right for you. If you want to only pay for what you use, then AWS is perfect for you.


There are many reasons for wanting to automatically stop and start EC2 (Elastic Compute Cloud) instances on AWS (Amazon Web Services). Here are some of them:

  • Allow instances to run during a specific period.
  • Only allow employees to work on staging instances within a certain time frame.
  • Reduce costs.
  • Prevent instances from loading up during the weekend.
  • All of the above.

The reason Think Forward Media needed to accomplish this was so that clients saved costs by disabling staging servers that weren’t being utilized outside of working hours.

Most server admins and agencies are already taking advantage of the ease of running EC2 instances in the AWS cloud. Since you can start and stop them, change the instance size and start them back up, copy them, change the storage size, and make snapshots of them; They are easy to use and provide an abundance of flexibility.

Having your EC2 instances start and stop so that they only run between work hours and not on weekends can be a bit of a puzzle. When you first look around within the AWS EC2 section you won’t find any auto start-stop feature. Instead your need to use some of the other features in conjunction with EC2 to accomplish this.

At first you’ll be introduced to some new technologies that are beyond the scope of this article but don’t be shy, it’s very easy to get it up and running.

I’ll outline what needs to get done and then I’ll show you in detail what to do.

Technologies we’ll use:

  • EC2 (your cloud server)
  • Lambda (compute service that lets you run code without worrying about servers)
  • CloudWatch (monitoring service for AWS)
  • Finally IAM (Identity and Access Management, control security access)

What we’ll be doing:

We’ll use CloudWatch events triggered by a specified time to run a Lambda function. Translated: We’ll start and stop EC2 instances using code – and it will be fun!

STEP 1 – EC2

You’ll need to make sure you have fired up an EC2 instance and that it is running. It doesn’t matter what is on the instance as long as it turns on. In our example we have fired up a test instance. This is the part of the article that you’ll need to have some knowledge about creating and running EC2 instances. If you know Step 1 then you are going to love the rest.

STEP 2 – LAMBDA STOP EC2

Now we are going to go the the Lambda console and create two functions. One for stopping our test EC2 instance and one for starting it.

[1] To get to the Lambda console click on ‘Services’ in the top right hand corner of the AWS console and click ‘Lambda’ under the ‘Compute’ section.

Finding Lambda

Note: If this is your first time using Lambda in AWS you’ll see this Getting Started screen. Click ‘Get Started Now’.

Lambda Getting Started

[2] Then click “Author from Scratch” to start your first function.

Lambda Author From Scratch

Configure triggers

[3] Before we create the function we are going to set up the trigger. Click on the dotted outline to the left of the Lambda icon:

Lambda Configure Triggers

[4] Select “CloudWatch Events” and then “Create a new rule”:

Lambda New Rule

[5] You should name the Rule something easy to remember self explanatory like “lambda-stop-ec2”. You can give it a description if you wish: “Lambda stop EC2”.

[6] For the “Rule type” this can get a bit tricky. I selected “Schedule expression” and choose to use a CRON expression “cron(0 22 * * ? *)”. Since the times are in UTC I put the hour at 22 (10pm at night) which is 18EST (6pm Eastern Standard Time). Remember that I wanted the EC2 to shut off automatically at 6pm as our developers usually leave around 5:30 each day. Half an hour gave enough time for employees to shut down their computers. You can of course set this to a later time to accommodate your needs.

cron(0 22 * * ? *) shuts the EC2 instance down at 6pm EST every day.

Lambda Cron

[7] Enable the trigger at the bottom of the page and click next.

Configure function

[8] Your your function a name: “EC2InstanceStop”, a description: “EC2 Instance Stop” and for Runtime select “Python 2.7”.

[9] Then in the Lambda function code section paste this in to stop your EC2 instance:
Import2
[10] You’ll need to replace the Region (‘XX-XXXXX-X’) and instance IDs (‘X-XXXXXXXX’) to match the ones you want to stop from the EC2 console window in AWS. One thing to note is that if you region ID is “us-east-1a” leave off the last letter and only put in “us-east-1”.

[11] Select “Create a custom role” from the “Lambda function handler and role” section:

Lambda Custom Role

[12] In the new window the opens enter “lambda-start-stop-ec2” for Role Name.

[13] Click “View Policy Document” and then “Edit”.

[14] Paste the following in the Edit window:
Version
[15] Click “Allow” when finished editing.

[16] From Advanced settings, input 10 seconds for the function timeout.

[17] Click “Next” and then “Create function” once complete.

[18] Finally click the “Code” tab so that we can test the function we have just created. Click the “Test” button and your instance should have stopped!


STEP 3 – LAMBDA START EC2

I’ll go a little faster now that you have the Stop EC2 function running.

To create the Start EC2 function click on “Functions” on the left hand side menu and then click “Create function”.

  • Click “Author from scratch”.
  • Add the “CloudWatch Events” trigger and select “Create a new rule”. Name it “lambda-start-ec2”. Description: “Lambda Start EC2”
  • For the CRON expression use “cron(30 12 ? * MON-FRI *)”, enable the trigger and click “Next”. This starts the EC2 instances Mon-Fri at 8:30EST / 12:30UTC.
  • For the function name use “EC2InstanceStart”, description use “EC2 Instance Start”, Runtime select “Python 2.7” and for the function code enter:
    Import
  • Make sure the “Handler” is set to “lambda_function.lambda_handler”.
  • For “Role” select “Choose an existing role” and from the “Existing role” dropdown select “lambda-start-stop-ec2” that you created earlier.
  • Under “Advanced settings” set the timeout to 10 seconds.
  • Click “Create Function”.
  • Next click the “Code” tab so that we can test the function we have just created. Click the “Test” button and your instance should have started!

You now have the ability to stop and start your EC2 instances in AWS thereby adding automation that leads to cost effectiveness improving your bottom line.


Contact Us Today!What to know more on how Think Forward Media can help grow your business and scale your website? No problem, feel free to contact us with any questions, concerns or issues!

Call today 705-302-1869