๐ŸŒ Static Website Hosting on AWS S3 Using Terraform ๐Ÿš€

Biswanath SahBiswanath Sah
3 min read

Are you ready to bring your static website online? ๐ŸŒ In this guide, I'll walk you through deploying a static website to AWS using Terraform - an awesome tool that lets you manage your infrastructure as code!

In this project, we'll deploy a static website to Amazon S3 using Terraform. We'll start by creating and configuring an S3 bucket for static website hosting. Then, we'll enable public access, set the hosting properties, and upload our website files like HTML, CSS, JavaScript, and images. Once everything is set up, AWS will provide a public URL for the website. This automated process makes hosting a static site simple, efficient, and fully managed through Terraformโ€™s code-based approach.

Let's dive in! ๐ŸŠโ€โ™‚๏ธ

๐Ÿ› ๏ธ Tools and Technologies Needed

  • Terraform: Think of it as your infrastructure assistant. ๐Ÿง‘โ€๐Ÿ’ป With Terraform, you can easily create, change, and version your infrastructure using simple configuration files.

    • Essential Commands:

      • terraform init: Prepares your workspace to run Terraform by setting up the environment. ๐Ÿ› ๏ธ

      • terraform plan: Gives you a sneak peek ๐Ÿ‘€ of the changes Terraform will make, so you can confirm before applying.

      • terraform apply: Executes your configuration and makes your infrastructure live. ๐ŸŽ‰

  • Amazon S3: Amazon S3 (Simple Storage Service) is a scalable, durable, and secure cloud storage solution from AWS (Amazon Web Services). It was introduced in March 2006 and has become one of the most popular object storage services worldwide.

    With flexible storage options, strong security features, and pay-as-you-go pricing, S3 is perfect for uses like data backup, static website hosting, and content distribution.

๐Ÿ“‹Prerequisites

  • Terraform is Installed

  • Terraform is connected to AWS

๐Ÿ“œ Step-by-Step Guide

  1. Initialize Your Terraform Environment ๐Ÿ”„

    • Start by running terraform init in your project directory. This command sets up Terraform by downloading necessary plugins and modules, making your environment ready for the next steps.
  2. Set Up Your Terraform Configuration Files ๐Ÿ“‚

    • Now, create three essential files:

      • main.tf: This file contains the definitions for your AWS resources, like the S3 bucket and access policies.

      • provider.tf: Here, you specify the AWS provider and the region youโ€™ll be using.

      • variables.tf: This file allows you to define variables to make your code more modular and easier to update in the future.

  3. Configure Your S3 Bucket ๐Ÿชฃ

    • In main.tf, define your S3 bucket resource and set up public access. Youโ€™ll also enable static website hosting and specify your index.html as the entry point.
  4. Upload Your Website Content ๐Ÿ–ผ๏ธ

    • Now, upload your HTML, JavaScript, CSS files, images and any other static assets to the S3 bucket. You can automate this with Terraform or use the AWS CLI for quick uploads.
  5. Plan and Apply with Terraform ๐Ÿ”โžก๏ธ๐Ÿš€

    • Run terraform plan to review the changes Terraform will make in your AWS account. This is a good way to double-check before deploying.

    • Then, run terraform apply -auto-approve to make your website live! ๐ŸŒ

  1. Visit Your New Website ๐ŸŽ‰

    • In the AWS S3 console, navigate to your S3 bucket, go to Properties, and find the Static website hosting URL.

  1. Open the URL in your browser, and voilร  โ€” your website is now live on AWS!". ๐ŸŽŠ


๐Ÿ’ก Wrapping Up

With Terraform, youโ€™ve not only deployed a static website but also adopted a modern, efficient way of managing infrastructure. ๐Ÿ”ฅ Next time, try expanding your Terraform configurations or integrating additional AWS services to make our project even more powerful.

0
Subscribe to my newsletter

Read articles from Biswanath Sah directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Biswanath Sah
Biswanath Sah