How to Host a Static Site in the Cloud in Four Steps

Static websites are back in the spotlight as a modern architecture for the web. According to the State of the Jamstack 2021 report, 32% of developers use the Jamstack (JavaScript, APIs, Markup) architecture for building websites, which relies heavily on static site generators and CDN hosting.

The benefits of static sites are compelling – by serving pre-built HTML, CSS and JavaScript files directly from a global content delivery network (CDN), static sites are:

  • 🚀 Ultra fast: Average page loads under 1 second
  • ⚡️ Infinitely scalable: Handle millions of concurrent users
  • 🔒 Extremely secure: No server-side vulnerabilities
  • 💸 Dramatically cheaper: No need for always-on servers
  • 😌 Simple to version and deploy

As a professional full-stack developer who has built dozens of large-scale websites, I‘ve become a huge proponent of the modern static site architecture. And when it comes to hosting static sites, nothing beats the power, reliability and affordability of the cloud.

In this guide, I‘ll share my battle-tested process for deploying a blazing fast static website on Amazon Web Services (AWS), the world‘s most comprehensive and broadly adopted cloud platform. We‘ll cover everything you need to go from zero to a production-ready static site in just four steps:

  1. Set up an AWS account and configure permissions
  2. Create a static site and deploy it globally with Amplify
  3. Register a custom domain using Route 53
  4. Connect the custom domain to the Amplify app

Whether you‘re a grizzled cloud veteran or fresh-faced newbie, by the end of this in-depth tutorial, you‘ll be able to harness the awesome power of AWS to deploy your own static masterpiece. Let‘s get cracking!

Why Host a Static Site on AWS?

So you may be wondering – why bother with AWS for static site hosting? After all, aren‘t there simpler "drag and drop" static hosters out there like Netlify or Vercel?

It‘s true that GUI-based static hosting platforms abstract away the underlying cloud infrastructure, and this ease-of-use can be attractive for simple sites. However, the aws-one-click approach quickly starts to constrain you when you want to customize your deployment or add more advanced cloud capabilities to your app.

Here are some of the key reasons why I and thousands of other professional developers choose to host static sites on AWS:

  • 🌎 Unmatched global infrastructure. With static site hosting on AWS you get access to the largest cloud network in the world, including 300+ points of presence across 90+ countries. This enables you to deliver your static assets to users at lightning speed anywhere in the world.

  • 🛠 Extensive developer tooling and services. AWS offers 200+ powerful web services that give developers the building blocks to add almost any functionality imaginable to their apps. With tight integration between services and robust APIs, the sky‘s the limit for what you can build.

  • 💹 Flexible pricing. With AWS you pay only for the resources you actually use, at the same low prices that power Amazon‘s global business. For a static site, your monthly hosting bill could easily be less than a cup of coffee. The generous AWS Free Tier also lets you try out services without any cost.

  • 🔧 Control and customization. Instead of being constrained by the limitations of a particular static host, with AWS you can tweak and optimize every aspect of your deployment to meet your exact needs and scale to handle any traffic demands.

How to Host a Static Site on AWS

Ready to experience the power of static hosting on AWS for yourself? I‘ve honed this process over years of deploying production sites, so stick with me and you‘ll have your own site live in no time!

Step 1: Set Up an AWS Account

The first step is to create an AWS account if you don‘t already have one. Don‘t worry, signing up is quick and easy:

  1. Navigate to the AWS sign up page
  2. Enter your email and a strong password
  3. Provide your credit card and phone number for identity verification
  4. Choose the Basic Support Plan (free)

Note that while AWS does require a credit card to sign up, if you‘re just deploying a simple static site, your usage will likely fall under the free tier, so you won‘t be charged. The AWS Free Tier includes:

  • 5 GB of standard storage in S3
  • 15 GB of data transfer out per month
  • 1 million requests per month

Pretty sweet deal for your first year!

Once your account is created, it‘s a good idea to secure it by setting up multi-factor authentication (MFA). I highly recommend using a hardware security key like a YubiKey for the strongest protection.

You‘ll also want to create a new user for yourself and grant it admin permissions. This follows the principle of least privilege and prevents you from accidentally breaking things with the god-like root user access.

To create a new admin user:

  1. In the AWS Console, navigate to the IAM service
  2. Click "Users" and then "Add User"
  3. Enter a name like "admin-yourusername" and select Programmatic access
  4. On the permissions page, select "Attach existing policies directly" and choose the AdministratorAccess policy
  5. Skip the tags and review the user details
  6. Click "Create user" and save the access keys in a secure place

