--When using EC2 for the development environment, it is easy to forget to Shutdown the Instance when returning home. ――It seems that there is a saying, "If dust accumulates, it becomes a mountain." ――Maybe everywhere is doing the same thing, EC2 Instance automatic stop bot-kun is made with Lambda. (In a company with a lot of overtime, when everyone forgets their existence, they mercilessly release the tragic Shutdown and tend to be relatively disliked) --A Chance to kill Instances that just run cron every day with ScheduleEvent attached to Lambda.
――I hope you can get a feel for the Role and GUI settings around here. [Class method] I tried to automatically start & stop EC2 with Lambda's Schedule event [Prevent forgetting to stop or delete EC2, RDS, and ELB! LambdaSchedule Event] (http://dev.classmethod.jp/cloud/aws/lambda-scheduled-event-info/ "Prevent forgetting to stop or delete EC2, RDS, ELB! LambdaSchedule event")
--Until now, there was EC2 dedicated to cron execution that automatically started at the start time and automatically stopped at a little later at night. ――When starting automatically, identify Saturdays, Sundays, and holidays, and start automatically only on weekdays. --Since boto3 can be used by default with Lambda's python, narrow down the target to Instance of a specific tag with Filter.
--For weekday or Saturday and Sunday judgment, if you specify MON-FRI in Lambda's ScheduleEvent (cron), it can be executed only on weekdays, but due to the UTC problem described later, if necessary, python standard date.weekday () etc. use ――There are various ways to judge holidays, but in the end, I rely on Google Calendar's Japanese holidays. So, I have a python module made by Google, so I will use it. [google-api-python-client] (https://github.com/google/google-api-python-client "google-api-python-client")
--There are two ways to write code in Lambda, one is to write while editing inline on the browser, and the other is to zip each module and upload the code if you want to use an external module. --Google-api-python-client can't be used, of course, so install it locally in any directory with pip.
cd [Appropriate directory]
sudo pip install --upgrade google-api-python-client -t ./
#Write the main file to run on Lambda in the same directory
vim hoge.py
hoge.py
import boto3
import datetime
import sys
from apiclient.discovery import build
# https://console.developers.google.com/project from here
API_KEY = '[Google development API key]'
CALENDAR_ID = 'ja.japanese#[email protected]'
#The year-end and New Year holidays and company holidays do not start automatically(YYYY-MM-Enumerate with DD)。
company_holiday_list = []
#Function name(Here lambda_handler)And the file name(Here hoge.py)To
#Set to Lambda Handler name ex.) hoge.lambda_handler
def lambda_handler(event, context):
client = boto3.client('ec2')
#Automatically starts when the tag name "Auto Shutdown" is AUTO/Stop
#If the tag name "Auto Shutdown" is ON, only automatic stop is performed.
query_start = [
{'Name': 'tag:AutoShutdown', "Values": ['AUTO']},
{'Name': 'instance-state-name', "Values": ['stopped']}
]
query_stop = [
{'Name': 'tag:AutoShutdown', "Values": ['ON', 'AUTO']},
{'Name': 'instance-state-name', "Values": ['running']}
]
service = build(serviceName='calendar', version='v3', developerKey=API_KEY)
events = service.events().list(calendarId=CALENDAR_ID).execute()
holiday_list = []
for item in events['items']:
holiday_list.append(item['start']['date'])
holiday_list.extend(company_holiday_list)
#Confirmation of ignition source event name
try:
#In case of automatic start, operate only on weekdays except Saturdays, Sundays, and holidays
if '[Event source ARN(ScheduleEvent for automatic startup)Copy]' in event['resources']:
if not str(datetime.date.today()) in holiday_list:
client.start_instances(InstanceIds=get_instanceid(query_start))
elif '[Event source ARN(ScheduleEvent for automatic stop)Copy]' in event['resources']:
client.stop_instances(InstanceIds=get_instanceid(query_stop))
elif '[Event source ARN(ScheduleEvent for automatic stop X minutes advance notification)Copy]' in event['resources'] \
and (not str(datetime.date.today()) in holiday_list):
#It's kinder to write a process here that notifies you of Slack 5 or 10 minutes in advance.(Automatic stop explosion prevention)
except Exception as e:
#Error handling. Like throwing a stack trace into Slack.
print("SUCCESS: task succeeded")
return
def get_instanceid(query):
client = boto3.client('ec2')
response = client.describe_instances(Filters=query)
ec2_count = len(response['Reservations'])
ec2_list = []
if not ec2_count == 0:
for i in range(0, ec2_count):
ec2_list.append(response['Reservations'][i]['Instances'][0]['InstanceId'])
return ec2_list
else:
print("SUCCESS: specified hosts is None")
sys.exit()
--Upload code to Lambda
zip -r ~/hoge.zip .
aws lambda update-function-code --function-name [Function name set when creating Lambda] --zip-file fileb://~/hoge.zip
-Please rewrite [Event source ARN] to your own environment m (_ _) m The ARN part in the image below is a copy and paste. --I don't want to divide Lambda Functions into multiple functions by automatic start / stop, so Branch whether it is start or stop depending on the firing source event (ScheduleEvent name). --Lambda's ScheduleEvent is UTC, so be careful when setting and handling it! For example, I want JST to start automatically at 8 am! In that case, UTC will change the date from JST, so it will not start automatically.
Recommended Posts