The Cloud Switch: IoT Button, Lambda, and CloudFormation

DZone 's Guide to

The Cloud Switch: IoT Button, Lambda, and CloudFormation

Turn your cloud infrastructure on and off with the push of a button. Here's how to connect an AWS IoT Button to CloudFormation via AWS Lambda.

· IoT Zone ·
Free Resource

Last one out turns off the light. What works for boring light bulbs can be adapted 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?

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 Wi-Fi that 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 hands over the package with your own IoT Button, you are ready to go.

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 the role type.
  5. Select the AdministratorAccess policy and proceed with the next step of the wizard.
  6. Type in iotbutton-cloudformation as the 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.

  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 paste 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

  • 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 your 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.

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.

iot ,cloud infrastructure ,cloudformation ,aws lambda ,aws iot ,tutorial

Published at DZone with permission of Andreas Wittig . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}