はじめに
こんにちは、CSM課の設樂です。
CloudFrontのOriginをAWS CLIを使って切り替えてみました。
AWS CLIで試してみたきっかけ
平時に利用しているリージョンに災害が起きた場合に平時に利用しているリージョンから別のリージョンに切り替えが必要になるケースがあると思います。
災害時の切り替えに時間的な制限がある場合、CloudFrontの切り替え操作に慣れている人が必ず対応できるとは限らず、不慣れな人でも操作ミスのリスクを減らす有効な方法としてAWS CLIを使って試してみました。
事前準備
東京リージョンとシンガポールリージョンのS3にコンテンツを配置し、CloudFrontのディストリビューションを作成しました。
コンテンツを迅速に配信する方法 – アマゾン ウェブ サービス (AWS)東京リージョンとシンガポールリージョンのCloudFrontのOriginを準備しました。

Behaviorsの設定で平時に利用するリージョンは東京リージョンにしています。

東京リージョンのコンテンツが配信されています。

AWS CLIで切り替える
- CloudFrontのディストリビューションID
E34E7NUNG3T78E

- メンテナンスを楽にするため、ディストリビューションIDを変数に格納します。
distribution_id=E34E7NUNG3T78E
- ディストリビューション設定を取得します。
aws cloudfront get-distribution-config --id ${distribution_id} > get-distribution-config.json
- 更新するディストリビューション設定を作成します。
cat get-distribution-config.json | jq .DistributionConfig > update-distribution-config.json
※更新するディストリビューション設定の形式は下記のため、"jq .DistributionConfig"で抽出しています。
{
"CallerReference": "cli-1574382155-496510",
"Aliases": {
"Quantity": 0
},
"DefaultRootObject": "index.html",
"Origins": {
"Quantity": 1,
"Items": [
{
"Id": "awsexamplebucket.s3.amazonaws.com-1574382155-273939",
"DomainName": "awsexamplebucket.s3.amazonaws.com",
"OriginPath": "",
"CustomHeaders": {
"Quantity": 0
},
"S3OriginConfig": {
"OriginAccessIdentity": ""
}
}
]
},
"OriginGroups": {
"Quantity": 0
},
"DefaultCacheBehavior": {
"TargetOriginId": "awsexamplebucket.s3.amazonaws.com-1574382155-273939",
"ForwardedValues": {
"QueryString": false,
"Cookies": {
"Forward": "none"
},
"Headers": {
"Quantity": 0
},
"QueryStringCacheKeys": {
"Quantity": 0
}
},
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "allow-all",
"MinTTL": 0,
"AllowedMethods": {
"Quantity": 2,
"Items": [
"HEAD",
"GET"
],
"CachedMethods": {
"Quantity": 2,
"Items": [
"HEAD",
"GET"
]
}
},
"SmoothStreaming": false,
"DefaultTTL": 86400,
"MaxTTL": 31536000,
"Compress": false,
"LambdaFunctionAssociations": {
"Quantity": 0
},
"FieldLevelEncryptionId": ""
},
"CacheBehaviors": {
"Quantity": 0
},
"CustomErrorResponses": {
"Quantity": 0
},
"Comment": "",
"Logging": {
"Enabled": false,
"IncludeCookies": false,
"Bucket": "",
"Prefix": ""
},
"PriceClass": "PriceClass_All",
"Enabled": false,
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true,
"MinimumProtocolVersion": "TLSv1",
"CertificateSource": "cloudfront"
},
"Restrictions": {
"GeoRestriction": {
"RestrictionType": "none",
"Quantity": 0
}
},
"WebACLId": "",
"HttpVersion": "http2",
"IsIPV6Enabled": true
}
update-distribution — AWS CLI 1.19.3 Command Reference
- 更新するディストリビューション設定の"TargetOriginId"を東京リージョンからシンガポールリージョンに変更するため、書き換えます。
sed -i -e 's/"TargetOriginId": "S3-s3-tokyo-bucket-test"/"TargetOriginId": "S3-s3-singapore-bucket-test"/g' update-distribution-config.json
- ETagを確認します。
cat get-distribution-config.json | jq -r .ETag E28CSRL5LOP0UR
ETagはディストリビューション設定ごとにことなるため、変数に格納します。
etag=E28CSRL5LOP0URディストリビューション設定を更新します。
aws cloudfront update-distribution --id $distribution_id --distribution-config file://update-distribution-config.json --if-match ${etag}
- ディストリビューション設定の確認画面が表示されるので、"q"で閉じます。
{
"ETag": "E8LHSC6OZVW9G",
"Distribution": {
"Id": "E34E7NUNG3T78E",
"ARN": "arn:aws:cloudfront::XXXXXXXXXXXXX:distribution/E34E7NUNG3T78E",
"Status": "InProgress",
"LastModifiedTime": "2021-02-07T01:30:34.715000+00:00",
"InProgressInvalidationBatches": 0,
"DomainName": "d283aj9tgs6ruo.cloudfront.net",
"ActiveTrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"ActiveTrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"DistributionConfig": {
"CallerReference": "1612652664457",
"Aliases": {
"Quantity": 0
},
"DefaultRootObject": "",
"Origins": {
"Quantity": 2,
"Items": [
{
"Id": "S3-s3-tokyo-bucket-test",
"DomainName": "s3-tokyo-bucket-test.s3.amazonaws.com",
"OriginPath": "",
"CustomHeaders": {
"Quantity": 0
},
"S3OriginConfig": {
"OriginAccessIdentity": ""
},
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"OriginShield": {
"Enabled": false
}
},
{
"Id": "S3-s3-singapore-bucket-test",
"DomainName": "s3-singapore-bucket-test.s3.amazonaws.com",
"OriginPath": "",
"CustomHeaders": {
"Quantity": 0
},
"S3OriginConfig": {
"OriginAccessIdentity": ""
},
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"OriginShield": {
"Enabled": false
}
}
]
},
"OriginGroups": {
"Quantity": 0
},
"DefaultCacheBehavior": {
"TargetOriginId": "S3-s3-singapore-bucket-test",
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TrustedKeyGroups": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "allow-all",
"AllowedMethods": {
"Quantity": 2,
"Items": [
"HEAD",
"GET"
],
"CachedMethods": {
"Quantity": 2,
"Items": [
"HEAD",
"GET"
]
}
},
"SmoothStreaming": false,
"Compress": false,
"LambdaFunctionAssociations": {
"Quantity": 0
},
"FieldLevelEncryptionId": "",
"CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6"
},
"CacheBehaviors": {
"Quantity": 0
},
"CustomErrorResponses": {
"Quantity": 0
},
"Comment": "",
"Logging": {
"Enabled": false,
"IncludeCookies": false,
"Bucket": "",
"Prefix": ""
},
"PriceClass": "PriceClass_All",
"Enabled": true,
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true,
"MinimumProtocolVersion": "TLSv1",
"CertificateSource": "cloudfront"
},
"Restrictions": {
"GeoRestriction": {
"RestrictionType": "none",
"Quantity": 0
}
},
"WebACLId": "",
"HttpVersion": "http2",
"IsIPV6Enabled": true
}
}
}
Behaviorsの設定がシンガポールリージョンに変更されています。

CloudFrontのキャッシュをクリアします。

※キャッシュをクリアする方法
https://blog.serverworks.co.jp/tech/2019/05/15/cloudfront-cache-clear/
- シンガポールリージョンのコンテンツが配信されています。

さいごに
シンガポールのコンテンツを探していてシンガポールの画像検索をしたところ、マリーナベイサンズの画像がたくさんでてきました。
個人的にはシンガポールといえばマーライオンというイメージだったのですが、いまはもうマリーナベイサンズなのですね。