You now have a locked-down AWS account ready for static site deployment!

Step 2: Create Your Static Site and Deploy It With Amplify

With your AWS environment squared away, you‘re ready to create a static site and unleash it on the web with AWS Amplify.

Amplify is an opinionated set of tools and services from AWS that allows developers to quickly ship full-stack web and mobile apps. The Amplify framework provides an easy, interactive way to add powerful AWS services to your app, while the Amplify Console offers Git-based continuous deployment and hosting.

For our static site, we‘ll use the Amplify Console to quickly provision the necessary AWS resources and deploy the site globally via Amazon‘s CloudFront CDN. But first, we need a static site to deploy!

Let‘s generate a simple static page:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Awesome AWS Static Site</title>
</head>
<body>
    <main>

        <p>Isn‘t static hosting in the cloud amazing?</p>
        <p>⚡️ Lightning fast performance</p>
        <p>🌎 Global distribution and scaling</p>
        <p>🔒 Secure by default</p>
        <p>💸 Pay only for what you use</p>
    </main>
</body>
</html>

Save this file as index.html. You can of course make your site as sophisticated as you want, the key is that everything must be contained in static files.

With your static site ready, open the Amplify Console and click "Get started" under Deploy. Choose Deploy without Git provider for now (we‘ll set up a Git workflow later for real projects).

Give your app and environment a name, then drag and drop your index.html and any other static files into the file uploader. Click "Save and deploy".

The Amplify Console will now spin up the necessary resources (S3 bucket, CloudFront distribution) and deploy your files. After a minute or so, you‘ll see a unique amplifyapp.com subdomain where your static site is live!

https://main.d3s0meun1qu3id.amplifyapp.com

Awesome, you‘ve just deployed your static site globally on AWS with just a few clicks. The Amplify Console automatically optimizes your assets and serves them from Amazon CloudFront edge servers in 300+ cities for lightning fast loads anywhere.

Step 3: Register a Custom Domain with Route 53

We‘re off to a great start, but one downside of the Amplify URL is that it‘s, well, not very memorable. Not to worry though, we can easily add a custom domain using Route 53, the highly available and scalable DNS service from AWS.

First, you‘ll need to register (purchase) your desired domain through Route 53. Head to the Route 53 dashboard and enter your domain name. Most top-level domains (TLDs) like .com, .io, .co and .dev are available with transparent, upfront pricing.

For a .com, expect to pay around $12/year (as of writing). Complete the registration checkout, inputting your contact info. It may take a few minutes to process the domain purchase.

With your domain secured, the final step is to point it to your Amplify app so the world can bask in your static magnificence!

Step 4: Connect Your Custom Domain to Your Amplify App

Connecting your custom domain to Amplify is a breeze:

  1. In the Amplify Console, select your app and click on "Domain Management"
  2. Click "Add domain" and enter the domain name you just registered in Route 53
  3. Amplify will automatically provision an SSL/TLS certificate for your domain to enable HTTPS
  4. After a few minutes of provisioning, you‘ll see your domain in the "Available Domains" list, both as the apex domain (yourdomain.com) and as a www subdomain (www.yourdomain.com)

That‘s all there is to it! You can now access your static site at your very own domain over a secure HTTPS connection. We‘ve taken a humble index.html file and transformed it into an enterprise-grade website powered by the battle-tested AWS cloud. Pretty slick.

Advanced Tips for Real-World Static Sites

Alright, you now have a solid foundation for static hosting on AWS. But what about real-world static sites and advanced usage? Don‘t worry, I‘ve got you covered.

Automate Deployments with Git and CI/CD

As much as I love the simplicity of dragging and dropping, for any serious static project you‘re going to want to automate your deployments with a Git-based workflow. With Git, your static site code will be version controlled, collaborative, and easy to roll back if needed.

The Amplify Console makes setting up continuous deployment from Git almost too easy:

  1. In Amplify, select "Get started" under Deploy, then choose your Git provider (GitHub, Bitbucket, GitLab, AWS CodeCommit)
  2. Authenticate Amplify and choose your static site repository and branch
  3. Tweak the build settings like the output directory and any custom build commands
  4. Click "Save and deploy"!

Now every time you push an update to your main branch, Amplify will automatically build and deploy your changes. PR previews and atomic deploys are also supported out of the box, making Amplify a surprisingly powerful CI/CD platform.

Measure and Improve Core Web Vitals

