Using Lambdas to start/stop EC2 instances

Dave MacDonald
Aug 19, 2019 · 4 min read

Here we show you how to start and stop your EC2 instances on an automated cycle, so you can save money for your team when the instances are not being used. Let’s say that you want them stopped at night and they should be back up and running when you start work the next morning. AWS Instance Scheduler is one solution, but here we show how easy this is to do with the Serverless Framework.

This framework makes it easy for us to write and deploy Lambda functions to start and stop the EC2 instances, and have them invoked on a schedule using cron-like CloudWatch Events. The Lambdas will only stop EC2 instances if they are given the tag:value Sleepy:True. We’ll also add an SNS notification so that you get an email when instances are going to sleep and waking up.

Ok, let’s get started!

Install the Serverless Framework

If you haven’t used the Serverless Framework before, have no fear. Think of this as a hands-on intro to the tool. It is a great way to create and deploy serverless functions to a cloud provider — in our case to deploy Lambda functions to AWS.

If you do not have the AWS CLI installed, install it first. For this article, we’re going to assume that you have admin AWS credentials in your ~/.aws/credentials under a profile called admin. It should look something like this:


In your console, run export AWS_PROFILE=admin. If you want to use a more limited set of credentials, see here. Now install the serverless framework

npm install -g serverless

Create a new Serverless project

serverless create -template aws-nodejs -path sleepmode
cd sleepmode

This will create two files, handler.js and serverless.yml:

Declare the functions used by your service

Now we’ll use serverless.yml to declare two serverless functions, goodmorning and goodnight, for starting and stopping EC2 instances. Here is the YAML for it:

Raw Source:

If you look into the YAML above, you can see that we are automating the invocation of these functions on a schedule. The schedule is given for each function using cron syntax with hours adjusted to UTC. During deployment, Serverless will set up a CloudWatch Event for each function, configured to invoke it at the scheduled time.

Above, we also set the cloud provider to aws, the runtime to nodejs10.x, and configure a few permissions needed for the functions in our service.

In the environment section, we provide some configuration variables, including the SNS topic where the Lambdas will send notifications when instances are stopped or started. You will need to provide your own SNS topic here — if you don’t have one, log into AWS, go to Simple Notification Service, and create a topic and a subscription to that topic for your email address, and get the ARN for the topic from there. It’s really simple. If you’d like to skip it though, just comment out the SNS_TOPIC_ARN.

handler.js (full source here:

Raw Source:

Deploy the Lambdas

You can deploy your new service using the Serverless Framework’s CLI as follows:

serverless deploy

The first deployment will normally take a few minutes since it creating your service using AWS CloudFormation in the background. If you need to clean up the resources you created, just log into AWS, open up CloudFormation, and delete the stack that was created containing the resources that were deployed.

To list the functions you have deployed:

serverless deploy list functions

To redeploy a function after making change:

serverless deploy function -f <functionName>

To run the function locally, using your local credentials:

serverless invoke local -f <functionName>

To invoke the deployed function (you won’t need to use this since our function is triggered by a scheduled CloudWatch Event):

serverless invoke -f <functionName>

Quick tip: To save a few keystrokes, you can type sls instead of serverless when using the CLI.

Enjoy — have fun with it! Now you can sit back, relax, and think about what to improve next with your powerful new Serverless toolkit at hand. Now you can go and add Sleepy = True tags to your EC2 instances and they’ll shut down in the evening and start back up in the morning.

The full code for this can be found on GitHub here:

More From Medium

Also tagged Serverless Framework

Also tagged Serverless Framework

Serverless alias insight

Mar 14 · 5 min read


Top on Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade