From c46b4ad552b55d503e883ee2e659dbee9575e6b8 Mon Sep 17 00:00:00 2001 From: "shimoda.m@nds-tyo.co.jp" Date: Fri, 19 May 2023 14:47:14 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=8B=95=E9=96=8B=E5=A7=8B?= =?UTF-8?q?=E3=82=B9=E3=82=AF=E3=83=AA=E3=83=97=E3=83=88=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lambda/start-rds/start-rds.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lambda/start-rds/start-rds.py diff --git a/lambda/start-rds/start-rds.py b/lambda/start-rds/start-rds.py new file mode 100644 index 00000000..a077cb88 --- /dev/null +++ b/lambda/start-rds/start-rds.py @@ -0,0 +1,29 @@ +import os + +import boto3 + +#環境変数 +client = boto3.client('rds') + +AUTO_START_TAG_TAGKEY = 'AUTO_START' +AUTO_START_ENABLE = 'true' + +def lambda_handler(event, context): + #クラスターを停止 + clusters = client.describe_db_clusters() + for cluster in clusters['DBClusters']: + if cluster['Status'] == "stopped": + tags = client.list_tags_for_resource(ResourceName=cluster['DBClusterArn']) + for tag in tags['TagList']: + if tag['Key'] == AUTO_START_TAG_TAGKEY and tag['Value'] == AUTO_START_ENABLE: + client.start_db_cluster(DBClusterIdentifier=cluster['DBClusterIdentifier']) + + #インスタンスを停止 + instances = client.describe_db_instances() + for instance in instances['DBInstances']: + if instance['DBInstanceStatus'] == "stopped": + tags = client.list_tags_for_resource(ResourceName=instance['DBInstanceArn']) + for tag in tags['TagList']: + if tag['Key'] == AUTO_START_TAG_TAGKEY and tag['Value'] == AUTO_START_ENABLE: + client.start_db_instance(DBInstanceIdentifier=instance['DBInstanceIdentifier']) +