LoginSignup
1
0

More than 1 year has passed since last update.

S3のレプリケーション(同一AWSアカウント)

Last updated at Posted at 2021-09-07

はじめに

DevOpsの一環でCICDを構築した際、レプリケーション先でレプリケート後にDeployを実行させる仕組みを構築したが、自分ではレプリケーションを設定した事がなかったので試しにやってみた

また、今回レプリケーションの設定をやる中でIAMロール?サービスロール?といった言葉出てきて、それぞれのロールの違いって何?となったのでそれについても調べたりして自分なりに理解を深めてみたを参照)
※一応自分の理解を確かめるという意味でも、ここではあえてIAMロールとサービスロールの2パターンでレプリケーションをやってみた

・参考:チュートリアル: レプリケーションの設定
・参考:同じアカウントが所有するレプリケート元バケットとレプリケート先バケットのレプリケーションの設定

IAMロール版

IAMロールの作成

レプリケーションは、S3というAWSサービスがユーザに代わってやる事なのでS3にロールを渡す必要があり、IAMロール版ではIAMロールをS3に渡す事になるのでまずはそのIAMロールを作成する

ここではManagement Consoleから作成した(サービスロール版ではAWS CLIコマンドで作成)
image.png
パスの部分が/になっているように、これはサービスロールではなくIAMロール

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket",
                "s3:GetReplicationConfiguration"
            ],
            "Resource": "arn:aws:s3:::repli-source"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "s3:GetObjectVersionTagging",
                "s3:GetObjectVersionAcl",
                "s3:GetObjectVersionForReplication"
            ],
            "Resource": "arn:aws:s3:::repli-source/*"
        },
        {
            "Sid": "VisualEditor2",
            "Effect": "Allow",
            "Action": [
                "s3:ReplicateObject",
                "s3:ObjectOwnerOverrideToBucketOwner",
                "s3:ReplicateTags",
                "s3:ReplicateDelete"
            ],
            "Resource": "arn:aws:s3:::repli-destinat/*"
        }
    ]
}
action名 説明
s3:ListBucket Amazon S3バケット内のオブジェクトの一部またはすべてを一覧表示する権限。レプリケーションするオブジェクトの対象を把握するのに必要。
s3:GetReplicationConfiguration AmazonS3バケットに設定されたレプリケーション構成情報を取得する権限。
s3:GetObjectVersionTagging オブジェクトの特定のバージョンに設定されたタグを返す権限。
s3:GetObjectVersionAcl 特定のオブジェクトバージョンのアクセス制御リスト(ACL)を返す権限。
s3:GetObjectVersionForReplication 暗号化されていないオブジェクトとSSE-S3またはSSE-KMSで暗号化されたオブジェクトの両方を複製する権限
s3:ReplicateObject オブジェクトとオブジェクトタグを宛先バケットに複製する権限。
s3:ObjectOwnerOverrideToBucketOwner レプリカ(レプリケーションされたオブジェクト)の所有権を変更する権限。
・参考:レプリケート先バケットポリシーへのレプリカの所有権を変更するアクセス許可の追加
s3:ReplicateTags オブジェクトタグをレプリケーション先のバケットに複製する権限。
s3:ReplicateDelete 削除マーカーをレプリケーション先のバケットに複製する権限。

・参考:AWS のサービス用ロールの作成 (コンソール)
・参考:許可のセットアップ

S3(レプリケーション元)の設定

IAMロールに上記IAMロールの作成で作成したロールを指定する事で、S3にレプリケートを行う権限を付与できる
レプリケーション時間のコントロール (RTC)については追加で課金されるので注意が必要だが、検証する上で時間がかかり過ぎるのも困るので☑している(私はテキストファイル1kbを5、6回レプリケーションをやったが特に課金はされていなかった)

#1 #2
image.png image.png

S3(レプリケーション先)の設定

今回は同一AWSアカウントのバケット間でのレプリケーションなので設定不要

実際にレプリケーションを確認してみる

以下のようにそれぞれレプリケーションが成功しているか?がステータスで分かる

レプリケーション元 レプリケーション先
image.png image.png

サービスロール版

サービスロールの作成

サービスロールはAWS CLIコマンドからしか作れない(と思っている)
ので以下のようなコマンドでサービスロールを作成する

--path /service-role/のようにパスを指定する事でサービスロールになる(サービスロールか?は識別子でしかなくロールの本質的な部分はIAMロールもサービスロールも同じでそれぞれ便宜上の区分で名前が違う。詳細はIAMロールとAWSサービスロールの違いって何?(自分なりの理解)を参照。)

aws iam create-role \
--role-name replication-service-role \
--assume-role-policy-document file://replication-service-role-trust-policy.json \
--path /service-role/

aws iam put-role-policy \
--role-name replication-service-role \
--policy-name replication \
--policy-document file://replication-service-role-inline-policy.json
replication-service-role-trust-policy.json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "s3.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

