コンテンツにスキップ

S3の静的サイトホスティング

手動でGUI操作

アクセス権限で以下のチェックボックスを外す

  • [ ] 新規のパブリックバケットポリシーをブロックする (推奨)
  • [ ] バケットにパブリックポリシーがある場合、パブリックアクセスとクロスアカウントアクセスをブロックする (推奨)
{
  "Version":"2012-10-17",
  "Statement":[{
    "Sid":"PublicReadGetObject",
        "Effect":"Allow",
      "Principal": "*",
      "Action":["s3:GetObject"],
      "Resource":["arn:aws:s3:::example-bucket/*"
      ]
    }
  ]
}

参考: https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/WebsiteAccessPermissionsReqd.html

TerraFormで自動

覚えておくと便利なコマンド:

$ terraform init   # initialize
$ terraform apply  # attach
$ terraform show   # check result

example-s3.tf

provider "aws" {
  access_key = "XXXXXXXXXXXX"
  secret_key = "YYYYYYYYYYYY"
  region     = "us-east-1"
}

resource "aws_s3_bucket" "b" {
  bucket = "s3-website-test.example.com"
  acl    = "public-read"
  policy = <<POLICY
{
  "Version":"2012-10-17",
  "Statement":[{
    "Sid":"PublicReadGetObject",
        "Effect":"Allow",
      "Principal": "*",
      "Action":["s3:GetObject"],
      "Resource":["arn:aws:s3:::s3-website-test.example.com/*"
      ]
    }
  ]
}
POLICY

  website {
    index_document = "index.html"
    error_document = "error.html"

    routing_rules = <<EOF
[{
    "Condition": {
        "KeyPrefixEquals": "docs/"
    },
    "Redirect": {
        "ReplaceKeyPrefixWith": "documents/"
    }
}]
EOF
  }
}