How to Host your Static Website with AWS Tutorial

How to Host your Static Website with AWS Tutorial

Last updated on 25th Sep 2020, Blog, Tutorials

About author

Aravind (Sr Technical Manager )

High level Domain Expert in TOP MNCs with 8+ Years of Experience. Also, Handled Around 16+ Projects and Shared his Knowledge by Writing these Blogs for us.

(5.0) | 14452 Ratings 346

You can use Amazon S3 to host a static website. On a static website, individual web pages include static content. They 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, but AWS has other resources for hosting dynamic websites. To learn more about website hosting on AWS, see Web Hosting.

You can use the AWS Amplify Console to host a single page web app. The AWS Amplify Console supports single page apps built with single page app frameworks (for example, React JS, Vue JS, Angular JS, and Nuxt) and static site generators (for example, Gatsby JS, React-static, Jekyll, and Hugo). For more information, see Getting Started in the AWS Amplify Console User Guide.To configure your bucket for static website hosting, you can use the AWS Management Console without writing any code. You can also create, update, and delete the website configuration programmatically by using the AWS SDKs. The SDKs provide wrapper classes around the Amazon S3 REST API. If your application requires it, you can send REST API requests directly from your application.

To host a static website on Amazon S3, you configure an Amazon S3 bucket for website hosting and then upload your website content to the bucket. When you configure a bucket as a static website, you must enable website hosting, set permissions, and create and add an index document. Depending on your website requirements, you can also configure redirects, web traffic logging, and a custom error document.

After you configure your bucket as a static website, you can access the bucket through the AWS Region-specific Amazon S3 website endpoints for your bucket. Website endpoints are different from the endpoints where you send REST API requests. For more information, see Website endpoints. Amazon S3 doesn’t support HTTPS access for website endpoints. If you want to use HTTPS, you can use CloudFront to serve a static website hosted on Amazon S3. For more information, see Speeding up your website with Amazon CloudFront.

Configuring a static website using a custom domain registered with Route 53.Suppose that you want to host your static website on Amazon S3. You’ve registered a domain with Amazon Route 53 (for example, example.com), and you want requests for http://www.example.com and http://example.com to be served from your Amazon S3 content. You can use this walkthrough to learn how to host a static website and create redirects on Amazon S3 for a website with a custom domain name that is registered with Route 53. You can work with an existing website that you want to host on Amazon S3, or you use this walkthrough to start from scratch.

Once you complete this walkthrough, you can optionally use Amazon CloudFront to improve the performance of your website. For more information, see Speeding up your website with Amazon CloudFront.

Amazon S3 does not support HTTPS access to the website. If you want to use HTTPS, you can use Amazon CloudFront to serve a static website hosted on Amazon S3.

For more information, see How do I use CloudFront to serve a static website hosted on Amazon S3? and Requiring HTTPS for communication between viewers and AWS CloudFormation.

Automating static website set up with an AWS CloudFormation template

You can use an AWS CloudFormation template to automate your static website setup. The AWS CloudFormation template sets up the components that you need to host a secure static website so that you can focus more on your website’s content and less on configuring components.

The AWS CloudFormation template includes the following components:

  • Amazon S3‐Creates an Amazon S3 bucket to host your static website.
  • CloudFront‐ Creates a CloudFront distribution to speed up your static website.
  • Lambda@Edge‐Uses Lambda@Edge to add security headers to every server response. Security headers are a group of headers in the web server response that tell web browsers to take extra security precautions. For more information, refer to this blog post: Adding HTTP security headers using Lambda@Edge and Amazon CloudFront.

This AWS CloudFormation template is available for you to download and use. For information and instructions, see Getting started with a secure static website in the Amazon CloudFront Developer Guide.

Topics

  • Before you begin
  • Step 1: Register a custom domain with Route 53
  • Step 2: Create two buckets
  • Step 3: Configure your root domain bucket for website hosting
  • Step 4: Configure your subdomain bucket for website redirect
  • Step 5: Configure logging for website traffic
  • Step 6: Upload index and website content

Before you begin

As you follow the steps in this example, you work with the following services:

Amazon Route 53 – You use Route 53 to register domains and to define where you want to route internet traffic for your domain. The example shows how to create Route 53 alias records that route traffic for your domain (example.com) and subdomain (www.example.com) to an Amazon S3 bucket that contains an HTML file.

Amazon S3 – You use Amazon S3 to create buckets, upload a sample website page, configure permissions so that everyone can see the content, and then configure the buckets for website hosting.

Subscribe For Free Demo
[contact-form-7 404 "Not Found"]

Step 1: Register a custom domain with Route 53

If you don’t already have a registered domain name, such as example.com, register one with Route 53. For more information, see Registering a New Domain in the Amazon Route 53 Developer Guide. After you register your domain name, you can create and configure your Amazon S3 buckets for website hosting.

Step 2: Create two buckets

To support requests from both the root domain and subdomain, you create two buckets.

  • Domain bucket ‐ example.com
  • Subdomain bucket ‐ www.example.com

These bucket names must exactly match your domain name. In this example, the domain name is example.com. You host your content out of the root domain bucket (example.com). You create a redirect request for the subdomain bucket (www.example.com). If someone enters www.example.com in their browser, they are redirected to example.com and see the content that is hosted in the Amazon S3 bucket with that name.

To create your buckets for website hosting

The following instructions provide an overview of how to create your buckets for website hosting. For detailed, step-by-step instructions on creating a bucket, see How Do I Create an S3 Bucket? in the Amazon Simple Storage Service Console User Guide.

