DEV Community

AzeematRaji
AzeematRaji

Posted on

How to Host a Static Website on AWS S3 with CloudFront CDN

Objective:

To host a static website on AWS S3 and optimize its performance and security using CloudFront as a Content Delivery Network (CDN), ensuring fast and secure global content delivery.

Prerequisite:

  • Active AWS account for setting up services.
  • Pre-built static website content (e.g., HTML, CSS, JavaScript files).

Step 1: Create an S3 Bucket

  • Log in to the AWS Management Console and navigate to the S3 Service.
  • Create a bucket with a globally unique name.
  • Enable public access to allow your website to be publicly accessible.

s3-bucket

Step 2: Upload files and folder to the bucket

  • Click upload on the newly created bucket
  • Upload .html file as a file, then upload others as folder

upload files & folders

Step 3: Configure the bucket to allow static web hosting

  • Click on properties tab in the bucket, Enable Static Website Hosting and edit
  • Choose "Host a static website" as the hosting option.
  • Specify Index and Error Documents
  • Enter the name of your main file (e.g index.html) in the Index Document field.
  • Specify an error file (e.g., 404.html) for handling invalid URLs. (Optional)
  • After saving, copy the Website Endpoint, the URL will be address for your website.

configure bucket

Step 4: create IAM bucket policy that makes the bucket contents publicly accessible.

  • Click on permission in the bucket
  • Edit the bucket policy with this
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::your-bucket-name/*"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

bucket policy

  • Save and open the website endpoint URL from Step 3 in a browser to ensure the website is accessible.

website

Step 5: Set Up a CloudFront Distribution

  • Navigate to cloudfront service, create a distribution
  • Configure the settings; add origin domain, enter index.html into default root object

cloudfront

Step 6: Copy the domain URL and check it on the browser to confirm access to your website

website

Conclusion

Benefits of Using CloudFront:

  • Speeds up content delivery with edge locations.
  • Provides an SSL certificate for secure connections.
  • Offers caching to reduce latency and S3 bucket costs.

Top comments (0)