tl;dr – get a $20 Amazon IoT button, hook it up to this open source iot-button-ec2-controller Lambda action, and turn on/off groups of tagged EC2 VMs.
What Is the Amazon Dash/IoT Button?
I’m a fan of Amazon’s Dash buttons – little $5 buttons that stick around the house next to stuff I can reorder from Amazon.
When we’re running out of laundry detergent, Erika can just push the All detergent button, and more shows up in a day or two. You get to pick which Amazon item gets associated with the button, although not all variations/sizes are available. Before you buy a button, click the “See what you can order with this button” link on the button’s sales page. And no, you can’t configure the All button to order you a Tide item – you need the Tide button for that. There aren’t buttons for every brand, but stoners are covered. The buttons cost $5, and you get the $5 back with your first order of that item, so they pay for themselves.
I like using the Dash buttons for big, bulky items that I don’t like dragging home from the store, like bottled water, toilet paper, paper towels, and detergents. We live in downtown Chicago, which means we can’t just pull the car into the garage and unload a bunch of stuff from Sam’s Club directly into our apartment. Amazon Prime is way more convenient – the boxes can just be brought straight to our front door.
Now, there’s a $20 Amazon button that triggers actions in Amazon’s cloud. It can do all kinds of stuff like post a message to an SNS queue, write a record into DynamoDB, or trigger Lambda code.
What is Amazon Lambda?
AWS Lambda is at the forefront of serverless computing: the idea that you can just upload code to someone’s cloud, and when an event is fired, your code runs.
It’s not really serverless – there are servers in the background running your code for you, but you don’t have to manage them, scale them, tune them, and in fact, you don’t even know what operating system they’re running. You just upload code, and the cloud company takes care of the rest.
With Lambda, you pay for every 100 milliseconds that your code runs.
And the first million requests per month are free.
Screw servers, man. Just write code in Node.js, Python, or Java, and let the cloud manage the rest. Microsoft developers can use Azure Service Fabric to do similar stuff with C#.
Now granted, Lambda is cutting-edge stuff, so it has a whole bunch of drawbacks. This isn’t for synchronous high-performance requirements, like running a constant, heavy-demand web site. The size of your code has to be very small, and the amount of server horsepower it gets is pretty small too – we’re talking under 2GB of RAM. Troubleshooting and coding is much more challenging, although there are frameworks like Serverless that are making it easier.
How Do You Put Them Together?
Heck if I know. I’m a database guy.
So I went on Upwork, a place to find freelancers and post jobs. I’ve used it a few times, like that time I needed to find a podcast producer to launch and manage our Office Hours podcast. I wrote a little job posting describing the script I needed.
I hooked up with Bradley C, a full-stack guy. He wasn’t the cheapest candidate, but I specifically picked him because he was one of the few people who’d done Lambda work in Node.js before. (I wanted to read the Node.js code afterward to understand what it was doing.) He did a great job and went the extra mile throughout the (very quick) project.
So now as I walk into my office, I can click the button once, and that starts up all the VMs that are tagged “IoT Enabled”. (We’ll add separate tags for each company team member so they can pick which VMs they start up with their own Amazon buttons.)
On the way out, I double-click the button, and the VMs shut down.
This is way more convenient than opening up Amazon’s web console or trying to guess a schedule of when I’ll need the VMs.
We open sourced the Node.js Lambda function code so you can do it too. Enjoy!
I love the location next to the light switch. Sort of like a blue canary. Watching over you.
AWS Lambda: the little birdhouse in my soul.
Great practical use case. Are you limited to single and double clicks?
Thanks! You can also do long clicks. If you wanted to chain stuff together (like, say, Morse code) you could do that in the app tier, like waiting for a certain number of seconds after the first click to see if additional ones arrive. There’s a gotcha with that though: the button has a battery lifespan of about 1,000 presses. It’s a AA battery welded into the device. (Sounds odd, but it’s fine for my use case.)
From the Amazon pic, it looks like the button has a little green light. Is that just battery status, or can it reflect the status of a recent command? (or even, the On/Off state of the EC2 machines?)
From the GitHub Readme, it looks like if you left the room in a distracted state and couldn’t remember whether you’d turned them off or not, you could just double-click again.
Aaron – yeah, the light lights up when you press the button, and confirms communication with AWS.
The repeated press handling is up to you. For example, with this script, pressing it when the VMs are already on doesn’t have an effect. In other scripts I’ve seen, people use it as a counter for when some activity happens, so each click adds one value.
You are correct, Aaron! The operations were designed to be idempotent.