CraftedTemplate
Blog How to Deploy Websites with Terraform (Infrastructure as Code)

How to Deploy Websites with Terraform (Infrastructure as Code)

11/2/2025 • Festus Ayomike
How to Deploy Websites with Terraform (Infrastructure as Code)

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:

  1. Creates the resources automatically
  2. Manages changes
  3. 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:

Code · batchfile
brew tap hashicorp/tap
brew install hashicorp/tap/terraform

On Ubuntu:

Code · batchfile
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 -version

See Also: How to Optimize Website Speed and Server Performance (Full Guide)

Step 2: Set Up Your Project Directory

Create a new folder:

Code · batchfile
mkdir terraform-website && cd terraform-website

Inside it, create a main configuration file:

Code · batchfile
touch main.tf

Step 3: Define Your Infrastructure

Here’s an example of a simple Terraform configuration to host a website on AWS S3 + CloudFront:

Code · sh
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:

Code · batchfile
terraform init
terraform plan
terraform apply

Terraform 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:

Code · batchfile
terraform destroy

Terraform 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:

Code · sh
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.