DEV Community

Cover image for Netlify Site + HCP Terraform Remote State
BENEDICT OKAFOR
BENEDICT OKAFOR

Posted on

Netlify Site + HCP Terraform Remote State

Deploying infrastructure manually is time-consuming and error-prone. What if you could define your entire cloud setup as code and deploy it with a single command? That’s where Terraform comes in.

In this blog post, I’ll walk you through my Terraform-Deploy project, which automates infrastructure provisioning using Terraform. Whether you're new to Infrastructure as Code (IaC) or looking to streamline your deployments, this guide will help you get started.

Would be utilizing HCP Terraform (formerly Terraform Cloud) for secure, centralized state storage. This approach improves collaboration, security, and scalability for infrastructure deployments.

To successfully see through this project, the following are key requirements.

  • Terraform is installed on your local operating system
  • A Netlify account: Sign up for a free account
  • An HCP Terraform account: Sign up for a free account
  • Terraform CLI: Install Terraform

Note: Without the right configuration, you would get an error.

Setup

Configure Netlify Credentials

You need a Netlify Personal Access Token to allow Terraform to deploy to your account.

  • Go to your Netlify dashboard.
  • Navigate to User Settings> Applications > Personal Access Tokens.
  • Click New access token and give it a descriptive name.

  • Copy the generated token.
  • On your HCP Terraform Account, create a project, then a Workspace

  • You should get a custom provider resource block to add to your project.

  • Clone this repository
  • Set environment variables:
    export NETLIFY_TOKEN="your_netlify_token"

  • On your project terminal, run terraform loginto authenticate HCP Cloud

On your first login, you would be prompted to input your generated token

  • Initialize Terraform terraform init

  • Perform your changes on your project, run terraform plan

N:B: If you encounter any issues connecting, you can run the plan and apply by appending the token terraform plan -var="NETLIFY_TOKEN=<your_token>"/

-Output after a successful Terraform plan

Run terraform apply and a successful build on HCP

Deploy your application to Netlify

  • On Netlify profile >> Click add new project >> select a deployment method you prefer

Live App URL & app on Browser

By using HCP Terraform for remote state, you eliminate the risks of local state management while gaining better collaboration and security. Whether you're a solo developer or part of a team, this approach ensures reliable, scalable infrastructure deployments.

Have questions? Drop a comment below! 🚀

Also, you could check out the HUG Ibadan Meetup group (https://www.meetup.com/ibadan-hashicorp-user-group/) to stay updated on events.

Terraform HashiCorp DevOps #InfrastructureAsCode CloudComputing

Top comments (0)