Cloud/DevOps Project - 2

Biswanath SahBiswanath Sah
3 min read

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

Flowchart illustrating static website deployment using Terraform and AWS. Steps include creating an S3 bucket, configuring resources, making the bucket public, enabling website hosting, uploading website files, and accessing the site via an S3 website URL.


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! ๐Ÿ–ฅ๏ธ

By the end of this tutorial, you'll have your website up and running on Amazon S3, complete with automated infrastructure management. No more clicking around in the AWS console; everything will be handled by Terraformโ€™s code-based approach. ๐Ÿ™Œ

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


๐Ÿ› ๏ธ Tools and Technologies Need

  • 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: Simple Storage Service(S3) is Amazonโ€™s highly scalable and durable object storage service. Itโ€™s designed to store and retrieve any amount of data from anywhere on the web, making it perfect for hosting static websites.


๐Ÿ“œ 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 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! ๐ŸŒ

  6. Visit Your New Website ๐ŸŽ‰

    • In the AWS S3 console, navigate to your S3 bucket, go to Properties, and find the Static website hosting URL. Open it up in your browser, and voila! Your website is now live on AWS. ๐ŸŽŠ

๐Ÿ’ก Wrapping Up

With Terraform, youโ€™ve not only deployed a static website, but youโ€™ve also adopted a modern, efficient way of managing infrastructure. ๐Ÿ”ฅ Next time, try adding more Terraform configurations to expand site or integrate additional AWS services.

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