Infrastructure as Code (IaC) is about making deployments reproducible, automated, and collaborative. In this project, I built and deployed a static website on Netlify using Terraform, while storing the Terraform state securely in HCP Terraform (Terraform Cloud).
This was part of the Terraform Challenge, and Iβll walk you through what I did and how you can replicate it.
π― Project Goals
Deploy a static frontend site to Netlify
Manage it with Terraform (Infrastructure as Code)
Store Terraform state in HCP Terraform (Terraform Cloud)
Keep secrets out of the repo (use workspace env vars instead)
Make the setup re-runnable and reproducible
Live Site π comforting-fudge-57ff13.netlify.app
Repo π github.com/SamuelUdeh/terraform-netlify-challenge
π Tools I Used
Terraform β To define infrastructure as code
HCP Terraform (Terraform Cloud) β To store remote state securely
Netlify β To host the static site
GitHub β Version control + Netlify deploy hook
βοΈ How It Works
Static Site β A minimal index.html page lives in /site.
Netlify β Hosts the site, automatically deploying on every push to main.
Terraform β Manages Netlify site settings (publish dir, environment variables).
HCP Terraform β Stores state remotely, enabling collaboration and history.
Architecture Flow:
GitHub Repo ---> Netlify (Site Hosting) ---> Live URL
|
+---> Terraform ---> HCP Terraform (Remote State)
π§βπ» Step-by-Step Setup
- Prerequisites
Terraform β₯ 1.6
Accounts: Netlify, GitHub, HCP Terraform
Netlify Personal Access Token (NETLIFY_TOKEN)
2.Create Site in Netlify
In Netlify: New site from Git β connect repo β publish directory = site
3.Configure HCP Terraform
Create Organization + Workspace
Add NETLIFY_TOKEN as a sensitive environment variable
4.Terraform Config
Example versions.tf:
terraform {
required_providers {
netlify = {
source = "netlify/netlify"
version = "~> 0.2"
}
random = {
source = "hashicorp/random"
version = "~> 3.6"
}
}
cloud {
organization = "your-org"
workspaces {
name = "portfolio-site-workspace"
}
}
}
outputs.tf:
output "live_url" {
value = "https://${var.site_name}.netlify.app"
}
5.Run Terraform
terraform init
terraform plan
terraform apply
Output:
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
live_url = "https://comforting-fudge-57ff13.netlify.app"
repo = "https://github.com/SamuelUdeh/terraform-netlify-challenge"
π Lessons Learned
Netlify provider doesnβt create sites (yet), you create the site in the UI once, then manage settings via Terraform.
Remote state in HCP Terraform is safer and team-ready vs local terraform.tfstate.
Keep secrets out of code, use workspace environment variables like NETLIFY_TOKEN.
Even a minimal project (simple HTML site) is enough to demonstrate IaC principles.
πΈ Deliverables
β Repo with Terraform + site
β Remote state in HCP Terraform
β Live Netlify URL
π Wrap Up
This challenge showed how simple it is to connect Terraform β Netlify β HCP Terraform and manage even small projects as Infrastructure as Code.
Try it yourself:
π Clone my repo, configure HCP Terraform, and run terraform apply.
Happy coding, and thanks to HUG-Ibadan for the challenge inspiration! π
Top comments (0)