LoginSignup
0
0

More than 3 years have passed since last update.

静的Webサイト用S3バケットを用意してそのバケットだけに書き込めるアクセスキーを発行する

Last updated at Posted at 2021-01-07

bucketを作成

  • 例: バケット名 = test.example.com
  • 作られたバケットを選んで「Permissions」タブを開く
  • Block public access (bucket settings) に全てをOFF
  • Access control list (ACL)にてEveryone (public access)にRead権限をつける

bucketのStatic website hostingを設定

  • Static website hosting: Enable
  • Hosting type: Host a static website
  • Index document: index.html
  • Error document: error.html

route53でウェブサイトエンドポイントに割り当てたいホストを割り当てる

  • Aレコードを選ぶ
  • Alias to S3 website endpoitを選ぶ
  • リージョンとバケットを選ぶ

IAMを作成

  • User name: test.example.com-s3fullaccess
  • Access type: Programmatic access
  • 権限はなしで一旦作成
  • 作成後「Permissions」 -> 「Add inline policy」でJSONタブを開いて以下で設定
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::test.example.com",
                "arn:aws:s3:::test.example.com/*"
            ]
        }
    ]
}

動作確認

  • AWS cliからアップロードできることを確認
AWS_ACCESS_KEY_ID=XXXXXXX AWS_SECRET_ACCESS_KEY=XXXXXXXXXX AWS_DEFAULT_REGION=ap-northeast-1 aws s3 cp test.html s3://test.example.com/
  • 別のバケットにはアップロードできないことを確認
AWS_ACCESS_KEY_ID=XXXXXXX AWS_SECRET_ACCESS_KEY=XXXXXXXXXX AWS_DEFAULT_REGION=ap-northeast-1 aws s3 cp test.html s3://test2.example.com/
  • 割り当てたドメイン下で表示できることを確認

  • ファイルを削除できることを確認
AWS_ACCESS_KEY_ID=XXXXXXX AWS_SECRET_ACCESS_KEY=XXXXXXXXXX AWS_DEFAULT_REGION=ap-northeast-1 aws s3 rm s3://test.example.com/test.html

参考

https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/EnableWebsiteHosting.html
https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/IndexDocumentSupport.html
https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/WebsiteAccessPermissionsReqd.html
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-envvars.html
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-services-s3-commands.html#using-s3-commands-managing-objects-copy

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0