Giter Site home page Giter Site logo

iot-button-ec2-controller's Introduction

iot-button-ec2-controller

Allows the start/stop of EC2 instances using an AWS IoT button

The project provides code for a Lambda function (written using Node.js) that allows a properly configured AWS IoT button to start and stop properly tagged EC2 instances.

Grunt is used to deploy packaged code to Lambda (check out the awesome grunt-aws-lambda project).

Initial development of this project was funded by Brent Ozar of Brent Ozar Unlimited.

Setup

  • Prerequisites
    • An AWS Account
    • Working AWS CLI installation
  • Create a new Lambda function in your AWS account.
    • In the web console, go into the Lambda area and use the "iot-button-email" blueprint to create the new function.
    • Change the "IoT type" to "IoT Button"; you'll be asked to provide the serial # for the button.
    • Once you've provided a valid serial #, press the "Generate Certificate and Keys" button.
    • Save the certificate and key file in a safe place, take a screenshot of the directions and click "Next".
    • Provide a name for the Lambda function, and alter the description if desired.
    • Create a new basic execution role (call it something appropriate, like "iot-button-ec2"), copy the contents of policy.json into the policy document area of the new role screen and save; the new role should populate in the role select list back in the Lambda screen.
    • Set execution time to 10 seconds.
    • Click "Save" at the bottom of the screen.
    • On the next screen, check the "Enable Event Source" box, then click the "Create Function" button at the bottom of the screen.
    • Immediately, after the function is created, you'll be taken to a confirmation page. At the top right corner of this page, you'll see "ARN - {longstringofstuff}". Copy the longstringofstuff part.
  • Update Gruntfile.js with the ARN.
    • With the ARN you just copied, replace replace-this-string-with-a-real-value with the value you just copied and save the file.
    • If you're deploying your Lambda function to another region, you'll need to update the region along with the ARN mentioned just above.
  • Update index.js with the proper region.
    • If your Lambda is not deployed in the us-east-1 region, you'll need to update the value accordingly. Save the file.

Deploy

Once all the config from the Setup section is done, you can deploy. You'll need to have Node.js installed, as well as Grunt and the grunt-cli. From within this repo, run npm install. That will download and save locally all required dependencies as outlined in the package.json spec file.

Once that step has run, a simple grunt deploy will package up all the code into a zip file and send it on its way to Lambda.

Set Up Your Button

Using the key, certificate file, and screenshot that you got from Lambda earlier (while we were setting up our function), set up your button and get it talking to the 'net.

Tag Your Instances

Simply creating an IoT Enabled tag is enough to make the EC2 instance work with this. If you'd rather use another tag key, you'll need to alter index.js accordingly.

Try It Out!

A single button press will turn on tagged instances if they're not already running. A double button press will turn off tagged instances if they're not already shutdown.

Enjoy!

TODO

There are still a few things I'd like to do, such as moving config to an external file and writing a few tests (though tests were run from within the Lambda interface), tightening up the permissions in the policy.json file, and figuring out what an appropriate long button push event might be and writing code to support that.

iot-button-ec2-controller's People

Contributors

geekmuse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

iot-button-ec2-controller's Issues

Unable to start/stop the VM

Hi,

i have the same issue than @tupacalypse187. When I try with a Test lambda I have:

{
"msg": "No instances in a state to be affected by this operation.",
"instanceIds": []
}

How I can fix that?
On my VM I have the tag KEY=IoT Enabled VALUE=IoT Enabled
I config on the index.js the region : AWS.config.region = 'eu-west-3';

When I click on the button, nothing happen.

Many thanks

AWS API request failed with undefined - TimeoutError: Missing credentials in config

@geekmuse When running "grunt deploy" I get an API credential failure. Do you know how to resolve? Where is the credential defined?

[root@antlet12 iot-button-ec2-controller]# grunt deploy
Running "lambda_package:default" (lambda_package) task
[email protected] ../../tmp/1521416107771.2512/node_modules/iot-button-ec2-controller
└── [email protected]
Created package at ./dist/iot-button-ec2-controller_1-0-0_2018-2-18-16-35-7.zip

Running "lambda_deploy:default" (lambda_deploy) task

AWS API request failed with undefined - TimeoutError: Missing credentials in config
Warning: Check your AWS credentials, region and permissions are correct. Use --force to continue.

Aborted due to warnings.

Adding tag Name --> IoT Enabled does not stop/start EC2 Instance

Hi - Can you build out and see if all works? Maybe something has changed on AWS end. I have an EC2 instance with a tag 'Name' -> 'IoT Enabled' and pressing the button does not start or stop EC2. I also do not see any action in the logs besides SINGLE/DOUBLE. Am I labeling the tag correctly? Key = Name, Value = IoT Enabled?
All code is deployed and lambda is operationally ready. Thank you.

grunt-aws-lambda requires peer of grunt@~0.4.5

Thanks for putting this code together. I'm playing around with the AWS Button and have this issue when working with your code. Do you recall how to resolve? Thank you.
$ npm install -->
npm WARN [email protected] requires a peer of grunt@~0.4.5 but none was installed.
npm ERR! code 1

Unable to find lambda function

No matter what I try and I've double checked as well as copied and pasted function name from cli command 'aws lambda list-functions'. Any thoughts on why the function can't be found?
[root@antlet12 iot-button-ec2-controller]# grunt deploy
Running "lambda_package:default" (lambda_package) task
[email protected] ../../tmp/1521421419985.6768/node_modules/iot-button-ec2-controller
└── [email protected]
Created package at ./dist/iot-button-ec2-controller_1-0-0_2018-2-18-18-3-39.zip

Running "lambda_deploy:default" (lambda_deploy) task
Warning: Unable to find lambda function arn:aws:lambda:us-west-2:570656418299:function:myButtonFunction03, verify the lambda function name and AWS region are correct. Use --force to continue.

Aborted due to warnings.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.