From d18f260ed135dc4f1affe5cbaf55b0c06f2f6617 Mon Sep 17 00:00:00 2001 From: "shibata.r" Date: Thu, 13 Oct 2022 14:31:08 +0900 Subject: [PATCH] =?UTF-8?q?feat:RDS=E3=82=92=E5=81=9C=E6=AD=A2=E3=81=95?= =?UTF-8?q?=E3=81=9B=E3=82=8BLambda?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lambda/stop-rds/stop-rds.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lambda/stop-rds/stop-rds.py b/lambda/stop-rds/stop-rds.py index e69de29b..9230349d 100644 --- a/lambda/stop-rds/stop-rds.py +++ b/lambda/stop-rds/stop-rds.py @@ -0,0 +1,27 @@ +import boto3 +import os + +#環境変数 +client = boto3.client('rds') +TAGKEY = os.environ['TAGKEY'] +TAGVALUE = os.environ['TAGVALUE'] + +def lambda_handler(event, context): + #クラスターを停止 + clusters = client.describe_db_clusters() + for cluster in clusters['DBClusters']: + if cluster['Status'] == "available": + tags = client.list_tags_for_resource(ResourceName=cluster['DBClusterArn']) + for tag in tags['TagList']: + if tag['Key'] == TAGKEY and tag['Value'] == TAGVALUE: + client.stop_db_cluster(DBClusterIdentifier=cluster['DBClusterIdentifier']) + + #インスタンスを停止 + instances = client.describe_db_instances() + for instance in instances['DBInstances']: + if instance['DBInstanceStatus'] == "available": + tags = client.list_tags_for_resource(ResourceName=instance['DBInstanceArn']) + for tag in tags['TagList']: + if tag['Key'] == TAGKEY and tag['Value'] == TAGVALUE: + client.stop_db_instance(DBInstanceIdentifier=instance['DBInstanceIdentifier']) + \ No newline at end of file