From 14d3573a559b03dd5def4407ffd8df59c6175e33 Mon Sep 17 00:00:00 2001 From: "shibata.r" Date: Thu, 13 Oct 2022 14:29:56 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:RDS=E3=82=92=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E3=81=95=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 | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 lambda/stop-rds/stop-rds.py diff --git a/lambda/stop-rds/stop-rds.py b/lambda/stop-rds/stop-rds.py new file mode 100644 index 00000000..e69de29b From d18f260ed135dc4f1affe5cbaf55b0c06f2f6617 Mon Sep 17 00:00:00 2001 From: "shibata.r" Date: Thu, 13 Oct 2022 14:31:08 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:RDS=E3=82=92=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E3=81=95=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 From c722349db3fb987e919c8600e7a42b57582b5750 Mon Sep 17 00:00:00 2001 From: "shibata.r" Date: Thu, 13 Oct 2022 16:50:32 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix:=E5=A4=89=E6=95=B0=E5=90=8D=E3=81=AE?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lambda/stop-rds/stop-rds.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lambda/stop-rds/stop-rds.py b/lambda/stop-rds/stop-rds.py index 9230349d..0a3a0783 100644 --- a/lambda/stop-rds/stop-rds.py +++ b/lambda/stop-rds/stop-rds.py @@ -3,8 +3,8 @@ import os #環境変数 client = boto3.client('rds') -TAGKEY = os.environ['TAGKEY'] -TAGVALUE = os.environ['TAGVALUE'] +AUTO_STOP_TAG_TAGKEY = os.environ['TAGKEY'] +AUTO_STOP_ENABLE = os.environ['TAGVALUE'] def lambda_handler(event, context): #クラスターを停止 @@ -13,7 +13,7 @@ def lambda_handler(event, context): 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: + if tag['Key'] == AUTO_STOP_TAG_TAGKEY and tag['Value'] == AUTO_STOP_ENABLE: client.stop_db_cluster(DBClusterIdentifier=cluster['DBClusterIdentifier']) #インスタンスを停止 @@ -22,6 +22,6 @@ def lambda_handler(event, context): 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: + if tag['Key'] == AUTO_STOP_TAG_TAGKEY and tag['Value'] == AUTO_STOP_ENABLE: client.stop_db_instance(DBInstanceIdentifier=instance['DBInstanceIdentifier']) \ No newline at end of file From d536a9c66cc1751ffe015c02c4308da169659217 Mon Sep 17 00:00:00 2001 From: "shibata.r" Date: Tue, 8 Nov 2022 16:24:12 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=E7=92=B0=E5=A2=83=E5=A4=89=E6=95=B0?= =?UTF-8?q?=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lambda/stop-rds/stop-rds.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lambda/stop-rds/stop-rds.py b/lambda/stop-rds/stop-rds.py index 0a3a0783..4f518b54 100644 --- a/lambda/stop-rds/stop-rds.py +++ b/lambda/stop-rds/stop-rds.py @@ -3,8 +3,8 @@ import os #環境変数 client = boto3.client('rds') -AUTO_STOP_TAG_TAGKEY = os.environ['TAGKEY'] -AUTO_STOP_ENABLE = os.environ['TAGVALUE'] +AUTO_STOP_TAG_TAGKEY = os.environ['AUTO_STOP_TAG_TAGKEY'] +AUTO_STOP_ENABLE = os.environ['AUTO_STOP_ENABLE'] def lambda_handler(event, context): #クラスターを停止