As your hosting needs grow, managing infrastructure manually becomes inefficient and error-prone.
That’s where Terraform comes in — an open-source Infrastructure as Code (IaC) tool that lets you define, provision, and manage cloud infrastructure using configuration files instead of clicking through dashboards.
In this tutorial, we’ll walk through how to deploy a simple website using Terraform, automate infrastructure setup, and manage updates like a pro.
See Also: How to Use Docker Swarm for Scalable Hosting (Step-by-Step Tutorial)
What Is Terraform?
Terraform (by HashiCorp) lets you describe your infrastructure in code.
Instead of creating servers or buckets manually, you write configuration files (using HCL – HashiCorp Configuration Language) to define your infrastructure.
Terraform then:
- Creates the resources automatically
- Manages changes
- Destroys them when no longer needed
It supports all major cloud providers — AWS, Azure, Google Cloud, DigitalOcean, and more.
Step 1: Install Terraform
On macOS:
brew tap hashicorp/tap
brew install hashicorp/tap/terraformOn Ubuntu:
sudo apt update && sudo apt install -y wget unzip
wget https://releases.hashicorp.com/terraform/1.7.5/terraform_1.7.5_linux_amd64.zip
unzip terraform_1.7.5_linux_amd64.zip
sudo mv terraform /usr/local/bin/
terraform -versionSee Also: How to Optimize Website Speed and Server Performance (Full Guide)
Step 2: Set Up Your Project Directory
Create a new folder:
mkdir terraform-website && cd terraform-websiteInside it, create a main configuration file:
touch main.tfStep 3: Define Your Infrastructure
Here’s an example of a simple Terraform configuration to host a website on AWS S3 + CloudFront:
provider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "static_site" {
bucket = "my-terraform-website"
website {
index_document = "index.html"
error_document = "404.html"
}
}
resource "aws_s3_bucket_policy" "site_policy" {
bucket = aws_s3_bucket.static_site.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-terraform-website/*"
}
]
}
EOF
}
resource "aws_s3_bucket_object" "index" {
bucket = aws_s3_bucket.static_site.id
key = "index.html"
source = "index.html"
content_type = "text/html"
}
This creates a static hosting bucket and uploads your website’s index.html.
Step 4: Initialize and Apply Terraform
Run these commands in your project directory:
terraform init
terraform plan
terraform applyTerraform will show what changes it’s about to make.
Type yes to confirm.
Your website will now be hosted on AWS S3, accessible via the public URL provided in the output.
See Also: How to Set Up Automatic Backups for Websites and Databases (Complete Guide)
Step 5: Manage and Destroy Resources
When you need to remove everything:
terraform destroyTerraform will automatically delete all resources it created — no manual cleanup required.
Bonus: Using Terraform for Other Hosts
You can deploy to:
- DigitalOcean: droplets, domains, and DNS records
- Vercel: via the Terraform Vercel Provider
- Azure: virtual machines, resource groups, and storage accounts
Example for Vercel:
provider "vercel" {}
resource "vercel_project" "site" {
name = "my-nextjs-site"
framework = "nextjs"
}Conclusion
With Terraform, you can automate your entire hosting workflow — from creating infrastructure to destroying it when finished.
It’s fast, repeatable, and works across multiple platforms, making it a cornerstone of modern DevOps hosting.
In the final Hosting Academy post, we’ll compare hosting costs across all platforms we’ve covered — and help you choose the best one for your needs.

