Try running a Schedule to start and stop an instance on AWS Lambda (Python)

Introduction

I've wanted to make it for a long time, but when I thought it would be great to make it in Java, I haven't written about AWS Lambda being able to run in Python, so It's been 3 months since I decided to take on the challenge.

I tried it while various people wrote it.

Overview

Create a table in DynamoDB, tag information set for time and instance, Enter whether to start or stop.

Lambda Function (Python) runs every 10 minutes, If it matches the time of the data stored in DynamoDB, For all instances whose tag information matches Start or stop.

Preparation

Create a table "EC2" in DynamoDB. There are four items. "Id" "Time" "Tag" "Start Stop".

  1. Id: Sequence number, please make sure to order
  2. Time: Time, "% H:% M" format, eg "09:00"
  3. Tag: Tag of the instance Enter the tag you want to start or stop
  4. StartStop: Enter whether to start or stop.

Lambda settings

  1. Runtime is "Python 2.7"
  2. Code later ...
  3. Handler is suitable
  4. Role is OK if there is read / write to DynamoDB and start / stop and reference to EC2
  5. I think the memory can be 128MB
  6. Timeout is appropriate (10sec this time)

Lambda Function(Python)

There are some points that I personally struggled with.

  1. "Filters = [{'Name':'tag-key','Values': [tableInfo ['Tag']]}]" I didn't know that there was a "[]" before and after the "tableInfo ['Tag']"
  2. There must be "['Items']" in "table.scan () ['Items']" Without this, you cannot get the Item of the data acquired from DynamoDB
  3. I don't know why, but it must be "boto3.client ('ec2')" It didn't work with "boto3.resource ('ec2')"
  4. It seems that "from boto3.dynamodb.conditions import Key, Attr" is required

And the code is as follows.

import boto3
import datetime
from boto3.dynamodb.conditions import Key, Attr

def lambda_handler(event, context):
    # create EC2 client
    ec2 = boto3.client('ec2')
    # get the time(%H:%M)
    timeInfo = datetime.datetime.now().strftime("%H:%M")
    tableInfoList = getTableInfoList(timeInfo)
    instanceList = getInstanceStartStopList(ec2, tableInfoList)
    executeInstanceStartStop(ec2, instanceList)

# get the list of data (Id, StartEnd, Tag, Time)
#     Id       : Sequence
#     StartEnd : "start" or "stop"
#     Tag      : Tag-key
#     Time     : %H:%M  ex) 09:00
# @param timeInfo
# @return list of data(Id, StartEnd, Tag, Time)
def getTableInfoList(timeInfo):
    dynamodb = boto3.resource('dynamodb')
    #Set TableName
    table    = dynamodb.Table('EC2')
    #Send the Query
    #Query Parameter
    #   @param TimeInfo : [hh:mm]
    #TimeInfo = "09:00"
    tableInfoList = table.scan(
            FilterExpression=Attr('Time').eq(timeInfo)
        )['Items']
    return tableInfoList

# get the list of data (Start or Stop Instance Ids)
# @param ec2
# @param tableInfoList
# @return instanceList Dictionaly('start', 'stop')
def getInstanceStartStopList(ec2, tableInfoList):
    startInstanceIdList = []
    stopInstanceIdList  = []
    for tableInfo in tableInfoList:
        for reservation in ec2.describe_instances(Filters=[{'Name':'tag-key','Values':[tableInfo['Tag']]}])["Reservations"]:
            for instance in reservation["Instances"]:
                if (tableInfo['StartStop'] == 'start'):
                    startInstanceIdList.append(instance["InstanceId"])
                elif (tableInfo['StartStop'] == 'stop'):
                    stopInstanceIdList.append(instance["InstanceId"])
    instanceList = {"start":startInstanceIdList, "stop":stopInstanceIdList}
    return instanceList

