Host a static website locally using Simple Storage Service (S3) and Terraform with LocalStack
Introduction
Section titled “Introduction”AWS Simple Storage Service (S3) is a proprietary object storage solution that can store an unlimited number of objects for many use cases. S3 is a highly scalable, durable and reliable service that we can use for any use case involving file-based storage: hosting a static site, handling big data analytics, managing application logs, storing web assets and much more!
With S3, objects are stored in buckets. A bucket is effectively a directory, while an object is a file. Every object (file) stores the name of the file (key), the contents (value), a version ID and the associated metadata. You can also use S3 to host to server static content as a static website. The static content might include HTML, CSS, JavaScript, images, and other assets that make up your website.
LocalStack supports the S3 API, which means you can use the same API calls to interact with S3 in LocalStack as you would with AWS. Using LocalStack, you can create and manage S3 buckets and objects locally, use AWS SDKs and third-party integrations to work with S3, and test your applications without making any significant alterations. LocalStack also supports the creation of S3 buckets with static website hosting enabled.
In this tutorial, we will deploy a static website using an S3 bucket over a locally emulated AWS infrastructure on LocalStack.
We will use Terraform to automate the creation & management of AWS resources by declaring them in the HashiCorp Configuration Language (HCL).
We will also learn about tflocal, a CLI wrapper created by LocalStack, that allows you to run Terraform locally against LocalStack.
Prerequisites
Section titled “Prerequisites”For this tutorial, you will need:
Architecture
Section titled “Architecture”The following diagram illustrates the architecture of the static website hosting setup using S3 and Terraform:

In this architecture:
- A browser makes an HTTP request to the S3 website endpoint
- LocalStack’s S3 service serves the static content from the configured bucket
- The bucket contains HTML files and optional assets
- Terraform provisions and configures all resources locally
Creating a static website
Section titled “Creating a static website”We will create a simple static website using plain HTML to get started.
To create a static website deployed over S3, we need to create an index document and a custom error document.
We will name our index document index.html and our error document error.html.
Optionally, you can create a folder called assets to store images and other assets.
Let’s create a directory named s3-static-website-localstack where we’ll store our static website files.
If you don’t have an index.html file, you can use the following code to create one:
<!DOCTYPE html><html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html" /> <meta charset="utf-8" /> <title>Static Website</title> </head> <body> <p>Static Website deployed locally over S3 using LocalStack</p> </body></html>