Sign in to the AWS Management Console and open the Amazon S3 console at Create your root domain bucket:

  • Choose Create bucket.
  • Enter the Bucket name (for example, example.com).
  • Choose the Region where you want to create the bucket.
    Choose a Region close to you to minimize latency and costs, or to address regulatory requirements. The Region that you choose determines your Amazon S3 website endpoint. For more information, see Website endpoints.
  • To accept the default settings and create the bucket, choose Create.

Create your subdomain bucket:

  • Choose Create bucket.
  • Enter the Bucket name (for example, www.example.com).
  • Choose the Region where you want to create the bucket.
    Choose a Region close to you to minimize latency and costs, or to address regulatory requirements. The Region that you choose determines your Amazon S3 website endpoint. For more information, see Website endpoints.
  • To accept the default settings and create the bucket, choose Create.

In the next step, you configure example.com for website hosting.

Step 3: Configure your root domain bucket for website hosting

In this step, you configure your root domain bucket (example.com) as a website. This bucket will contain your website content. When you configure a bucket for website hosting, you can access the website using the Website endpoints.

To enable static website hosting

  • Sign in to the AWS Management Console and open the Amazon S3 console at 
  • In the Bucket name list, choose the bucket that you want to use for your static website.
  • Choose Properties.
  • Choose Static website hosting.
  • Choose Use this bucket to host a website.
  • Enter the name of your index document.
    The index document name is 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. For more information, see Configuring an index document.
  • (Optional) If you want to add a custom error document, in the Error document box, enter the key name for the error document (for example, error.html).
    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. For more information, see (Optional) configuring a custom error document.
  • (Optional) If you want to specify advanced redirection rules, in Edit redirection rules, use XML to describe the rules.
    For more information, see Configuring advanced conditional redirects.
  • Under Static website hosting, note the Endpoint.
    The Endpoint is the Amazon S3 website endpoint for your bucket. After you finish configuring your bucket as a static website, you can use this endpoint to test your website.
  • Choose Save.

In the next step, you configure your subdomain (www.example.com) to redirect requests to your domain (example.com).

Step 4: Configure your subdomain bucket for website redirect

After you configure your root domain bucket for website hosting, you can configure your subdomain bucket to redirect all requests to the domain. In this example, all requests for www.example.com are redirected to example.com.

To configure a redirect request

  • On the Amazon S3 console, in the Buckets list, choose your subdomain bucket ( www.example.com in this example).
  • Choose Properties.
  • Choose Static website hosting.
  • Choose Redirect requests.
  • In the Target bucket or domain box, enter your domain (for example, example.com).
  • In the Protocol box, enter http.
  • Choose Save.

Step 5: Configure logging for website traffic

If you want to track the number of visitors accessing your website, you can optionally enable logging for your root domain bucket. For more information, see Amazon S3 Server Access Logging. If you plan to use Amazon CloudFront to speed up your website, you can also use CloudFront logging.

To enable server access logging for your root domain bucket

  • Open the Amazon S3 consoleIn the same Region where you created the bucket that is configured as a static website, create a bucket for logging, for example logs.example.com.
  • Create a folder for the server access logging log files (for example, logs).
  • (Optional) If you want to use CloudFront to improve your website performance, create a folder for the CloudFront log files (for example, cdn).
  • In the Bucket list, choose your root domain bucket.
  • Choose Properties.
  • Choose Server access logging.
  • Choose Enable logging.
  • For Target bucket, choose the bucket that you created for the log files, for example logs.example.com.
  • For Target prefix, enter the name of the folder that you created for the log files followed by the delimiter (/), for example logs/.
    When you set the Target prefix, you group your log data files in a folder so that they are easy to locate.
  • Choose Save.
    In your log bucket, you can now access your logs. Amazon S3 writes website access logs to your log bucket every 2 hours.
  • To view the logs, choose Overview, and choose the folder.

Step 6: Upload index and website content

In this step, you upload your index document and optional website content to your root domain bucket.

When you enable static website hosting for your bucket, you enter the name of the index document (for example, index.html). After you enable static website hosting for the bucket, you upload an HTML file with this index document name to your bucket.

AWS Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

To configure the index document

  • Create an index.html file.
    If you don’t have an index.html file, you can use the following HTML to create one:
  • <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>
  1. 1. Save the index file locally with the exact index document name that you entered when you enabled static website hosting for your bucket (for example, index.html).
  2. 2. The index document file name must exactly match the index document name that you enter in the Static website hosting dialog box. The index document name is case sensitive. For example, if you enter index.html for the Index document name in the Static website hosting dialog box, your index document file name must also be index.html and not Index.html.
  3. 3. Sign in to the AWS Management Console and open the Amazon S3 console at In the Buckets list, choose the name of the bucket that you want to use to host a static website.
  4. 4. Enable static website hosting for your bucket, and enter the exact name of your index document (for example, index.html). For more information, see Enabling website hosting.After enabling static website hosting, proceed to step 6.
  5. 5. To upload the index document to your bucket, do one of the following:
  • Drag and drop the index file into the console bucket listing.
  • Choose Upload, and follow the prompts to choose and upload the index file.
  • For step-by-step instructions, see How Do I Upload Files and Folders to an Amazon S3 Bucket? in the Amazon Simple Storage Service Console User Guide.
  • (Optional) Upload other website content to your bucket.

Conclusion

By hosting your site on the EC2 Amazon Web Service server (AWS), you join the ever growing list of enterprises and derive a multitude of benefits at virtually no extra cost. This exercise will keep your enterprise in good stead and help it compete against the established players. So, should you want to design, develop and host your site on the AWS cloud server at cost effective rates, contact a professional and experienced website development company.

Are you looking training with Right Jobs?

Contact Us

Popular Courses