0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

こんにちは。
株式会社クラスアクト インフラストラクチャ事業部の大塚です。

今回は巷でよく聞くS3でWebサイトのホスティングをやってみたいと思います。

環境イメージ

S3のバケットを作成し、そのバケットを使って静的Webサイトをホスティングします。
デフォルトの画面をindex.htmlが担い、エラーを示す画面をerror.htmlが担うようにしていきます。
20240620から-ページ24.drawio.png

参考サイト

公式を参考にしました。

構築

S3バケットの作成

S3の管理画面に遷移して、バケットを作成を押下します。
Untitled (16).png
長くなってしまいますが、以下のような設定で作成をしていきました。
バケットタイプは汎用にしています。ブロックパブリックアクセス設定はチェックを全て外します。デフォルトだと全てにチェックが付いていますが、これを外さないとホスティングが出来ません。
またバケットの名前ですが、以前お名前.comで購入してRoute53と連携しているものを使用しています。将来的に独自ドメインでアクセスできるようにしたいなら、併せたほうが良いらしいですがそうでもないなら名前はドメインでなければならないという事はなさそうです。
screencapture-ap-northeast-1-console-aws-amazon-s3-bucket-create-2024-07-10-20_34_45.png
バケットが作成されたことを確認します。プロパティタブの下の方に静的ウェブサイトホスティングという項目がありますので、これの編集ボタンを押下します。
screencapture-ap-northeast-1-console-aws-amazon-s3-buckets-ohtsuka-aws-xyz-2024-07-10-20_35_38.png
画面が切り替わりましたら有効にするというラジオボタンを選択してください。
するとオプションが色々表示されるようになります。
ホスティングタイプで静的ウェブサイトをホストするを選択します。またインデックスドキュメントをindex.htmlと入力します。エラードキュメントをerror.htmlと入力します。
ここにあるHTMLファイルは後程作成してバケットにアップロードして使います。
index.htmlはホスティングする際にバケットに払い出されるURLをWebブラウジングする際に使用するデフォルトのHTMLとなり、error.htmlは例えば存在しないファイルなどに対してアクセスしようとした際に使用されるHTMLとなります。
screencapture-ap-northeast-1-console-aws-amazon-s3-bucket-ohtsuka-aws-xyz-property-website-edit-2024-07-10-20_38_53 (1).png
静的ホスティングを有効にすると以下のように画面の表示が変わります。
バケットウェブサイトエンドポイントのURLを控えます。
Untitled (1).png
控えたURLに対してWebブラウジングしてみると「403 Forbidden」と権限的に許可されていないエラーメッセージが表示されます。
これを解消していきます。
Untitled (2).png
アクセス許可タブのバケットポリシーの項目があります。こちらを編集しますので、編集ボタンを押下します。
screencapture-ap-northeast-1-console-aws-amazon-s3-buckets-ohtsuka-aws-xyz-2024-07-10-21_17_56.png
以下の内容を貼り付けて更新します。
Resourceの部分は各自のバケット名を入力します。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::ohtsuka-aws.xyz/*"
            ]
        }
    ]
}

screencapture-ap-northeast-1-console-aws-amazon-s3-bucket-ohtsuka-aws-xyz-property-policy-edit-2024-07-10-21_19_51.png
更新されたことを確認します。
Untitled (3).png
改めてURLに対してWebブラウジングをしてみます。先程のエラー内容と異なり、今度は「Not Found」という内容になると思います。これはindex.htmlもerror.htmlも無いことが原因となります。これらのファイルを作成していきます。
Untitled (4).png
以下のindex.htmlを作成します。中身はなんでも大丈夫です。

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My S3 Hosting Website</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }
        h1 {
            color: #333;
        }
        p {
            color: #666;
        }
    </style>
</head>
<body>
    <h1>Welcome to my S3 Hosting website</h1>
    <p>AWS is so exciting,isn't it?</p>
</body>
</html>

このHTMLをアップロードしていきます。
バケットのオブジェクトタブからアップロードボタンを押下します。
Untitled (5).png
ファイルを追加ボタンを押下してindex.htmlを選択します。アップロードボタンを押下します。
screencapture-ap-northeast-1-console-aws-amazon-s3-upload-ohtsuka-aws-xyz-2024-07-10-21_27_54.png
アップロードされたことを確認します。
Untitled (6).png
改めてWebブラウジングしていきましょう。「Not Found」から変わりHTMLの中身が表示されるようになりました。
Untitled (7).png
この調子でerror.htmlも作成してバケットにアップロードしていきます。
以下の内容でHTMLを作成しました。

error.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Error - My S3 Hosting Website</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            background-color: #ff4c4c;
            color: #fff;
        }
        h1 {
            color: #fff;
            font-size: 3em;
        }
        p {
            color: #fff;
            font-size: 1.5em;
        }
        .error-icon {
            font-size: 5em;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="error-icon">⚠️</div>
    <h1>Error 404: Page Not Found</h1>
    <p>Sorry, the page you are looking for does not exist.</p>
    <p>AWS is so exciting, isn't it?</p>
</body>
</html>

これをバケットにアップロードします。
Untitled (8).png
Webブラウジングをする際に、例えば存在しないファイルに対してアクセスしようと試みます。
例えばhttp://"バケットウェブサイトエンドポイント"/hogehogeなどとしてみます。
するとerror.htmlの内容が表示されました。
Untitled (9).png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?