Deploying a Static Website on Netlify with Terraform + HCP Remote State

Mary EMary E
3 min read

Infrastructure as Code (IaC) isn’t just for servers and cloud VMs anymore. With Terraform, you can provision modern frontend hosting platforms like Netlify — and when combined with HCP Terraform for remote state management, you get a workflow that’s both reproducible and team-friendly.

In this post, I’ll walk through how I built and deployed a small static site on Netlify using Terraform, while managing my Terraform state securely in HCP Terraform.


Prerequisites

Before getting started, you’ll need:

  • Terraform v1.x installed

  • A Netlify account

  • A Netlify Personal Access Token (NETLIFY_TOKEN)

  • An HCP Terraform (Terraform Cloud) organization + workspace

  • A GitHub account (if using Git-based deploys)


Project Structure

Here’s what my repo looks like:


├── main.tf
├── providers.tf
├──variables.tf
├── outputs.tf
├── site/
│   └── index.html
└── README.md

Remote State with HCP Terraform

Instead of storing Terraform state locally (terraform.tfstate), I connected to HCP Terraform using a cloud block:

terraform {
  cloud {
    organization = "your-hcp-org" #your-organization

    workspaces {
      name = "netlify-challenge" #workspace
    }
  }

  required_providers {
    netlify = {
      source  = "AegirHealth/netlify"
      version = "0.6.12"
    }
    random = {
      source = "hashicorp/random"
    }
  }
}

provider "netlify" {
  token = var.netlify_api_token
}
provider "random" { }

This means:

  • State is stored securely in Terraform Cloud.

  • Multiple team members can collaborate without conflicts.


Deploying the Netlify Site

Here’s the minimal Terraform config for provisioning a Netlify site:


resource "random_pet" "name" {
  length    = 2
}

resource "netlify_site" "example" {
  name = random_pet.name.id

  repo {
    provider    = "github"
    repo_path   = "your-username/your-repo"
    repo_branch = "master"
    dir         = "site"
    command     = ""
  }
}

resource "netlify_build_hook" "build_trigger" {
  site_id = netlify_site.example.id
  branch  = "master"
  title   = "Terraform-triggered build"
}
in my variables.tf 

variable "netlify_api_token" {
  description = "Netlify personal access token"
  type        = string
  sensitive   = true
}

This does a few things:

  • Generates a random unique site name (random_pet)

  • Creates a Netlify site linked to a GitHub repo

  • Publishes the /site directory as the live frontend

  • triggers the netlify_build with the build hook


Minimal Static Site

Inside /site/index.html:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello from Terraform + Netlify</title>
  </head>
  <body>
    <h1>🚀 Deployed with Terraform & HCP Remote State</h1>
    <p>This site is fully reproducible via IaC.</p>
  </body>
</html>

Simple, but enough to validate the deployment.


Running It

  1. Initialize Terraform (with HCP remote backend):

     terraform init
    
  2. Plan the resources:

     terraform plan
    
  3. Deploy :

     terraform apply
    
  4. Grab the site URL:

     terraform output site_url
    

Visit that URL, and you’ll see your live Netlify site.


Key Takeaways

  • IaC for Frontend Hosting: Terraform isn’t just for servers — you can manage Netlify sites declaratively.

  • Remote State = Team Ready: Using HCP Terraform ensures state consistency across machines.

  • Reproducibility: Anyone with the repo + a token can spin up the same infrastructure.


Deliverables

  • ✅ Public GitHub repo with Terraform + /site

  • ✅ Live site link (Netlify subdomain)

  • ✅ Screenshot of successful terraform apply

  • ✅ This write-up documenting the process


0
Subscribe to my newsletter

Read articles from Mary E directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mary E
Mary E