{CloudFront}オリジンアクセスアイデンティティ (OAI) を使用して Amazon S3 コンテンツへのアクセスを制限する


https://docs.aws.amazon.com/ja_jp/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html
https://aws.amazon.com/jp/premiumsupport/knowledge-center/cloudfront-serve-static-website/


ウェブサイトエンドポイントとして設定されている Amazon S3 バケットを使用する場合、
CloudFront でカスタムオリジンとして設定する必要があります。
オリジンアクセスアイデンティティ機能を使用することはできません。


-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

 

-- 1.2 jqインストール
sudo yum -y install jq


-- 2. S3 バケットを作成する

aws s3 ls

aws s3 mb s3://bucket123

 


-- 3. インデックスドキュメントの設定

vim index.html

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>My Website Home Page</title>
</head>
<body>
  <h1>Welcome to my website</h1>
  <p>Now hosted on Amazon S3!</p>
</body>
</html>

aws s3api put-object --bucket bucket123 --key index.html --body index.html --content-type text/html

 

 

-- 4. ディストリビューションの作成


aws cloudfront create-distribution \
--origin-domain-name bucket123.s3.ap-northeast-1.amazonaws.com \
--default-root-object index.html

 

aws cloudfront list-distributions

aws cloudfront get-distribution \
--id AAAAAAAAAAAAA

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA

 


-- 5. OAIの作成

aws cloudfront create-cloud-front-origin-access-identity \
--cloud-front-origin-access-identity-config '{
    "CallerReference": "caller01",
    "Comment": "oai01"
}'


aws cloudfront list-cloud-front-origin-access-identities

aws cloudfront get-cloud-front-origin-access-identity \
--id BBBBBBBBBBBBBB

 

-- 6. OAIをディストリビューションに追加する

 

aws cloudfront get-distribution \
--id AAAAAAAAAAAAA

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA


aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA | jq -r .DistributionConfig > distribution.json

vim distribution.json

Origins -> Items -> CustomHeaders の下の
CustomOriginConfigを削除して下記S3OriginConfigを追加


        "S3OriginConfig": {
            "OriginAccessIdentity": "origin-access-identity/cloudfront/BBBBBBBBBBBBBB"
        },

 

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA | jq -r .ETag

aws cloudfront update-distribution \
--id AAAAAAAAAAAAA \
--if-match CCCCCCCCCCCCC \
--distribution-config file://distribution.json

 


-- 7. OAI に Amazon S3 バケット内のファイルの読み込みアクセス許可を付与する


vim b.json

{
    "Version": "2012-10-17",
    "Id": "PolicyForCloudFrontPrivateContent",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity BBBBBBBBBBBBBB"
            },
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": "arn:aws:s3:::bucket123/*"
        }
    ]
}


aws s3api put-bucket-policy \
--bucket bucket123 \
--policy file://b.json


aws s3api get-bucket-policy \
--bucket bucket123

 

 


-- 8. 動作確認

 

curl -v -X GET http://xxxxxxxxxxxxxx.cloudfront.net/index.html

 


-- 9. クリーンアップ

 

 

-- ディストリビューションの無効化

aws cloudfront get-distribution \
--id AAAAAAAAAAAAA

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA


※ distribution.jsonはget-distribution-configコマンドのDistributionConfigから取得し、Enabledをfalseに変更する

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA | jq -r .DistributionConfig > distribution.json

sed -i 's/"Enabled": true/"Enabled": false/' distribution.json

 

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA | jq -r .ETag

aws cloudfront update-distribution \
--id AAAAAAAAAAAAA \
--if-match DDDDDDDDDDDDDD \
--distribution-config file://distribution.json


※ if-matchにはETagの値をセット

無効化されるまで待つ


-- ディストリビューションの削除

aws cloudfront get-distribution \
--id AAAAAAAAAAAAA

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA

aws cloudfront get-distribution-config \
--id AAAAAAAAAAAAA | jq -r .ETag

aws cloudfront delete-distribution \
--id AAAAAAAAAAAAA \
--if-match EEEEEEEEEEEEEE

 

aws cloudfront list-distributions


-- OAIの削除

aws cloudfront list-cloud-front-origin-access-identities

aws cloudfront get-cloud-front-origin-access-identity \
--id BBBBBBBBBBBBBB

aws cloudfront delete-cloud-front-origin-access-identity \
--id BBBBBBBBBBBBBB \
--if-match FFFFFFFFFFFFFF


-- バケットの削除
aws s3 ls

aws s3 rb s3://bucket123 --force