{CloudFront}標準ログ (アクセスログ) の設定および使用

https://docs.aws.amazon.com/ja_jp/AmazonCloudFront/latest/DeveloperGuide/AccessLogs.html

 

-- 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 バケットを作成する
bucket123 -> オリジン用
bucket456 -> アクセスログ保存用

aws s3 ls

aws s3 mb s3://bucket123
aws s3 mb s3://bucket456

 


-- 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 11111111111111

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

 


-- 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 2222222222222

 

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

 

aws cloudfront get-distribution \
--id 11111111111111

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


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

vim distribution.json

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


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

 

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

aws cloudfront update-distribution \
--id 11111111111111 \
--if-match 33333333333333 \
--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 2222222222222"
            },
            "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 11111111111111

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


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

vim distribution.json


        "Logging": {
            "Enabled": false,
            "IncludeCookies": false,
            "Bucket": "",
            "Prefix": ""
        },

        "Logging": {
            "Enabled": true,
            "IncludeCookies": false,
            "Bucket": "bucket456.s3.amazonaws.com",
            "Prefix": ""
        },

 

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

aws cloudfront update-distribution \
--id 11111111111111 \
--if-match 4444444444444 \
--distribution-config file://distribution.json

 

-- 10. ログ生成を確認

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

aws s3 ls s3://bucket456 --recursive

aws s3 cp s3://bucket456/11111111111111.2022-07-13-15.00000000.gz - | zcat

 

-- 11. クリーンアップ

 

 

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

aws cloudfront get-distribution \
--id 11111111111111

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


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

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

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

 

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

aws cloudfront update-distribution \
--id 11111111111111 \
--if-match 55555555555555 \
--distribution-config file://distribution.json


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

無効化されるまで待つ


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

aws cloudfront get-distribution \
--id 11111111111111

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

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

aws cloudfront delete-distribution \
--id 11111111111111 \
--if-match 66666666666666

 

aws cloudfront list-distributions


-- OAIの削除

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

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

aws cloudfront delete-cloud-front-origin-access-identity \
--id 2222222222222 \
--if-match 77777777777777


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

aws s3 rb s3://bucket123 --force
aws s3 rb s3://bucket456 --force