Ich wollte es schon lange machen, aber als ich dachte, es wäre großartig, es in Java zu machen, Ich habe nicht geschrieben, dass AWS Lambda in Python ausgeführt werden kann Es ist 3 Monate her, seit ich mich entschlossen habe, die Herausforderung anzunehmen.
Ich habe es versucht, während verschiedene Leute es geschrieben haben.
Erstellen Sie eine Tabelle in DynamoDB, Tag-Informationen für Zeit und Instanz festgelegt. Geben Sie ein, ob gestartet oder gestoppt werden soll.
Die Lambda-Funktion (Python) wird alle 10 Minuten ausgeführt. Wenn es mit der Zeit der in DynamoDB gespeicherten Daten übereinstimmt, Für alle Instanzen, deren Tag-Informationen übereinstimmen Starten oder stoppen.
Erstellen Sie eine Tabelle "EC2" in DynamoDB. Es gibt vier Elemente. "Id" "Time" "Tag" "Start Stop".
Lambda Function(Python)
Es gibt einige Punkte, mit denen ich persönlich zu kämpfen hatte.
Und der Code ist wie folgt.
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)
Ich habe es mit Versuch und Irrtum geschafft.
Von nun an denke ich darüber nach, etwas zu machen, das dies begleitet.
Ich weiß nicht wann es sein wird. .. ..
Recommended Posts