Site icon Brent Ozar

Using the Amazon AWS IoT Button to Start and Stop EC2 VMs

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 keep forgetting which one of these launches the missiles.

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.

AWS IoT Button on my light switch

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!

Exit mobile version