How to Setup a Static Website Using a Custom Domain in AWS Guide

You may stuck at Host a Static Website in Amazon Web Services(AWS). Here is a brief description to evaluate.
Suppose you want to host your static website on Amazon Simple Storage Service (Amazon S3). You have registered a domain, for techprozetta, techprozetta.com, and you want requests for http://www.techprozetta.com and http://techprozetta.com to be served from your Amazon S3 content. Whether you have an existing static website that you want to host on Amazon S3 or are starting from scratch, use this techprozetta to learn how to host websites on Amazon S3.

Before you Begin:
As you walk through the steps in this techprozetta, you work with the following services:

Domain registrar of your choice If you do not already have a registered domain name, such as techprozetta.com, you need to create and register one with a registrar of your choice. You can typically register a domain for a small yearly fee. For procedural information about registering a domain name, see the registrar's website.

Amazon S3 You use Amazon S3 to create buckets, upload a sample website page, configure permissions so everyone can see the content, and then configure the buckets for website hosting. In this techprozetta, because you want to allow requests for both http://www.techprozetta.com and http://techprozetta.com, you create two buckets; however, you host content in only one bucket. You configure the other Amazon S3 bucket to redirect requests to the bucket that hosts the content.

Amazon Route 53 You configure Amazon Route 53 as your DNS provider. You create a hosted zone in Amazon Route 53 for your domain and configure applicable DNS records. If you are switching from an existing DNS provider, you need to ensure that you have transferred all of the DNS records for your domain.

We recommend that you have basic familiarity with domains, Domain Name System (DNS), CNAME records, and A records. A detailed explanation of these concepts is beyond the scope of this guide. Your domain registrar should provide any basic information that you need.

In this techprozetta, we use Amazon Route 53, however, you can use most registrars to define a CNAME record that points to an Amazon S3 bucket.

Note
All the steps in this techprozetta use techprozetta.com as a domain name. You will need to replace this domain name with the one you registered.
Step 1: Register a Domain

If you already have a registered domain, you can skip this step. If you are new to hosting a website, your first step is to register a domain, such as techprozetta.com, with a registrar of your choice.  

After you have chosen a registrar, you will register your domain name according to the instructions at the registrar’s website. For a list of registrar websites that you can use to register your domain name, see Information for Registrars and Registrants at the ICANN.org website.

When you have a registered domain name, your next task is to create and configure Amazon S3 buckets for website hosting and to upload your website content.

Step 2: Create and Configure Buckets and Upload Data

To support requests from both the root domain such as techprozetta.com and subdomain such as www.techprozetta.com, you create two buckets. One bucket contains the content. You configure the other bucket to redirect requests.

Step 2.1: Create Two Buckets

The bucket names must match the names of the website that you are hosting. For techprozetta, to host your techprozetta.com website on Amazon S3, you would create a bucket named techprozetta.com. To host a website under www.techprozetta.com, you would name the bucket www.techprozetta.com. In this techprozetta, your website will support requests from both techprozetta.com and www.techprozetta.com.

In this step, you sign in to the Amazon S3 console with your AWS account credentials and create the following two buckets.

techprozetta.com
www.techprozetta.com
Note
Like domains, subdomains must have their own S3 buckets, and the buckets must share the exact names as the subdomains. In this techprozetta, we are creating the www.techprozetta.com subdomain, so we also need an S3 bucket named www.techprozetta.com.
To create your buckets and upload your website content for hosting

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

Create two buckets that match your domain name and subdomain. For instance, techprozetta.com and www.techprozetta.com.

For step-by-step instructions, see How Do I Create an S3 Bucket? in the Amazon Simple Storage Service Console User Guide.

Upload your website data to the techprozetta.com bucket.