replication-service-role-inline-policy.jsonの中身はIAMロールの作成の項のJSONとほぼ同じで、"Resource"のバケット名の部分だけ変更したもの(repli-source-cli, repli-destinat-cli

AWS CLIコマンドのリファレンスは以下

・参考:IAM 識別子 IAM ARN
・参考:IAM ロールの作成

S3バケットの作成

単にバケットを作成しただけだと、デフォルトでパブリックアクセス制限がオフになっているので、作成後にパブリックアクセス制限をオンにする

aws s3api create-bucket --bucket repli-source-cli --create-bucket-configuration LocationConstraint=ap-northeast-1
aws s3api create-bucket --bucket repli-destinat-cli --create-bucket-configuration LocationConstraint=ap-northeast-1

aws s3api put-public-access-block 
--bucket repli-source-cli \
--public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true"
aws s3api put-public-access-block \
--bucket repli-destinat-cli \
--public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true"

AWS CLIコマンドのリファレンスは以下

・参考:【AWS CLI】ap-northeast-1でcreate-bucketした際にIllegalLocationConstraintExceptionがスローされた場合の対処法

create-bucketを行う際には--create-bucket-configurationを指定しないとエラーになるので注意

xxxxxxxxxx:/mnt/c/Users/user/OneDrive/ドキュメント$ aws s3api create-bucket --bucket repli-source-cli

An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.
xxxxxxxxxx:/mnt/c/Users/user/OneDrive/ドキュメント$ aws s3api create-bucket --bucket repli-source-cli --region ap-northeast-1

An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.

S3(レプリケーション元)の設定

レプリケーションの設定を行うconfigurationのJSONが今回はかなり大きめなのでテンプレ(スケルトン)を作成して、それを編集するようにする
また、IAMロールではなく、サービスロールをレプリケーション設定に渡す("Role": "arn:aws:iam::xxxxxxxxxxxx:role/service-role/replication-service-role"

aws s3api put-bucket-replication --generate-cli-skeleton > replication-config.json
aws s3api put-bucket-versioning --bucket repli-source-cli --versioning-configuration Status=Enabled
aws s3api put-bucket-replication --bucket repli-source-cli --replication-configuration file://replication-config.json
replication-config.json
{
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/service-role/replication-service-role",
    "Rules": [
        {
            "ID": "sync_to_destinat_cli",
            "Priority": 0,
            "Status": "Enabled",
            "Filter": {
                "Prefix": ""
            },
            "Destination": {
                "Bucket": "arn:aws:s3:::repli-destinat-cli",
                "ReplicationTime": {
                    "Status": "Enabled",
                    "Time": {
                        "Minutes": 15
                    }
                },
                "Metrics": {
                    "Status": "Enabled",
                    "EventThreshold": {
                        "Minutes": 15
                    }
                }
            },
            "DeleteMarkerReplication": {
                "Status": "Disabled"
            }
        }
    ]
}

AWS CLIコマンドのリファレンスは以下

・参考:JSON または YAML 入力ファイルからの AWS CLI スケルトンと入力パラメータの生成

put-bucket-replicationを行う際にはバケットのバージョン管理が有効になっている必要があるので注意

xxxxxxxxxx:/mnt/c/Users/user/OneDrive/ドキュメント$ aws s3api put-bucket-replication --bucket repli-source-cli --replication-configuration file://replication-config.json

An error occurred (InvalidRequest) when calling the PutBucketReplication operation: Versioning must be 'Enabled' on the bucket to apply a replication configuration

--replication-configurationのJSONについて、Rule内に"Filter": {"Prefix": ""}がないとエラー1になるので注意

"Filter": {
   "Prefix": ""
}

S3(レプリケーション先)の設定

バケットのバージョン管理が有効になっている必要があるのでそれを設定する

aws s3api put-bucket-versioning --bucket repli-destinat-cli --versioning-configuration Status=Enabled

実際にレプリケーションを確認してみる

レプリケーション元にレプリケーションのテスト用.txtを追加して、レプリケーション先のオブジェクトを取得してみると・・・

aws s3 cp レプリケーションのテスト用.txt s3://repli-source-cli

xxxxxxxxxx:/mnt/c/Users/user/OneDrive/ドキュメント$ aws s3api head-object \--bucket repli-source-cli --key レプリケーションのテスト用.txt
{
    ...,
    "ReplicationStatus": "COMPLETED"
}

xxxxxxxxxx:/mnt/c/Users/user/OneDrive/ドキュメント$ aws s3api list-objects --bucket repli-destinat-cli
{
    "Contents": [
        {
            "Key": "レプリケーションのテスト用.txt",
            "LastModified": "2021-09-06T14:40:46+00:00",
            ...,
            "Owner": {...}
        }
    ]
}

xxxxxxxxxx:/mnt/c/Users/user/OneDrive/ドキュメント$ aws s3api head-object --bucket repli-destinat-cli --key レプリケーションのテスト用.txt
{
    ...,
    "ReplicationStatus": "REPLICA"
}

"ReplicationStatus": "COMPLETED""ReplicationStatus": "REPLICA"となっているようにレプリケーションが成功している事が分かる

AWS CLIコマンドのリファレンスは以下

まとめ

レプリケーションはかなり簡単に設定できる
また、IAMロールでもサービスロールでもレプリケーションは設定できる

おまけ

作成したサービスロールは、ちゃんとサービスロールになっているか?

image.png
パスの部分が/service-role/になっているように、これはIAMロールではなくサービスロール


  1. An error occurred (MalformedXML) when calling the PutBucketReplication operation: The XML you provided was not well-formed or did not validate against our published schema
    参考:Python Boto3 PutBucketReplication operation: The XML you provided was not well-formed or did not validate against our published schema 

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