Cloud/DevOps Project - 2
๐ Static Website Hosting on AWS S3 Using Terraform ๐
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
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.
- Start by running
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.
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 yourindex.html
as the entry point.
- In
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.
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! ๐
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.
Subscribe to my newsletter
Read articles from Biswanath Sah directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by