You will host your content out of the root domain bucket (techprozetta.com), and you will redirect requests for www.techprozetta.com to the root domain bucket. You can store content in either bucket. For this techprozetta, you will host content in the techprozetta.com bucket. The content can be text files, family photos, videos—whatever you want. If you have not yet created a website, then you only need one file for this techprozetta. You can upload any file. For techprozetta, you can create a file using the following HTML and upload it the bucket. The file name of the home page of a website is typically index.html, but you can give it any name. In a later step, you provide this file name as the index document name for your website.

Copy
<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>
For step-by-step instructions, see How Do I Upload an Object to an S3 Bucket? in the Amazon Simple Storage Service Console User Guide.

Configure permissions for your objects to make them publicly accessible.

Attach the following bucket policy to the techprozetta.com bucket substituting the name of your bucket for techprozetta.com. For step-by-step instructions to attach a bucket policy, see How Do I Add an S3 Bucket Policy? in the Amazon Simple Storage Service Console User Guide.

{
  "Version":"2012-10-17",
  "Statement":[{
          "Sid":"PublicReadGetObject",
        "Effect":"Allow",
            "Principal": "*",
      "Action":["s3:GetObject"],
      "Resource":["arn:aws:s3:::techprozetta.com/*"
      ]
    }
  ]
}
You now have two buckets, techprozetta.com and www.techprozetta.com, and you have uploaded your website content to the techprozetta.com bucket. In the next step, you will configure www.techprozetta.com to redirect requests to your techprozetta.com bucket. By redirecting requests you can maintain only one copy of your website content. Both visitors who type www in their browsers and those who specify only the root domain are routed to the same website content in your techprozetta.com bucket. 

Step 2.2: Configure Buckets for Website Hosting

When you configure a bucket for website hosting, you can access the website using the Amazon S3 assigned bucket website endpoint.

In this step, you configure both buckets for website hosting. First, you configure techprozetta.com as a website and then you configure www.techprozetta.com to redirect all requests to the techprozetta.com bucket.

To configure your buckets for website hosting

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

In the Bucket name list, choose the name of the bucket that you want to enable static website hosting for.

Choose Properties.

Choose Static website hosting.

Configure the techprozetta.com bucket for website hosting. In the Index Document box, type the name that you gave your index page.


Choose Save.

Step 2.3: Configure Your Website Redirect

Now that you have configured your bucket for website hosting, configure the www.techprozetta.com bucket to redirect all requests for www.techprozetta.com to techprozetta.com.

To redirect requests from www.techprozetta.com to techprozetta.com

In the Amazon S3 console, in the Buckets list, choose your bucket ( www.techprozetta.com, in this techprozetta).

Choose Properties.

Choose Static website hosting.

Choose Redirect requests. In the Target bucket or domain box, type techprozetta.com.


Step 2.4: Configure Logging for Website Traffic

Optionally, you can configure logging to track the number of visitors accessing your website. To do that, you enable logging for the root domain bucket. For more information, see (Optional) Configuring Web Traffic Logging.

Step 2.5: Test Your Endpoint and Redirect

To test the website, type the URL of the endpoint in your browser. Your request is redirected, and the browser displays the index document for techprozetta.com.

You have now successfully redirected requests from one bucket to another, and the following Amazon S3 bucket website endpoints are accessible to any Internet user:

techprozetta.com.s3-website-us-east-1.amazonaws.com
http://www.techprozetta.com.s3-website-us-east-1.amazonaws.com
Perform additional configuration tasks to serve requests from the domain you registered in the preceding step. For techprozetta, if you registered a domain techprozetta.com, you want to serve requests from the following URLs :

In the next step, we use Amazon Route 53 to enable customers to use all of the URLs to navigate to your site.

Step 3: Create and Configure Amazon Route 53 Hosted Zone

Configure Amazon Route 53 as your Domain Name System (DNS) provider. If you want to serve content from your root domain, such as techprozetta.com, you must use Amazon Route 53. You create a hosted zone, which holds the DNS records associated with your domain:

An alias record that maps the domain techprozetta.com to the techprozetta.com bucket. This is the bucket that you configured as a website endpoint in step 2.2.
Another alias record that maps the subdomain www.techprozettae.com to the www.techprozetta.com bucket. You configured this bucket to redirect requests to the techprozetta.com bucket in step 2.2.

Step 3.1: Create a Hosted Zone for Your Domain
Go to the Amazon Route 53 console at https://console.aws.amazon.com/route53 and then create a hosted zone for your domain. For instructions, go to Creating a Hosted Zone in the http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/.


The following techprozetta shows the hosted zone created for the techprozetta.com domain. Write down the Amazon Route 53 name servers (NS) for this domain. You will need them later.

Step 3.2: Add Alias Records for techprozetta.com and www.techprozettae.com

The alias records that you add to the hosted zone for your domain maps example.com and www.example.com to the corresponding S3 buckets. Instead of using IP addresses, the alias records use the Amazon S3 website endpoints. Amazon Route 53 maintains a mapping between the alias records and the IP addresses where the S3 buckets reside.

For step-by-step instructions, see Creating Resource Record Sets by Using the Amazon Route 53 Console in the Amazon Route 53 Developer Guide.

The following screenshot shows the alias record for example.com as an illustration. You also need to create an alias record for www.techprozettae.com.

To enable this hosted zone, you must use Amazon Route 53 as the DNS server for your domain techprozetta.com. If you are moving an existing website to Amazon S3, you must transfer DNS records associated with your domain techprozetta.com to the hosted zone that you created in Amazon Route 53 for your domain first. If you are creating a new website, you can go directly to step 4.

Note
Creating, changing, and deleting resource record sets take time to propagate to the Route 53 DNS servers. Changes generally propagate to all Route 53 name servers in a couple of minutes. In rare circumstances, propagation can take up to 30 minutes.
Step 3.3: Transfer Other DNS Records from Your Current DNS Provider to Amazon Route 53

Before you switch to Amazon Route 53 as your DNS provider, you must transfer remaining DNS records, including MX records, CNAME records, and A records, from your DNS provider to Amazon Route 53. You don't need to transfer the following records:

NS records– Instead of transferring these, replace their values with the name server values that are provided by Amazon Route 53.
SOA record– Amazon Route 53 provides this record in the hosted zone with a default value.
Migrating required DNS records is a critical step to ensure the continued availability of all the existing services hosted under the domain name.

Step 4: Switch to Amazon Route 53 as Your DNS Provider

To switch to Amazon Route 53 as your DNS provider, contact your DNS provider and update the name server (NS) record to use the name servers in the delegation that you set in Amazon Route 53.

On your DNS provider's site, update the NS record with the delegation set values of the hosted zone as shown in the following Amazon Route 53 console screenshot. For more information, see Updating Your DNS Service's Name Server Records in Amazon Route 53 Developer Guide.

When the transfer to Amazon Route 53 is complete, verify that the name server for your domain has indeed changed. On a Linux computer, use the dig DNS lookup utility. For example, use this dig command:

Copy
dig +recurse +trace www.techprozetta.com any
It returns the following output (only partial output is shown). The output shows the same name servers on the Amazon Route 53 hosted zone that you created for the techprozetta.com domain.

Copy
...
example.com.      172800  IN      NS      ns-9999.awsdns-99.com.
example.com.      172800  IN      NS      ns-9999.awsdns-99.org.
example.com.      172800  IN      NS      ns-9999.awsdns-99.co.uk.
example.com.      172800  IN      NS      ns-9999.awsdns-99.net.

www.techprozetta.com.  300     IN      CNAME   www.techprozetta.com.s3-website-us-east-1.amazonaws.com.
...
Step 5: Testing

To verify that the website is working correctly, in your browser, try the following URLs:

http://techprozetta.com - Displays the index document in the techprozetta.com bucket.
In some cases, you might need to clear the cache of your web browser to see the expected behavior.

Comments