8
5

More than 3 years have passed since last update.

毎週起動する RDS を毎週停止させる CodeBuild

Last updated at Posted at 2019-10-31

背景

RDSの停止ができるようになりましたが、停止してから1週間経過すると、ふたたび起動してきます。停止したのは、停止しておきたい理由があるから停止したのに、おまえ何で起動さした?

毎週手作業でやるのも微妙。Lambdaでbotoするのを書くのもダルい。

CodeBuild で毎週実行させて awscli でやるのが究極に簡単でした。

buildspec.yml

コンテナイメージは awscli が入るなら何でも良いです。作例では amazonlinux:2 を使っています。

version: 0.2

env:
  variables:
     RDS_NAME: "RDSインスタンス名"

phases:
  install:
    commands:
      - yum install -y awscli
  build:
    commands:
      - aws rds stop-db-instance --db-instance-identifier ${RDS_NAME}

IAM

CodeBuildに付けてるIAMロールに StopDBInstance 権限を付与します。Resourceの範囲はお好みの塩梅で。ほんとうにStopDBInstanceしてよいものだけに絞るのが安全かと思います。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "rds:StopDBInstance",
            "Resource": "arn:aws:rds:*:*:db:RDSインスタンス名"
        }
    ]
}

CodeBuild内でawscliを実行するので iam:PassRole も付けてください。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "iam:PassRole"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}

実行周期

CodeBuildの「ビルドのトリガー」で実行周期の設定ができますね。毎週1回実行などとしておきます。

参考

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5