https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/userguide/HostingWebsiteOnS3Setup.html
https://qiita.com/airtanker/items/0cc194c8dcff1ee5a531
-- 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
-- 2. S3 バケットを作成する
aws s3 ls
aws s3 mb s3://bucket123
-- 3. 静的ウェブサイトホスティングの有効化
{
"IndexDocument": {
"Suffix": "index.html"
},
"ErrorDocument": {
"Key": "error.html"
}
}
aws s3api put-bucket-website \
--bucket bucket123 \
--website-configuration file://a.json
aws s3api get-bucket-website \
--bucket bucket123
-- 4. パブリックアクセスブロック設定の編集
-- 4.1 アカウントレベル
aws s3control put-public-access-block \
--account-id 999999999999 \
--public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"
aws s3control get-public-access-block \
--account-id 999999999999
-- 4.2 バケットレベル
aws s3api put-public-access-block \
--bucket bucket123 \
--public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"
aws s3api get-public-access-block \
--bucket bucket123
-- 5. バケットポリシーの設定
vim b.json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::bucket123/*",
"Condition": {
"IpAddress": {
"aws:SourceIp": "203.0.113.1/32"
}
}
}
]
}
※IPアドレス制限追加
aws s3api put-bucket-policy \
--bucket bucket123 \
--policy file://b.json
aws s3api get-bucket-policy \
--bucket bucket123
-- 6. インデックスドキュメントの設定
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
※ --content-typeオプションの指定必要
-- 7. エラードキュメントの設定
vim error.html
<h1>This is error page.</h1>
aws s3api put-object --bucket bucket123 --key error.html --body error.html --content-type text/html
-- 8. 動作確認
http://bucket123.s3-website-ap-northeast-1.amazonaws.com
curl http://bucket123.s3-website-ap-northeast-1.amazonaws.com
アカウントレベルのパブリックアクセスブロック解除でエラーになる
バケットレベルのパブリックアクセスブロック解除でエラーになる
バケットポリシー削除でエラーになる
したがって静的webホスティングのためには上記3つのすべてで許可が必要
-- 9. クリーンアップ
aws s3 rb s3://bucket123 --force
-- アカウントレベルのパブリックアクセスブロックの有効化
aws s3control put-public-access-block \
--account-id 999999999999 \
--public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true"
aws s3control get-public-access-block \
--account-id 999999999999