In the age of mobile-first indexing and laser focus on user experience, page speed and performance are mission critical for any web project. Google‘s Core Web Vitals (CWV) provide a data-driven framework for measuring and optimizing your site‘s user experience.

Static sites deployed via AWS Amplify tend to ace the CWV assessment due to the inherent speed of the edge-first architecture. In fact, the default Amplify deployment already implements many web performance best practices like:

  • Global CDN caching of static assets (HTML, CSS, JS, images)
  • Asset minification
  • Compression with Gzip or Brotli
  • HTTP/2 with server push

Still, it‘s important to regularly test your site against the Core Web Vitals using tools like Google Lighthouse and Chrome User Experience Report (CrUX). Amplify even includes CWV suggestions and vitals data right in the Console.

Based on the CWV assessment, some additional optimizations you can make to your static site include:

  • Optimize images by compressing and converting to next-gen formats (WebP, AVIF)
  • Eliminate render-blocking resources by deferring non-critical JS/CSS
  • Minimize main thread work by reducing JS execution time
  • Avoid large layout shifts by specifying width/height on images and embeds

With a combination of the right static architecture (AWS) and CWV optimizations, it‘s entirely possible to achieve 90+ Lighthouse scores and instant 1s page loads. Pretty rad.

Enhance with Dynamic Functionality

I know, I know. We‘ve been hyping up static sites for this entire guide. But I‘ll let you in on a little secret – your static site doesn‘t have to be 100% static!

Thanks to the power of JavaScript and APIs, you can add all sorts of dynamic functionality to your static site while still reaping the speed, security and simplicity of the Jamstack. Here are just a few examples of dynamic features you can add to your Amplify static site:

  • Client-side forms and email capture with no backend (see Formspree, Getform)
  • Payments and checkout with Stripe Checkout
  • Search with Algolia or AWS OpenSearch
  • Commenting with Disqus or Hyvor
  • User authentication with Amazon Cognito or Auth0
  • Webhooks and automation with AWS Lambda functions

All of these services can be integrated directly into your frontend code and talk to third-party APIs, no backend necessary. And if you do find yourself needing a backend for more complex or custom logic, you can always add an API or serverless functions to your app using – you guessed it – Amplify.

The beauty of using AWS for your static site is that you get easy access to the entire AWS ecosystem to quickly add and iterate on new functionality. Imagine adding personalization, A/B testing, AI/ML and more to your static site with just a few CLI commands – that‘s the power of the AWS cloud.

Conclusion and developer notes:

Phew, we covered a lot of ground in this guide! To recap, we walked through the four key steps to hosting a high-performance static site on AWS:

  1. Setting up an AWS account and securing it with a new IAM admin user
  2. Generating a simple index.html and deploying it globally with the Amplify Console
  3. Registering a custom domain with Route 53
  4. Connecting the custom domain to the Amplify hosted app

We then explored some real-world tips for static hosting like automating deployments with Git, monitoring Core Web Vitals, and enhancing static sites with dynamic functionality.

The combination of static architecture and cloud infrastructure is a powerful paradigm for modern web development. By using AWS Amplify to abstract the configuration and deployment complexity, any developer can quickly ship a sleek, performant static site that is also easily extendable.

From a technical standpoint, here are some of the key AWS services being used under the hood for static hosting with Amplify:

  • Amazon S3 for storage and hosting of static files
  • Amazon CloudFront for global content delivery and caching
  • AWS Certificate Manager for free SSL/TLS certificates
  • AWS CodeBuild for building and testing code
  • AWS CodePipeline for continuous integration and deployment
  • AWS IAM for securing and controlling access to services

Of course, you can dive much deeper into the AWS ecosystem and use additional services to power your static site. Some ideas to explore:

  • Use AWS CloudFormation to define your static site infrastructure as code
  • Implement a custom build process with AWS CodeBuild buildspec
  • Add backend API services like AWS AppSync or API Gateway
  • Process form submissions or webhooks with AWS Lambda functions
  • Manage users and authentication with Amazon Cognito
  • Query data from a database like Amazon DynamoDB

Static hosting on AWS really is a choose-your-own-adventure experience. You can start simple with a single HTML file, then evolve to a full-fledged serverless application over time, all on top of the same battle-tested cloud infrastructure that powers Amazon.com.

I hope this guide has armed you with the context and step-by-step details to deploy your own static site on AWS with confidence. Remember, experimentation is key – the more you build and ship, the more you‘ll learn about harnessing the power of the cloud.

Now stop reading and go build something awesome! Happy coding 🚀

Similar Posts