# Start of Stop Instances
# @param ec2
# @param instanceList
def executeInstanceStartStop(ec2, instanceList):
    startInstanceIdList = instanceList["start"]
    stopInstanceIdList  = instanceList["stop"]

    if not len(startInstanceIdList) == 0:
        ec2.start_instances(InstanceIds=startInstanceIdList)

    if not len(stopInstanceIdList) == 0:
        ec2.stop_instances(stopInstanceIdList)

I managed to do it with trial and error.

from now on

From now on, I'm thinking of making something that accompanies this.

  1. Script that can register / update / delete / view DynamoDB data
  2. Register / update / delete / view DynamoDB data with static HTML + JS with S3
  3. Real-time execution with API Gateway + Lambda

I don't know when it will be. .. ..

Recommended Posts

Try running a Schedule to start and stop an instance on AWS Lambda (Python)
Posted as an attachment to Slack on AWS Lambda (Python)
Run Python on Schedule on AWS Lambda
Things to note when running Python on EC2 from AWS Lambda
Building an environment to run ChainerMN on a GPU instance on AWS
[Python] Create a linebot to write a name and age on an image
Periodically run a python program on AWS Lambda
Try Tensorflow with a GPU instance on AWS
Use AWS lambda to scrape the news and notify LINE of updates on a regular basis [python]
A story about trying to install uwsgi on an EC2 instance and failing
Summary of points I was addicted to running Selenium on AWS Lambda (python)
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
Try running a Django application on an nginx unit
I want to AWS Lambda with Python on Mac!
A little trick to know when writing a Twilio application using Python on AWS Lambda
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Try to bring up a subwindow with PyQt5 and Python
Try automating Start / Stop for EC2 instances with AWS Lambda
Building an environment to execute python programs on AWS EC2
[Python] Allow pip3 packages to be imported on AWS Lambda
Just try to receive a webhook in ngrok and python
Try to generate a cyclic peptide from an amino acid sequence with Python and RDKit
How to make AWS Lambda Layers when running selenium × chrome on AWS Lambda
Python 3.6 on Windows ... and to Xamarin.
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
A game to go on an adventure in python interactive mode
xgboost (python) on EC2 Spot instance environment prepared by AWS Lambda
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
Try to extract a character string from an image with Python3
Disguise the grass on GitHub and try to become an engineer.
Prepare an environment to use OpenCV and Pillow with AWS Lambda
The road to installing Python and Flask on an offline PC
[Python] I wrote a REST API using AWS API Gateway and Lambda.
[For Python] Quickly create an upload file to AWS Lambda Layer
I thought a Python class variable was an instance variable and died
Everything from building a Python environment to running it on Windows
[AWS lambda] Deploy including various libraries with lambda (generate a zip with a password and upload it to s3) @ Python
I want to pass an argument to a python function and execute it from PHP on a web server
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
Try to make foldl and foldr with Python: lambda. Also time measurement
Try to build python and anaconda environment on Mac (by pyenv, conda)
Try to write python code to generate go code --Try porting JSON-to-Go and so on
How to stop a program in python until a specific date and time
Migrate Django applications running on Python 2.7 to Python 3.5
Run TensorFlow on a GPU instance on AWS
Installing Anaconda Python on an ECS instance
[Python] Run Headless Chrome on AWS Lambda
Connect to s3 with AWS Lambda Python
[Python] I made a script that automatically cuts and pastes files on a local PC to an external SSD.
Try implementing a Cisco Spark bot with AWS Lambda + Amazon API Gateway (Python)
[Python] Concatenate a List containing numbers and write it to an output file.
Feel free to turn Python using the library into an AWS Lambda function
A complete guidebook to using pyenv, pip and python in an offline environment
WEB scraping with python and try to make a word cloud from reviews
A story about cross-compiling a python package for AWS Lambda and deploying it serverless
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
I tried to use Twitter Scraper on AWS Lambda and it didn't work.
A memo of a tutorial on running python on heroku
Consider a cloud-native WebSocket application running on AWS
Try to calculate a statistical problem in Python