3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

本番環境のS3をステージング環境にもコピーする

Posted at

やりたいこと

ステージング環境と本番環境でS3データの差異があり、表示ができないなどを解消したい。
主に画像です。

処理フロー

ステージング環境のEC2上でS3コピーのAWS CLIを実行します。
実行できればcronで毎日コピーするようにします。

手順1 本番環境のバケットポリシーを変更

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowBAccountReadAccess",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::<ステージング環境アカウントID>:root"
      },
      "Action": ["s3:GetObject", "s3:ListBucket"],
      "Resource": [
        "arn:aws:s3:::本番環境バケット名",
        "arn:aws:s3:::本番環境バケット名/*"
      ]
    }
  ]
}

手順2 ステージング環境でロールを作成して、EC2にロール付与


{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:ListBucket"
      ],
      "Resource": [
        "arn:aws:s3:::本番環境バケット名",
        "arn:aws:s3:::本番環境バケット名/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:PutObject",
        "s3:PutObjectAcl"
      ],
      "Resource": [
        "arn:aws:s3:::ステージング環境バケット名/*"
      ]
    }
  ]
}

手順3 EC2でコマンド実行

AWS CLIインストール手順は以下を参考

aws s3 sync s3://本番環境バケット s3://ステージング環境バケット

問題なく実行できればcronの設定を行います。

手順4 cronを設定する

crontab -e
0 10 * * * aws s3 sync s3://本番環境バケット s3://ステージング環境バケット

※上記例は朝10時に本番環境のS3内容をステージング環境にコピーします

まとめ

定期的にコピーしておけばステージング環境の差異を埋めるだけではなく、本番環境のS3バックアップにもなりますので便利かなと思っています。
ただ、初回にコピー内容が多いと少し時間はかかります。

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?