In this project, I developed a streamlined infrastructure for hosting a static website using Terraform and Amazon Web Services (AWS) S3. The primary goal of this project is to demonstrate the automated provisioning and deployment of a web hosting solution for static websites.
I utilized Terraform, an IaC tool, to define and provision the AWS resources required for my static website hosting solution. This allows for version-controlled, repeatable infrastructure deployments.
I created an S3 bucket to store and serve the static website files. This bucket is configured for website hosting, allowing for easy content delivery.
I provided instructions and scripts for uploading and managing my website content within the S3 bucket.
- Basic knowledge of AWS services and concepts.
- Familiarity with Terraform and infrastructure as code principles.
- An AWS account with appropriate permissions.
- An IDE of your Choice , I would suggest VS Code Editor .
- This project serves as an excellent foundation for hosting various types of static websites, including personal blogs, portfolio sites, or small business websites.
Install Terraform and the AWS Command Line Interface (CLI) on your local machine. Configure your AWS credentials by running aws configure
and providing your AWS access key and secret key.
To prepare static website files (HTML), place them in the directory where your Terraform configuration files are located. Name the main HTML file "index.html," and optionally, you can also include an "error.html" file. If you prefer, you can reference my repository for the static website HTML files.
If we want to Create a terraform configuration file we have to use .tf (e.g., main.tf) to define the infrastructure as code using terraform.
- Define
provider.tf
file using the below code :
provider "aws" {
region = "ap-south-1"
}
- In your Integrated Development Environment (IDE), open the terminal and navigate to the directory where you have created these configuration files.
- After navigating to the directory where your configuration files are located in your IDE's terminal, you can run the following command to initialize Terraform and prepare it for use with AWS:
terraform init
Runningterraform init
will install the necessary plugins and modules required for connecting to AWS and managing your infrastructure. - And then define resource.tf file for creating bucket by using the below code :
resource "aws_s3_bucket" "bucket1" {
bucket = "web-bucket-mathesh"
}
- Then below command for creating the bucket :
terraform apply -auto-approve
- And then add the below codes in resource.tf file :
resource "aws_s3_bucket_public_access_block" "bucket1" {
bucket = aws_s3_bucket.bucket1.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_object" "index" {
bucket = "web-bucket-mathesh"
key = "index.html"
source = "index.html"
content_type = "text/html"
}
resource "aws_s3_object" "error" {
bucket = "web-bucket-mathesh"
key = "error.html"
source = "error.html"
content_type = "text/html"
}
resource "aws_s3_bucket_website_configuration" "bucket1" {
bucket = aws_s3_bucket.bucket1.id
index_document {
suffix = "index.html"
}
error_document {
key = "error.html"
}
}
resource "aws_s3_bucket_policy" "public_read_access" {
bucket = aws_s3_bucket.bucket1.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": [ "s3:GetObject" ],
"Resource": [
"${aws_s3_bucket.bucket1.arn}",
"${aws_s3_bucket.bucket1.arn}/*"
]
}
]
}
EOF
}
- And then again run the command :
terraform applyb -auto-approve
- The code above will apply the necessary configurations for features such as static website hosting, bucket policies, and blocking public access to your bucket.
- Certainly, it's important to customize the code to your specific needs. Please remember to change the bucket name, region, and configurations as per your requirements when using the code from the Terraform documentation.
- We use an output file to obtain your website link in your IDE, eliminating the need to access the link through the AWS Console.
- Define output.tf file by using the below terraform code :
output "websiteendpoint" {
value = aws_s3_bucket.bucket1.website_endpoint
}
3.And then run the following command :
terraform apply -auto-approve
4.It will give your website link as output as shown below
"web-bucket-shamshad.s3-website.ap-south-1.amazonaws.com"
Copy the link and paste it in your browser.