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.
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:
(The above URL is an example; use an actual public repository URL.)
npm install
Through the required libraries (for example: @aws -sdk/client-ec2
, Axios
etc.) will be installed.This part How to easily automatically create an AWS Lambda (lambda) function Please refer to the post.
TELEGRAM_BOT_TOKEN
, TELEGRAM_CHAT_ID
etc.)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
2. Select EventBrigge
3. Click the add button
cron
or rate
It can be set using an expression.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.