Deploying a Static Website on Amazon S3 with Terraform!

vishal_raju_6a7ca9503a75b

Vishal Raju

Posted on July 26, 2024

Deploying a Static Website on Amazon S3 with Terraform!

Introduction
Static websites are a simple and effective way to present content without the need for server-side processing. Amazon S3 provides a robust platform for hosting these websites, ensuring high availability and scalability. Terraform, an Infrastructure as Code (IaC) tool, can automate the creation and management of your AWS resources, making the deployment process even more streamlined.

In this guide, we will walk through the process of hosting a static website on Amazon S3 using Terraform, leveraging a modular file structure for clarity and ease of management. By the end of this tutorial, you will have a fully functional static website hosted on Amazon S3, managed entirely through Terraform.

Prerequisites
Ensure you have the following prerequisites before starting:

AWS Account: Sign up for an AWS account if you don’t have one.
Terraform: Download and install Terraform from the official website.
AWS CLI: Install the AWS CLI by following the instructions here.
AWS Credentials: Configure your AWS CLI with your credentials by running aws configure.

Step 1: Create the Project Directory
Begin by creating a directory for your Terraform project and navigating into it.

mkdir my-static-website
cd my-static-website

Step 2: Define the Terraform Configuration
Create a file named terraform.tf and define your provider configuration to set up Terraform with the AWS provider.

terraform.tf

terraform {
required_version = "1.8.5"
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.40.0"
}
}
}

provider "aws" {
profile = "default"
region = "us-east-1"
}
Step 3: Create the S3 Bucket
Create a file named bucket.tf to define your S3 bucket and upload an index.html file to it.

bucket.tf

resource "aws_s3_bucket" "terraform_demo_bucket" {
bucket = "terraform-demo-1808"
}

resource "aws_s3_object" "index_file" {
bucket = aws_s3_bucket.terraform_demo_bucket.id
key = "index.html"
source = "index.html"
content_type = "text/html"
etag = filemd5("index.html")
}

resource "aws_s3_bucket_website_configuration" "website_config" {
bucket = aws_s3_bucket.terraform_demo_bucket.id

index_document {
suffix = "index.html"
}
}
Step 4: Set Up Bucket Policies
Create a file named policy.tf to define your S3 bucket policies to allow public access.

policy.tf

resource "aws_s3_bucket_public_access_block" "public_access_block" {
bucket = aws_s3_bucket.terraform_demo_bucket.id
block_public_acls = false
block_public_policy = false
}

resource "aws_s3_bucket_policy" "bucket_policy" {
bucket = aws_s3_bucket.terraform_demo_bucket.id

policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "PublicReadGetObject"
Effect = "Allow"
Principal = ""
Action = ["s3:GetObject"]
Resource = "${aws_s3_bucket.terraform_demo_bucket.arn}/
"
},
]
})
depends_on = [aws_s3_bucket_public_access_block.public_access_block]
}
Step 5: Configure the Output
Create a file named output.tf to define the output variable for your website’s URL.

output.tf

output "website_url" {
value = "http://${aws_s3_bucket.terraform_demo_bucket.bucket}.s3-website.${aws_s3_bucket.terraform_demo_bucket.region}.amazonaws.com"
}
Step 6: Initialize Terraform
Initialize Terraform to prepare the working directory for managing infrastructure. This command downloads and installs the required provider plugins.

terraform init

Step 7: Validate the Configuration
Validate the Terraform configuration files to ensure the syntax is correct.

terraform validate

Step 8: Plan the Deployment
Generate and review the execution plan to understand the changes Terraform will apply.

terraform plan

Step 9: Apply the Configuration
Apply the Terraform configuration to create the infrastructure.

terraform apply

Step 10: Access Your Website
After the apply process completes, Terraform will output your website's URL. Visit the URL to see your static website live.

Conclusion
Congratulations! You have successfully hosted a static website on Amazon S3 using Terraform. This approach ensures that your infrastructure is version-controlled and easily reproducible. By following this guide, you can quickly deploy static websites for various purposes, such as personal blogs, portfolios, or documentation sites. Explore the power of Infrastructure as Code with Terraform and elevate your web hosting experience!

💖 💪 🙅 🚩
vishal_raju_6a7ca9503a75b
Vishal Raju

Posted on July 26, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related