Host Your Static Website Globally with Amazon S3 and CloudFront
Deploying a modern frontend application (built with React, Vue, Next.js, or just plain HTML/CSS) shouldn't require maintaining a complex server. While you can host files directly out of an object storage bucket, exposing a storage bucket directly to the public internet poses security risks and lacks optimization.
Why Use S3 + CloudFront Instead of Just S3?
• Enhanced Security: By keeping your S3 bucket strictly private and using Origin Access Control (OAC), users can only access your files through the secure CloudFront URL.
• Global Speed: CloudFront caches your website at edge locations worldwide, ensuring blazing-fast loading speeds for users regardless of their location.
• Cost Efficiency: S3 storage is incredibly cheap, and CloudFront offers a massive free tier for data transfer, making this setup virtually free for low-to-medium traffic sites.
Step 1: Create a Private Amazon S3 Bucket
First, we need a secure storage space to hold our website's static assets (HTML, CSS, JS, images).
• Enhanced Security: By keeping your S3 bucket strictly private and using Origin Access Control (OAC), users can only access your files through the secure CloudFront URL.
• Global Speed: CloudFront caches your website at edge locations worldwide, ensuring blazing-fast loading speeds for users regardless of their location.
• Cost Efficiency: S3 storage is incredibly cheap, and CloudFront offers a massive free tier for data transfer, making this setup virtually free for low-to-medium traffic sites.
Step 1: Create a Private Amazon S3 Bucket
First, we need a secure storage space to hold our website's static assets (HTML, CSS, JS, images).
- Log into the AWS Management Console and navigate to S3.
- Click Create bucket.
- Configure the settings:
• Bucket name: Enter a globally unique name (e.g., my-awesome-portfolio-2026).
• Object Ownership: Leave as ACLs disabled.
• Block Public Access settings: Keep the checkbox checked for "Block all public access". We want this bucket to remain completely private. - Click Create bucket.
- Click into your new bucket, click Upload, and upload a simple index.html file containing some basic HTML code
Step 2: Set Up Amazon CloudFront Distribution
Next, we will set up the Content Delivery Network (CDN) to fetch files safely from our private bucket and serve them to the world. - Navigate to CloudFront in the AWS Console and click Create distribution.
- Under Origin:
• Origin domain: Select the S3 bucket you just created from the dropdown menu.
• Origin access: Choose Origin access control settings (recommended).
• Click Create control setting, leave the defaults, and click Create. This allows CloudFront to authenticate with your private S3 bucket. - Under Default cache behavior:
• Viewer protocol policy: Select Redirect HTTP to HTTPS for modern security. - Under Settings:
• Default root object: Type index.html. - Scroll to the bottom and click Create distribution.
Step 3: Update the S3 Bucket Policy
Once the distribution is created, AWS will display a prominent banner at the top of your screen telling you to update your S3 bucket policy. - Copy the policy provided in the banner (or click Copy policy).
- Go back to your S3 Bucket, select the Permissions tab, and scroll down to Bucket policy.
- Click Edit, paste the copied JSON block into the editor, and click Save changes.
This explicit policy grants CloudFront—and only CloudFront—permission to read the files inside your private bucket.
Step 4: Test Your Live Website - Return to your CloudFront Distribution.
- Look for the Distribution domain name (e.g., d123456abcdef.cloudfront.net).
- Copy the domain name and paste it into a new browser tab.
Your Expected Output:
You should see the contents of your index.html file render instantly over a secure HTTPS connection.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article