AWS RDSの自動起動/停止


AWS RDSの自動起動/停止
  • 参考資料
  • http://ghkdgh2365.blogspot.com/2020/12/aws-lambda-cloudwatch-aws-rds-how-to.html
    https://aws.amazon.com/ko/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/
  • AWS Lambda-作成関数-保存-配置(配置)
  • import boto3
    region = 'ap-northeast-2' # RDS가 존재하는 region 
    db_instance_identifier = 'WRITE YOUR DB INSTANCE IDENTIFIER' # RDS Instance의 식별자
    
    def lambda_handler(event, context):
        ec2 = boto3.client('rds', region_name=region)
        ec2.stop_db_instance(DBInstanceIdentifier=db_instance_identifier)
        print 'stopped your RDS Instance: ' + str(db_instance_identifier)

  • 設定→権限→ロール名のコピー→編集→タイムアウト10秒の設定

  • AWS IAM-ポリシーメニュー-ポリシーの作成-JSONの作成-次:タグ-次:レビュー-名前(カスタマイズ)-ポリシーの作成
  • {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "logs:CreateLogGroup",
            "logs:CreateLogStream",
            "logs:PutLogEvents"
          ],
          "Resource": "arn:aws:logs:*:*:*"
        },
        {
          "Effect": "Allow",
          "Action": [
            "rds:Start*",
            "rds:Stop*"
          ],
          "Resource": "*"
        }
      ]
    }

  • 「ロール」メニュー-「ロール名の貼り付け」(上図)-クリック-接続ポリシー-検索ポリシー名(カスタマイズ)-チェック-接続ポリシー

  • AWS CloudWatch-イベントメニュー-ルール-Amazon EventBridge-作成ルール-名前、説明-アレイ定義、スケジュール-Cron入力-ターゲット-lambda関数-選択(作成)-作成
  • 매일
    오전12시10분  -> 15시10분 stop,  cron(10 15 * * ? *)
    오전8시40분 -> 23시40분 start,  cron(40 23 * * ? *)
  • 1~1725プロセスstart図
  • を作成する.