Tech
2025-03-20
Prevent pricing bombs by automatically controlling AWS EC2 instances

Table of contents

In the process of using a corporate public development account, I had an experience where I almost incited unnecessary costs because I forgot to launch an EC2 instance and then shut it down. To prevent this, I developed my own program that can automatically control and manage EC2 instances.

However, in order to prevent accidentally touching another developer's instance, I only enter and use a list of instances managed by me in HTML. However, in the case of personal accounts, it's OK to display and use all instances returned by Lambda as is.

1. Program introduction and screen examples

EC2 인스턴스 제어

The AWS EC2 Instance Automatic Control and Scheduled Termination Program makes it easy to start or stop instances through a web interface, and provides the ability to set tags to automatically terminate at a specific time (for example, after 1 hour). This provides the following benefits:

  • Cost savings: Reduce unnecessary costs by automatically terminating unused instances
  • expediency: You can easily check the instance status and easily control it through the web UI
  • automation: If you specify an end time using the scheduled end tag, the instance is automatically stopped without any manual operation

2. Prerequisites

2.1. Set up an AWS account and IAM permissions

  • You need IAM user credentials (Access Key & Secret Key) with start/stop permissions for EC2 instances.
  • To automatically update DNS records using Route53, that permission must also be included in the IAM policy.

2.2. Node.js & npm environment configuration

  • A Node.js environment is required to install the AWS SDK and required libraries.
  • The latest LTS version is recommended as the version.

2.3. Check the GitHub repository

  • See the file structure of the GitHub repository disclosed at the bottom of this article.
  • Clone the repository or download it as a ZIP file to proceed with the project.

3. Download and install code

3.1. Cloning a project on GitHub

(The above URL is an example; use an actual public repository URL.)

3.2. Installing dependencies

  • npm installThrough the required libraries (for example: @aws -sdk/client-ec2, Axios etc.) will be installed.

3.3. Create a lambda function and upload a file

This part How to easily automatically create an AWS Lambda (lambda) function Please refer to the post.

  • Create an AWS Lambda function
  • Zip the entire ScheduleDec2Stopper folder
  • Upload a file to the function code
  • Set environment variables if needed (for example: TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID etc.)
    • When linked to Telegram, you'll be notified with a message the moment the instance starts and when it's been used for an hour or more.

4. Connecting to EventBridge

scheduleDec2Stopper The function runs periodically, such as every 10 minutes, to monitor EC2 instances and automatically find instances that need to be stopped. To do this, you can schedule Lambda functions using EventBridge rules.

1. Click the Add Trigger button

key

2. Select EventBrigge

key
  • From source selection EventBriggeSelect.

3. Click the add button

key
  • Enter the rule name and expression extra Click the button.
  • What is the repetition cycle cron or rate It can be set using an expression.

5. Conclusions and additional tips

By using this program, EC2 instances can be set to run only when actually needed and automatically shut down after a certain period of time, effectively reducing unnecessary monthly expenses.

In terms of security, it is important to set IAM permissions according to the Principle of Least Privilege (Principle of Least Privilege). Additionally, when managing multiple instances, you must ensure scalability by considering AWS Lambda limitations such as timeouts, call cycles, and number of concurrent executions.

Currently, the program can be accessed by anyone who knows only a specific lambda endpoint (URL), so we recommend adding a personal authentication function to enhance security.

Experience Monitoring with WhaTap!