RDS自動起動停止

http://dev.classmethod.jp/cloud/aws/rds-auto-stop-start/
https://qiita.com/aoi1/items/fe0caaedb036ab79773a

 

(1)Lambda用のIAMロールの作成

(1.1)ポリシーの作成
ポリシー名:pol_rds

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"rds:StopDBInstance",
"rds:StartDBInstance"
],
"Resource": "arn:aws:rds:*:*:db:*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}

(1.2)ロールの作成
ロール名:role_rds
参照ポリシー名: pol_rds

 

(2)Lambdaファンクションの作成

(2.1)起動用ファンクションの作成
関数名: fun_start_rds
ランタイム: Python3.7
ロール名:role_rds

import boto3

def lambda_handler(event, context):
client = boto3.client('rds')
for id in event["instanceid"]:
response = client.start_db_instance(DBInstanceIdentifier=id)
print(response)


(2.2)停止用ファンクションの作成

関数名: fun_stop_rds
ランタイム: Python3.7
ロール名:role_rds

import boto3

def lambda_handler(event, context):
client = boto3.client('rds')
for id in event["instanceid"]:
response = client.stop_db_instance(DBInstanceIdentifier=id)
print(response)

 

(3)CloudWatch Eventsの登録
※ 注意:cronはUTC指定です

(3.1)起動用ルールの作成
ルール名: rule_start_rds
Cron式: 24 11 * * ? *
関数名: fun_start_rds
入力: 定数: {"instanceid": ["test1","test2"] }

(3.2)停止用ルールの作成
ルール名: rule_stop_rds
Cron式: 32 11 * * ? *
関数名: fun_stop_rds
入力: 定数: {"instanceid": ["test1","test2"] }