DEV Community

Cover image for Kaizen or how to start/stop ec2.
pedchenkoroman
pedchenkoroman

Posted on

Kaizen or how to start/stop ec2.

Hi guys.

Today, I’d like to share with you a technique I’ve typically used in my work. This technique is called Kaizen.

So, what is Kaizen, and why should you care? Great question. Kaizen is a continuous improvement method that is used in manufacturing. For some, you might be familiar with similar methods like Lean in Project Management or the Toyota Production System (TPS). Kaizen was first introduced in the 1960’s when engineer Taiichi Ohno, who created TPS. It was designed to achieve objectives like improving quality, increasing profitability, and reducing costs.

Normally, each of my PRs consists of a dedicated task and a small improvement. This could be anything, such as adding logging, encapsulating code and covering it with unit tests or reduce unnecessary http requests. Some might refer to this as following the "Boy Scout Rule" and it will be absolutely right. In my humble opinion it is just adjacent topic.

So, I’d like to share a story about how I used this technique to reduce our AWS monthly bill.

Requirements

The application I’ve been working on includes some third-party integrations, and to test the entire workflow, I came up with the idea of spinning up an EC2 instance with primitive logic to simulate those integrations. Everything worked perfectly until I noticed that, most of the time, the instance was idle. So, I did a quick calculation and realized that I could save $1.46 per environment per month by optimizing this setup. Then I considered an effort to implement it and got a green light from a PM to implement it.

Calculations

Before

1 instances x 0.006 USD On Demand hourly cost x 730 hours in a month = 4.380000 USD

After

1 instances x 0.006 USD On Demand hourly cost x 486.66666666666663 hours in a month = 2.920000 USD

Solution

The solution is straightforward and consists of three steps:

  • Create a New AWS Role: Set up a new AWS role with permissions to start and stop EC2 instances.

Create a new role

  • Set Up CloudWatch Rules: Create two CloudWatch rules—one to start the EC2 instance and another to stop it at predefined times.

Set up rules

  • Implement a Lambda Function: Use AWS SDK within a Lambda function to send start and stop commands to the EC2 instance.

Lambda logic

That’s pretty much it! The next step is to get rid of the EC2 instance entirely and use only AWS Lambda, with input and output stored in S3. Considering AWS Lambda’s generous free tier, I anticipate the cost will be $0.

It’s the little details that are vital. Little things make big things happen. ~ John Wooden

NOTE: The source code you can find here.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay