How do I configure an S3 bucket for static website hosting? - Amazon Simple Storage Service

How do I configure an S3 bucket for static website hosting?

You can host a static website on Amazon S3. On a static website, individual webpages include static content. A static website might also contain client-side scripts. By contrast, a dynamic website relies on server-side processing, including server-side scripts such as PHP, JSP, or ASP.NET. Amazon S3 does not support server-side scripting.

You can use the following quick procedures to configure an S3 bucket for static website hosting in the Amazon S3 console. For more information, see Hosting a Static Website on Amazon S3 in the Amazon Simple Storage Service Developer Guide. For information about configuring a static website with a custom domain, see Configuring a static website using a custom domain registered with Route 53 in the Amazon Simple Storage Service Developer Guide.

Step 1: Configuring a bucket for static website hosting

  1. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/.

  2. In the Buckets list, choose the name of the bucket that you want to use to host a static website.

  3. Choose Properties.

  4. Under Static website hosting, choose Edit.

  5. Choose Use this bucket to host a website.

  6. Under Static website hosting, choose Enable.

  7. In Index document, enter the file name of the index document, typically index.html.

    The index document name is case sensitive and must exactly match the file name of the HTML index document that you plan to upload to your S3 bucket. When you configure a bucket for website hosting, you must specify an index document. Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders. For more information, see Configuring an index document in the Amazon Simple Storage Service Developer Guide.

  8. (Optional) If you want to provide your own custom error document for 4XX class errors, in Error document, enter the custom error document file name.

    The error document name is case sensitive and must exactly match the file name of the HTML error document that you plan to upload to your S3 bucket. If you don't specify a custom error document and an error occurs, Amazon S3 returns a default HTML error document. For more information, see Configuring a custom error document in the Amazon Simple Storage Service Developer Guide.

  9. (Optional) If you want to specify advanced redirection rules, in Redirection rules, enter XML to describe the rules.

    For example, you can conditionally route requests according to specific object key names or prefixes in the request. For more information, see Configuring advanced conditional redirects in the Amazon Simple Storage Service Developer Guide.

  10. Choose Save changes.

    Amazon S3 enables static website hosting for your bucket. At the bottom of the page, under Static website hosting, you see the website endpoint for your bucket.

  11. Upload the index document to your bucket.

    For step-by-step instructions on uploading an object to an S3 bucket, see Uploading Files by Pointing and Clicking.

  12. Upload other files for your website, including optional custom error documents.

In the next section, you set the permissions required to access your bucket as a static website.

Step 2: Editing S3 Block Public Access settings

By default, Amazon S3 blocks public access to your account and buckets. If you want to use a bucket to host a static website, you can use these steps to edit your block public access settings.

Warning

Before you complete this step, review Using Amazon S3 Block Public Access to ensure that you understand and accept the risks involved with allowing public access. When you turn off block public access settings to make your bucket public, anyone on the internet can access your bucket. We recommend that you block all public access to your buckets.

  1. Open the Amazon S3 console at https://console.aws.amazon.com/s3/.

  2. Choose the name of the bucket that you have configured as a static website.

  3. Choose Permissions.

  4. Under Block public access (bucket settings), choose Edit.

  5. Clear Block all public access, and choose Save changes.

    Warning

    Before you complete this step, review Using Amazon S3 Block Public Access to ensure you understand and accept the risks involved with allowing public access. When you turn off block public access settings to make your bucket public, anyone on the internet can access your bucket. We recommend that you block all public access to your buckets.

    Amazon S3 turns off Block Public Access settings for your bucket. To create a public, static website, you might also have to edit the Block Public Access settings for your account before adding a bucket policy. If account settings for Block Public Access are currently turned on, you see a note under Block public access (bucket settings).

Step 3: Adding a bucket policy

After you edit S3 Block Public Access settings, you can add a bucket policy to grant public read access to your bucket. When you grant public read access, anyone on the internet can access your bucket.

Important

The following policy is an example only and allows full access to the contents of your bucket. Before you proceed with this step, review How can I secure the files in my Amazon S3 bucket? to ensure that you understand the best practices for securing the files in your S3 bucket and risks involved in granting public access.

  1. Under Buckets, choose the name of your bucket.

  2. Choose Permissions.

  3. Under Bucket Policy, choose Edit.

  4. To grant public read access for your website, copy the following bucket policy, and paste it in the Bucket policy editor.

    { "Version": "2012-10-17", "Statement": [ { "Sid": "PublicReadGetObject", "Effect": "Allow", "Principal": "*", "Action": [ "s3:GetObject" ], "Resource": [ "arn:aws:s3:::example.com/*" ] } ] }
  5. Update the Resource to your bucket name.

    In the preceding example bucket policy, example.com is the bucket name. To use this bucket policy with your own bucket, you must update this name to match your bucket name.

  6. Choose Save changes.

    A message appears indicating that the bucket policy has been successfully added.

    If you see an error that says Policy has invalid resource, confirm that the bucket name in the bucket policy matches your bucket name. For information about adding a bucket policy, see How do I add an S3 bucket policy?

    If you get an error message and cannot save the bucket policy, check your account and bucket Block Public Access settings to confirm that you allow public access to the bucket.

After you edit S3 Block Public Access settings, you can add a bucket policy to grant public read access to your bucket. When you grant public read access, anyone on the internet can access your bucket.

Important

The following policy is an example only and allows full access to the contents of your bucket. Before you proceed with this step, review How can I secure the files in my Amazon S3 bucket? to ensure that you understand the best practices for securing the files in your S3 bucket and risks involved in granting public access.

Step 4: Testing your website endpoint

After you configure your bucket as a static website and set permissions, you can access your website through an Amazon S3 website endpoint. For more information, see Website endpoints in the Amazon Simple Storage Service Developer Guide. For a complete list of Amazon S3 website endpoints, see Amazon S3 Website Endpoints in the Amazon Web Services General Reference.

  1. Under Buckets, choose the name of your bucket.

  2. Choose Properties.

  3. At the bottom of the page, under Static website hosting, choose your Bucket website endpoint.

    Your index document opens in a separate browser window.