The Cloud Switch: IoT Button, Lambda, and CloudFormation

Andreas Wittig – 23 Aug 2017

Last one out turns off the light. What works for boring light bulbs can be adopted to your cloud infrastructure as well. Are you using a development and testing environment that is only used during working hours? Why not turning off the cloud infrastructure with the press of a button when the last one leaves the office?

Last one out turns off the light.

Sounds like magic? It is not. Launching and terminating your cloud infrastructure with the press of a button can be achieved by combining the following building blocks:

  • AWS IoT Button: the physical button connected to your Wifi sends events to AWS.
  • AWS Lambda: the runtime environment to execute a small piece of Node.js code triggered by events from the IoT button.
  • AWS CloudFormation: used to create and delete all parts of your development and testing infrastructure in an automated way.

Before you get started. Order an AWS IoT Button. After your friendly parcel carrier handed over the package with your own IoT Button you are ready to go.

IoT Button

Creating a CloudFormation template

First of all, you need a CloudFormation template describing your development and testing environment. Upload your template to S3 and note down its URL.

Looking for inspiration? Check out our collection of useful CloudFormation templates.

Creating an IAM Role

The Lambda function needs to be authorized to create and delete CloudFormation stacks and all the resources defined within the template on your behalf.

Create a new IAM Role to do so.

  1. Go to Identity and Access Management (IAM).
  2. Select Roles from the sub navigation.
  3. Press the Create new role button.
  4. Select AWS Lambda as role type.
  5. Select the AdministratorAccess policy and proceed with the next step of the wizard.
  6. Type in iotbutton-cloudformation as role name and proceed with the next step of the wizard.
  7. Press the Create role button to create the role and finish the wizard.

Creating a Lambda function

Start the Lambda Wizard.

IoT Button: ENV

Andreas and Michael Wittig

Hej, Andreas & Michael here!

We launched the cloudonaut blog in 2015. Since then, we have published 325 articles: small tips and tricks, best practices, and service reviews. We enjoy writing about all things AWS a lot.

Do you like our blog posts and podcast episodes? Have you learned something new? Consider supporting us create in-depth and independent AWS content. Please help us with a monthly or one-time payment through GitHub Sponsors.

Start supporting us today!
  1. Select the trigger type AWS IoT.
  2. Choose IoT Type IoT Button.
  3. Enter the Device Serial Number of your IoT Button.
  4. Press the Generate certificate and keys and follow the shown steps to configure your IoT Button.
  5. Select the Enable trigger option and press the Next button to proceed with the next step of the wizard.

The next step asks you for the Node.js code.

  • Type in iotbutton-cloudformation as the Name of the function and Last one out turns off the light. as description.
  • Choose Node.js 6.10 as the runtime environment.
  • Copy and past the following Node.js code.
const AWS = require('aws-sdk');
const cloudformation = new AWS.CloudFormation({apiVersion: '2010-05-15'});

var stackName = process.env.STACK_NAME;
var templateUrl = process.env.TEMPLATE_URL;

exports.handler = function(event, context, callback) {
if (event.clickType ==='DOUBLE') {
StackName: stackName
}, function(err, res) {
} else if (event.clickType === 'SINGLE') {
StackName: stackName,
Capabilities: ['CAPABILITY_IAM'],
Tags: [
Key: 'Name',
Value: stackName
TemplateURL: templateUrl
}, function(err, res) {
} else {
callback(new Error(`click type ${event.clickType} not supported`));

Add two environment variables.

// STACK_NAME = name of your CloudFormation stack
STACK_NAME = vpc-development-and-testing

// TEMPLATE_URL = S3 URL of your CloudFormation template

IoT Button: ENV

  • Type in index.handler as the handler.
  • Select Choose an existing role.
  • Select the IAM Role iotbutton-cloudformation that you created in the previous section.

Click Next to proceed with the next step.

A summary is shown for review. Click Create function to create the function and finish the wizard.

That’s all. You are ready for a first test.

Turn on the lights!

There is only one thing you need to do now: press your IoT Button! Switch to the CloudFormation service overview to watch your development and test environment being created.

IoT Button: CloudFormation

Turn off the lights!

After your development and test environment has reached the state CREATE_COMPLETE you can delete your infrastructure by pressing your IoT Button twice.


We are turning off the lights to save costs and protect the environment. AWS is offering computing, storage, and networking infrastructure on-demand. Turning off unused cloud resources has the same effect than turning off the lights: it saves you money and reduces environmental impact. By combining an AWS IoT Button, AWS Lambda, and AWS CloudFormation, you can create and delete your development and test environment with the push of a button.

Andreas Wittig

Andreas Wittig

I'm an independent consultant, technical writer, and programming founder. All these activities have to do with AWS. I'm writing this blog and all other projects together with my brother Michael.

In 2009, we joined the same company as software developers. Three years later, we were looking for a way to deploy our software—an online banking platform—in an agile way. We got excited about the possibilities in the cloud and the DevOps movement. It’s no wonder we ended up migrating the whole infrastructure of Tullius Walden Bank to AWS. This was a first in the finance industry, at least in Germany! Since 2015, we have accelerated the cloud journeys of startups, mid-sized companies, and enterprises. We have penned books like Amazon Web Services in Action and Rapid Docker on AWS, we regularly update our blog, and we are contributing to the Open Source community. Besides running a 2-headed consultancy, we are entrepreneurs building Software-as-a-Service products.

We are available for projects.

Feedback? Questions? Drop me a line: Email, Twitter, LinkedIn.

Briefcase icon
Hire me