🚀 Automating Netlify Deployments with Terraform + CLI
Deploying static sites manually can be repetitive. With Terraform and the Netlify CLI, you can automate everything — from creating a Netlify site to deploying your frontend, and even setting environment variables.
In this guide, I’ll walk you through how I deployed a simple static website using Terraform and the Netlify CLI.
🔧 Prerequisites
Before you start, make sure you have:
Terraform installed
Netlify CLI installed (
npm install -g netlify-cli
)A Netlify account
A site folder with static content (
site/index.html
)
⚡ Step 1: Create a New Site with Terraform
We’ll use Terraform to dynamically generate a Netlify site name.
resource "random_pet" "suffix" {
length = 2
}
locals {
site_name = "hug-ibadan-${random_pet.suffix.id}"
}
output "site_name" {
value = local.site_name
}
This ensures each site gets a unique name like hug-ibadan-demo-wolf
.
⚡ Step 2: Deploy the Site with Netlify CLI
Since the Netlify Terraform provider doesn’t fully support site creation yet, we’ll call the Netlify CLI inside Terraform using null_resource
.
resource "null_resource" "netlify_site_deploy" {
provisioner "local-exec" {
command = <<EOT
netlify deploy --prod \
--dir "site" \
--site "f682ae93-7d1d-40c9-a19c-7e2728d8349a"
EOT
}
}
👉 Important: Use the Site ID (UUID) instead of the human-readable site name.
You can find this with:
netlify sites:list
⚡ Step 3: Add Environment Variables
If you want to set build/runtime environment variables:
resource "netlify_environment_variable" "welcome" {
site_id = "f682ae93-7d1d-40c9-a19c-7e2728d8349a"
key = "WELCOME_MESSAGE"
value = "Hello from Terraform 🚀"
}
⚡ Step 4: Apply and Deploy
Run:
terraform init
terraform apply
Once applied, Terraform will:
✅ Create a site
✅ Deploy your static files from the site/
directory
✅ Configure environment variables
You can confirm everything from your Netlify dashboard.
🔍 SEO Benefits of Terraform + Netlify
Fast deployment → Sites go live in seconds
Infrastructure as Code (IaC) → Your hosting setup is versioned and repeatable
SEO boost → Netlify automatically adds HTTPS and fast CDN delivery
Scalability → Perfect for personal blogs, portfolios, or even client projects
🎯 Final Thoughts
By combining Terraform with the Netlify CLI, you get the best of both worlds: automated infrastructure and one-command deployments. No more manual clicks on the Netlify dashboard!
If you’re working on static sites, JAMstack apps, or frontend projects, this workflow will save you hours.
Subscribe to my newsletter
Read articles from Udoma Kingsley Akpan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by