UPD: updated lambda-ec2StartStop.py with logging

This commit is contained in:
xpk 2024-03-12 11:49:02 +08:00
parent 88cadbd0b2
commit c010e2889d
Signed by: xpk
GPG Key ID: CD4FF6793F09AB86
1 changed files with 11 additions and 2 deletions

View File

@ -3,6 +3,13 @@ import boto3
import os
import json
import time
import logging
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.getLogger(__name__)
def start_instances(instances: list[str]) -> dict:
@ -36,11 +43,13 @@ def instance_status(instances: list[str]) -> str:
def lambda_handler(event, context):
instances: list[str] = json.loads(os.environ['instances'])
if event['action'] == 'start':
logger.info('Starting instances: ' + str(instances))
resp = start_instances(instances)
print(instance_status(instances))
logger.info(instance_status(instances))
elif event['action'] == 'stop':
logger.info('Stopping instances: ' + str(instances))
resp = stop_instances(instances)
print(instance_status(instances))
logger.info(instance_status(instances))
else:
resp = "Event action not provided"
return resp