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

  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') {
cloudformation.deleteStack({
StackName: stackName
}, function(err, res) {
callback(err);
});
} else if (event.clickType === 'SINGLE') {
cloudformation.createStack({
StackName: stackName,
Capabilities: ['CAPABILITY_IAM'],
Tags: [
{
Key: 'Name',
Value: stackName
}
],
TemplateURL: templateUrl
}, function(err, res) {
callback(err);
});
} 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.

Summary

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’ve been building on AWS since 2012 together with my brother Michael. We are sharing our insights into all things AWS on cloudonaut and have written the book AWS in Action. Besides that, we’re currently working on bucketAV,HyperEnv for GitHub Actions, and marbot.

Here are the contact options for feedback and questions.