Riptide Blog

​Auto-on/off with AWS Lambda

Posted by Jose O'farril on July 7, 2016

Scheduled tasks and Cron jobs have always been a staple in a Systems Administrator toolkit. While workloads move to cloud platform, so do our tools. Below I’ll show one way of tackling the promise of being able to turn schedule EC2 resources to auto start and auto shutdown with AWS Lambda service.

AWS Lambda service

As of late 2015 AWS added a Scheduled Event feature in their Lambda service. This included a Cron feature. At the same time they also included support for Python, which may be more familiar to a SysAdmin/Engineer. At least is was in my case. So this example will use the AWS Python SDK (boto3).

Here’s the code we’ll be working with. The configurable fields are in the filters, here we add the tag values of our EC2 instance. In this use case, I’ve added the following tag to each of the EC2 instances I want to automate. Auto-on = True

import boto3
import logging

# setup simple logging for INFO
logger = logging.getLogger()

# define the connection
ec2 = boto3.resource('ec2')

def lambda_handler(event, context):
# We'll use boto's filtering method to find
# all stopped EC2 instances with our autotag value
filters = [{
'Name': 'tag:Auto-on',
'Values': ['True']
'Name': 'instance-state-name',
'Values': ['stopped']

# Start the filtering
instances = ec2.instances.filter(Filters=filters)

# Get all the instances that are off
StoppedInstances = [instance.id for instance in instances]

# print these for the log
print StoppedInstances

# Check to see if there are any stopped instances.
if len(StoppedInstances) > 0:
#Start'em Up
startingUp = ec2.instances.filter(InstanceIds=StoppedInstances).start()
print startingUp
print "Nothing to see here"

Setting up our Lambda function

When you create your lambda function you’ll see an option to select a template. In our use case we’re going to skip these, but they are a great resource to see basic usage for lambda.

In the Configure Function screen we’re going to give our function the following options

  • Name (something easily recognizable
  • Description (Anything that helps tell you what’s here
  • Runtime (Python 2.7)

And we’ll copy/paste our code into the inline editor.

We’ll leave the Handler name as is since we’re using the default lambda_hander name.
Under Role we’ll create a new Basic Execution Role (A new screen will open).

In the new screen create the new IAM role and name it something you’ll understand. While this is the default Lambda function to be able to write into CloudWatch Logs, we need to add in permission to interact with EC2. We’ll add the following policy to the new role.

"Sid": "Stmt1467215252000",
"Effect": "Allow",
"Action": [
"Resource": [

Back in the lambda configuration we’ll select our new policy. We’ll increase the timeout to 1 min or so since we’re interacting with the AWS API’s. We can leave the No VCP option since the API isn’t an internal resource to AWS.

Event Source

Now that we have the lambda function all loaded, lets set up the event source. In our case I want to start the EC2 instances at 11am UTC. Fill in the following options.

  • Event Source Type (Scheduled)
  • Rule Name (Custom Name)
  • Description of the Rule
  • Schedule Expression (Lambda uses more of the Java Expression of cron found here)

Testing it all out

If you have a test instance or 2 in a stopped state in your environment we can initiate a test. Make sure your instances have a custom tag of Auto-on = True or whatever your chose to call your tag.
In the input test even we’ll select Scheduled Event from the the drop down. In this case the parameters in the can remain as the default since we’re not pulling any of the event message data into our function. Save the Test and run.

If all goes well you should have an output similar to this.

Where the log output shows that instances have been started.

START RequestId: 45897b28-3e12-11e6-a4cd-bd2a1889586e Version: $LATEST
[INFO] 2016-06-29T15:58:11.259Z 45897b28-3e12-11e6-a4cd-bd2a1889586e Calling paginated ec2:describe_instances with {'Filters': [{'Values': ['True'], 'Name': 'tag:Auto-on'}, {'Values': ['stopped'], 'Name': 'instance-state-name'}]}
[INFO] 2016-06-29T15:58:11.325Z 45897b28-3e12-11e6-a4cd-bd2a1889586e Starting new HTTPS connection (1): ec2.us-east-1.amazonaws.com
['i-4a14b3da', 'i-4b14b3db']
[INFO] 2016-06-29T15:58:12.0Z 45897b28-3e12-11e6-a4cd-bd2a1889586e Calling paginated ec2:describe_instances with {'InstanceIds': ['i-4a14b3da', 'i-4b14b3db']}
[INFO] 2016-06-29T15:58:12.199Z 45897b28-3e12-11e6-a4cd-bd2a1889586e Calling ec2:start_instances with {u'InstanceIds': ['i-4a14b3da', 'i-4b14b3db']}
[{u'StartingInstances': [{u'InstanceId': 'i-4b14b3db', u'CurrentState': {u'Code': 0, u'Name': 'pending'}, u'PreviousState': {u'Code': 80, u'Name': 'stopped'}}, {u'InstanceId': 'i-4a14b3da', u'CurrentState': {u'Code': 0, u'Name': 'pending'}, u'PreviousState': {u'Code': 80, u'Name': 'stopped'}}], 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '84e54c5f-d5c1-44d1-a935-ab24d37dddf0'}}]
END RequestId: 45897b28-3e12-11e6-a4cd-bd2a1889586e
REPORT RequestId: 45897b28-3e12-11e6-a4cd-bd2a1889586e Duration: 1450.06 ms Billed Duration: 1500 ms Memory Size: 128 MB Max Memory Used: 61 MB

Adding a Lambda function to stop instances follows the same steps expect change the tag and the code has a couple changes to stop instead of start the instances.

import boto3
import logging

# setup simple logging for INFO
logger = logging.getLogger()

# Connection
ec2 = boto3.resource('ec2')

def lambda_handler(event, context):

# all running EC2 instances.
filters = [{
'Name': 'tag:AutoOff',
'Values': ['True']
'Name': 'instance-state-name',
'Values': ['running']

# filter the instances
instances = ec2.instances.filter(Filters=filters)

# locate all running instances
RunningInstances = [instance.id for instance in instances]

# print the instances for logging purposes
print RunningInstances

# make sure there are actually instances to shut down.
if len(RunningInstances) > 0:
#perform the shutdown
shuttingDown = ec2.instances.filter(InstanceIds=RunningInstances).stop()
print shuttingDown
print "Nothing to see here"

If you are looking for AWS solutions or need advice from our AWS Cloud Architect give us a shout!
Read More

Topics: AWS, AWS Lambda service, AWS Solution Architect, Cloud/IoT

Successful Digital Transformation = Positive Impact on Revenue

Posted by Marketing on June 20, 2016

A recent survey conducted by Mulesoft concluded that most businesses are trying to understand how to work with new channels, engagement, and ubiquity. Nearly ¾ of IT decision makers said that if digital transformation initiatives weren’t completed, it would have a negative impact on revenue in three months.

Read More

Topics: API, Cloud/IoT, Digital Transformation, IoT, Mobile, Mule ESB

AWS IoT Service is a Simple Yet Powerful IoT Backend

Posted by Cesar Gonzalez on April 8, 2016

The Internet of Things (IoT) is a topic that I’m passionate about, I envision massive opportunities for the development of new technologies on what this new wave of innovation is bringing. The possibility of an interconnected world will lead us to a big jump into what a lot of us have just only dreamed to become a reality.

Read More

Topics: AWS, AWS IoT, Cloud/IoT, HTTP, IoT, MQTT, WebSockets

Security of IoT

Posted by Marketing on January 28, 2016

This past week, the internet was up in a storm over an article on Shodun’s recently launched
search engine for IoT. What this search engine exposes is that most web cameras home owners have are highly vulnerable to prying eyes. The feed includes images of backyards, insides of homes, and even sleeping babies.

Read More

Topics: Cloud/IoT, FTC, Internet of Things, IoT

AWS Cloud’s High Availability Weathers Through Storms

Posted by Marketing on January 26, 2016

While many are weathering through winter storm that is passing through the Northeast, AWS is also ensuring that their ducks are all in a row.  For  those that are wondering what that entails, here it is, straight from the AWS Blog:

Read More

Topics: Amazon Solutions Partner, Amazon Web Services, AWS, Cloud Computing, Cloud/IoT

Why Cloud Integration Challenges Remain an Issue

Posted by Marketing on January 14, 2016

The cloud has quickly become a pillar in IT departments with the public cloud market reaching over $45 billion and the private cloud services surpassing $24 billion in the next year. A recent report stated that 41% cite cloud as a catalyst for new directions requiring business transformation. Although cloud adoption rates are high and market numbers are continuing to grow, there is still some confusion related to cloud computing.

Read More

Topics: AWS, Cloud Integration, Cloud Migration, Cloud/IoT, Hybrid Cloud

IoT and CES

Posted by Marketing on January 13, 2016

Last week CES held its annual conference in Las Vegas, the breakout star of the conference, of course, was emerging Internet of Things (IoT) technology.

Read More

Topics: CES, Cloud/IoT, Internet of Things, IoT

Digital Transformation: API Connectivity

Posted by Marketing on January 5, 2016

Digital Transformation is a hot topic for businesses and consumers in 2016 as APIs, cloud, and mobile technologies continue to disrupt IT-enabled businesses. From smart fridges to connected thermostats, and apps for basically all your needs -- where do you stand in the age of digital transformation?

Read More

Topics: API Connectivity, Cloud/IoT, Digital Transformation, Mule ESB, Mule’s Enterprise Service Bus, Service Oriented Architecture, SOA

What's the Value of An API Anyway?

Posted by Marketing on December 16, 2015

There’s no doubt that APIs make companies and applications more efficient. They connect with increasingly demanding consumers and are a source of revenue as well. A recent report from Mulesoft showed, 50 percent of large companies (10,000 employees or more) are making more than 10 million dollars in annual revenue from APIs. With such a large revenue stream, don’t you wonder what’s the value of an API?

Read More

Topics: API, API Costs, Cloud Computing, Cloud/IoT

Technologies that Will Transform Your Enterprise

Posted by Marketing on December 2, 2015

It may seem that all the emerging innovations from the tech sector won’t have an impact on businesses, but that is far from the truth. Any business can can take advantage of digital transformation. In a piece by Computer Weekly, three technologies were described as beneficial for the enterprise.

Read More

Topics: 3D Printing, API, Cloud/IoT, Digital Transformation, Sensors, Solutions

Request a Free Consultation

Subscribe to Email Updates

Posts by Topic

see all