I tried to reduce costs by starting / stopping EC2 collectively on AWS Lambda

Since there are a large number of EC2 instances that I usually use for development and testing, I use cron to schedule startup and stop of instances, and use them at the lowest possible cost. I haven't touched AWS Lambda at all, so I thought it was a good opportunity to migrate from cron batch, so I made some changes for AWS Lambda, so I will expose it.

What you can do

** "I want to start the instance used for development etc. only during business hours" **, ** "I want to start it regularly only on a specific day of the week" **. To set the instance, just put the tag written in the configuration file in the tag of the instance, and delete the tag to release it.

Preparation

Since the libraries boto and pytz are required, install them in the same layer as ec2_scheduler.py.

$ pip install boto -t path/to/ec2_scheduler/
$ pip install pytz -t path/to/ec2_scheduler/

Only instances with the same tags as tag-key and tag-value in the configuration file are scheduled to be executed. If you want to stop the scheduled execution, just delete the tag.

The setting items are as follows (file name can be changed)

schedule.cfg


[Develop]
region: ap-northeast-1 <=region
type: daily <=Execution Type(Since there is only daily, it is as it is)
tag-key: AWS-Schedule <=Instance tag name
tag-value: Dev-Weekday  <=Tag value
start: 9:00  <=Start-up
stop: 19:00  <=Stop
skip: saturday, sunday <=Specify the day to skip by day of the week
timezone: Asia/Tokyo  <=Time zone specification

↓ You can set multiple schedules in the configuration file.
[TestServer]
region: ap-northeast-1
...
...

Prepare a Lambda Function to execute

lambda_function.py


# -*- coding: utf-8 -*-

from ec2_scheduler import EC2Scheduler

def lambda_handler(event, context):
    access_id = 'AWS access ID'
    secret_key = 'AWS secret key'
    conf = 'schedule.cfg'

    schedule = EC2Scheduler(access_id=access_id, secret_key=secret_key, conf=conf)
    schedule.job()

When you have prepared the Lambda Function, zip the prepared file. (Run in the same directory)

cd path/to/ec2_scheduler
zip -r ec2_scheduler.zip .

AWS Lambda settings

Create a new Lambda Function in AWS Lambda in the AWS Management Console [Lambda] ** Select [Create a Lambda function]-> [Python2.7]-> [lambda canary] ** [Configure event sources] ** Select [Event source type]-> [CloudWatch Events --Schedule] ** ** Enter a name in [Rule name] ** as appropriate ** Select the execution schedule with [Schedule expression] ** [Configure function] A suitable name for ** [Name] ** [Runtime] -> [Python 2.7] ** Upload the zip file prepared in advance with [Lambda function code]-> [Upload a .ZIP file] ** Put the handler name in ** [Handler \ *] ** ("lambda_function.lambda_handler" in the example) [Role] -> [Basic execution role] ** [Advanced settings] ** Make appropriate settings

The rest should be created with ** [Create function] **

After creating a Lambda Function, check it with Test. The Test should be succeeded, so all you have to do is set the ScheduleEvent you set earlier in ** [Event source] ** to ** State Enabled **.

Recommended Posts

I tried to reduce costs by starting / stopping EC2 collectively on AWS Lambda
I tried to use Twitter Scraper on AWS Lambda and it didn't work.
I tried to get an AMI using AWS Lambda
I want to AWS Lambda with Python on Mac!
I tried to launch ipython cluster to the minimum on AWS
xgboost (python) on EC2 Spot instance environment prepared by AWS Lambda
Things to note when running Python on EC2 from AWS Lambda
I tried to delete bad tweets regularly with AWS Lambda + Twitter API
I tried to notify Zabbix Server of execution error of AWS Lambda function
I made a bot to post on twitter by web scraping a dynamic site with AWS Lambda (continued)
Summary of points I was addicted to running Selenium on AWS Lambda (python)
I tried to rescue the data of the laptop by booting it on Ubuntu
What I found by deploying Django on EC2
I tried to get an image by scraping
I tried connecting AWS Lambda with other services
I tried to classify dragon ball by adaline
I wanted to operate google spread sheet with AWS lambda, so I tried it [Part 2]
I tried to implement Minesweeper on terminal with python
How to set layer on Lambda using AWS SAM
[AWS EC2] How to install Maven on Amazon Linux 2
AWS Lambda now supports Python so I tried it
I tried to notify the honeypot report on LINE
I tried to install scrapy on Anaconda and couldn't
[Introduction to AWS] I tried playing with voice-text conversion ♪
I tried to summarize the languages that beginners should learn from now on by purpose
I tried to debug.
I tried to paste
I tried AWS Iot
Posted as an attachment to Slack on AWS Lambda (Python)
[AWS] I tried using EC2, RDS, Django. Environment construction from 1
I tried to implement anomaly detection by sparse structure learning
I tried using "Syncthing" to synchronize files on multiple PCs
[Introduction to simulation] I tried playing by simulating corona infection ♬
[Django] I tried to implement access control by class inheritance.
[Introduction to Pandas] I tried to increase exchange data by data interpolation ♬
I tried to classify MNIST by GNN (with PyTorch geometric)
[AWS EC2] Settings you want to do on Amazon Linux 2
Building an environment to execute python programs on AWS EC2
I tried to classify music major / minor on Neural Network
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
[Python] Allow pip3 packages to be imported on AWS Lambda
Sample script to monitor Load Average on CloudWatch on AWS EC2