🎉 We are launching a new weekly show: Hot off the Cloud

🎉 We are launching a new weekly show

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

Looking for a new challenge?

  • tecRacer

    Cloud Consultant • AWS Migrations

    tecRacer • Premier AWS Consulting Partner • Germany, Austria, Portugal, and Switzerland
    Assessment Transformation Change Management

    Senior Lead Full Stack Developer

    DEMICON • AWS Advanced Consulting Partner • Remote
    AWS JavaScript/TypeScript Angular React

  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
TEMPLATE_URL = https://s3-eu-west-1.amazonaws.com/widdix-aws-cf-templates-releases-eu-west-1/stable/vpc/vpc-2azs.yaml

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.

Become a cloudonaut supporter

Andreas Wittig

Andreas Wittig ( Email, Twitter, or LinkedIn )

We launched the cloudonaut blog in 2015. Since then, we have published 360 articles, 49 podcast episodes, and 48 videos. It's all free and means a lot of work in our spare time. We enjoy sharing our AWS knowledge with you.

Please support us

Have you learned something new by reading, listening, or watching our content? With your help, we can spend enough time to keep publishing great content in the future. Learn more

Amount must be a multriply of 5. E.g, 5, 10, 15.

Thanks to Alan Leech, Alex DeBrie, ANTHONY RAITI, Christopher Hipwell, Jaap-Jan Frans, Jason Yorty, Jeff Finley, Jens Gehring, jhoadley, Johannes Grumböck, Johannes Konings, John Culkin, Jonas Mellquist, Juraj Martinka, Kamil Oboril, Ken Snyder, Markus Ellers, Ross Mohan, Ross Mohan, sam onaga, Satyendra Sharma, Shawn Tolidano, Simon Devlin, Thorsten Hoeger, Todd Valentine, Victor Grenu, and all anonymous supporters for your help! We also want to thank all supporters who purchased a cloudonaut t-shirt.