Deploying a Static Website Using S3 and CloudFront

Introduction

Hosting a static website on AWS is a cost-effective and scalable solution. In this article, we’ll cover three different ways to deploy a static website using S3 and CloudFront, including step-by-step instructions for:

AWS Con…


This content originally appeared on DEV Community and was authored by Kachi

Introduction

Hosting a static website on AWS is a cost-effective and scalable solution. In this article, we’ll cover three different ways to deploy a static website using S3 and CloudFront, including step-by-step instructions for:

  1. AWS Console (Manual setup)
  2. AWS CLI (Command-line automation)
  3. Terraform (Infrastructure as Code)

We’ll also explore alternative methods for hosting an S3-based website without CloudFront, such as Route 53, ACM, and API Gateway.

Architectural Diagram

Image description

Method 1: AWS Console (Manual Setup)

This method is ideal for beginners who want to quickly deploy a static site.

Steps:

  1. Create an S3 Bucket

    • Go to AWS S3 Console → Click Create Bucket
    • Enter a unique name (e.g., my-static-site)
    • Uncheck Block all public access
    • Enable Static website hosting
  2. Upload Website Files

    • Upload your index.html and other static files.
  3. Set Permissions

    • Update the bucket policy to allow public read access.
  4. Create a CloudFront Distribution

    • Set Origin as your S3 Bucket
    • Enable OAI (Origin Access Identity) for security
    • Configure caching and SSL settings
  5. Copy CloudFront Distribution URL

    • This is your website’s final HTTPS-enabled URL.

Method 2: AWS CLI (Command-Line Automation)

For those who prefer automation, the AWS CLI simplifies deployment.

Steps:

  1. Create an S3 Bucket
   aws s3 mb s3://my-static-site
  1. Upload Website Files
   aws s3 cp index.html s3://my-static-site --acl public-read
  1. Enable Static Website Hosting
   aws s3 website s3://my-static-site --index-document index.html
  1. Create a CloudFront Distribution
   aws cloudfront create-distribution --origin-domain-name my-static-site.s3.amazonaws.com
  1. Copy the CloudFront URL
    • This is your new secure website URL.

Method 3: Terraform (Infrastructure as Code)

Using Terraform, you can define your website’s infrastructure in code and deploy it instantly.

Terraform Configuration (main.tf)

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "static_site" {
  bucket = "my-static-site"
}

resource "aws_s3_bucket_website_configuration" "website" {
  bucket = aws_s3_bucket.static_site.id
  index_document {
    suffix = "index.html"
  }
}

resource "aws_cloudfront_distribution" "cdn" {
  origin {
    domain_name = aws_s3_bucket.static_site.bucket_domain_name
    origin_id   = "S3Origin"
  }
}

Deploy with Terraform

terraform init
terraform apply -auto-approve

Get the Website URL

  • Terraform will output the CloudFront distribution URL.

Alternative Ways to Host an S3 Website Without CloudFront

If you don’t want to use CloudFront, here are three other ways to host a static website with S3:

1. S3 Static Website Hosting (Public Access - No CloudFront)

This is the simplest way, but it does not support HTTPS.

Steps:

  1. Enable Static Website Hosting in the S3 Bucket Properties.
  2. Upload website files (index.html, style.css, etc.).
  3. Update Bucket Policy to allow public access:
   {
     "Version": "2012-10-17",
     "Statement": [
       {
         "Effect": "Allow",
         "Principal": "*",
         "Action": "s3:GetObject",
         "Resource": "arn:aws:s3:::my-static-site/*"
       }
     ]
   }
  1. Copy the S3 Website Endpoint URL.

Pros: Simple, fast setup.
Cons: No HTTPS (only HTTP), not recommended for production.

2. S3 + Route 53 + ACM (Custom Domain with HTTPS - No CloudFront)

This method uses Route 53 and AWS Certificate Manager (ACM) to enable HTTPS without CloudFront.

Steps:

  1. Create an S3 Bucket named after your domain (e.g., example.com).
  2. Enable Static Website Hosting and upload files.
  3. Request an SSL Certificate from AWS Certificate Manager (ACM).
  4. Create an Alias Record in Route 53 pointing to the S3 Website Endpoint.
  5. Verify the SSL Certificate and use it with your domain.

Pros: Supports HTTPS and custom domains.
Cons: More setup required, still no CDN for caching.

3. S3 + API Gateway (Secure Static Hosting with HTTPS & Caching)

This method uses Amazon API Gateway to serve content from S3 with HTTPS support.

Steps:

  1. Create an S3 Bucket, upload website files.
  2. Create an API Gateway REST API integrating with S3.
  3. Enable CORS and CloudWatch logging.
  4. Deploy the API and get the endpoint URL.
  5. Map a custom domain (Optional) using Route 53.

Pros: Supports HTTPS, better security, throttling & caching.
Cons: More complex, might have additional costs.

Conclusion

AWS provides multiple ways to host static websites on S3. Using CloudFront ensures faster performance, security, and HTTPS support, but you can also use Route 53 + ACM or API Gateway for different use cases.

Comparison Table

Image description

For production websites, S3 + CloudFront remains the best choice.

Let me know if you have any questions! 🚀


This content originally appeared on DEV Community and was authored by Kachi


Print Share Comment Cite Upload Translate Updates
APA

Kachi | Sciencx (2025-02-23T16:43:23+00:00) Deploying a Static Website Using S3 and CloudFront. Retrieved from https://www.scien.cx/2025/02/23/deploying-a-static-website-using-s3-and-cloudfront/

MLA
" » Deploying a Static Website Using S3 and CloudFront." Kachi | Sciencx - Sunday February 23, 2025, https://www.scien.cx/2025/02/23/deploying-a-static-website-using-s3-and-cloudfront/
HARVARD
Kachi | Sciencx Sunday February 23, 2025 » Deploying a Static Website Using S3 and CloudFront., viewed ,<https://www.scien.cx/2025/02/23/deploying-a-static-website-using-s3-and-cloudfront/>
VANCOUVER
Kachi | Sciencx - » Deploying a Static Website Using S3 and CloudFront. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/23/deploying-a-static-website-using-s3-and-cloudfront/
CHICAGO
" » Deploying a Static Website Using S3 and CloudFront." Kachi | Sciencx - Accessed . https://www.scien.cx/2025/02/23/deploying-a-static-website-using-s3-and-cloudfront/
IEEE
" » Deploying a Static Website Using S3 and CloudFront." Kachi | Sciencx [Online]. Available: https://www.scien.cx/2025/02/23/deploying-a-static-website-using-s3-and-cloudfront/. [Accessed: ]
rf:citation
» Deploying a Static Website Using S3 and CloudFront | Kachi | Sciencx | https://www.scien.cx/2025/02/23/deploying-a-static-website-using-s3-and-cloudfront/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.