Terraform GitOps: Declarative Infrastructure Automation Through Git

Infrastructure management has shifted from manual provisioning to code-driven workflows. Terraform provides a declarative language for defining cloud resources, while GitOps applies DevOps best practices to infrastructure automation by using Git repositories as the single source of truth. Combining the two creates version-controlled, auditable, and automatically reconciled infrastructure deployments across environments.

What GitOps Means for Infrastructure

GitOps is an operational framework that takes DevOps best practices used for application development and applies them to infrastructure automation. At its core, GitOps uses Git repositories as the single source of truth for declarative infrastructure and applications. This approach leverages Git's version control capabilities to manage infrastructure changes, providing a clear audit trail and facilitating collaboration among team members.

In a GitOps workflow, any change to the infrastructure is made through a Git repository. Automated processes then sync these changes with the actual infrastructure, ensuring that the deployed state always matches the desired state defined in the repository. This method enhances transparency, improves security, and streamlines the change management process.

GitOps is the practice of using Git as the single source of truth for infrastructure. Every change goes through a pull request, every deployment is triggered by a Git merge, and the actual state of your infrastructure always matches what is in the repository. Terraform is a natural fit for GitOps because it already defines infrastructure declaratively in code. The challenge is building the automation that closes the loop between Git and your actual infrastructure.

Terraform as Infrastructure as Code

Terraform is an open source Infrastructure as Code tool created by HashiCorp. It allows developers and operations teams to define and provision infrastructure using a declarative language.

Terraform is an infrastructure-as-code tool developed by HashiCorp. It allows you to build, change, manage, and version your infrastructure through human-readable configuration files. It offers a unified way to define, provision, and manage resources across various cloud providers and services using Hashicorp Configuration Language syntax, which is easy to learn and read.

Because Terraform lets you describe infrastructure in code files, this enables version control, change tracking, collaboration, and repeatability when provisioning infrastructure, ensuring consistent deployments across environments and avoiding manual configuration errors.

Terraform supports infrastructure management across multiple cloud providers and on-premises data centers through plugins, which convert Terraform configurations into specific API calls for each platform. Terraform uses a declarative approach.

Terraform is one of the cornerstones of Infrastructure as Code, enabling teams to define and manage cloud resources in a declarative way. By eliminating manual provisioning, Terraform ensures consistency, scalability, and automation across multiple providers, like AWS, Azure, and Google Cloud. Its state management capabilities makes infrastructure changes more predictable and controlled.

Core GitOps Principles Applied to Terraform

The four GitOps principles applied to Terraform provide a clear operating model.

  • Declarative - Infrastructure is defined in Terraform HCL files
  • Versioned and immutable - All changes are committed to Git with full history
  • Pulled automatically - The CI/CD system watches Git and applies changes
  • Continuously reconciled - Drift is detected and corrected automatically

These principles map directly to Terraform's capabilities.

Principle Terraform Expression
Declarative Infrastructure is defined in Terraform HCL files, already done
Versioned and immutable All changes are committed to Git with full history
Pulled automatically CI/CD system watches Git and applies changes
Continuously reconciled Drift is detected and corrected automatically

Why GitOps Pairs With Terraform

Combining Terraform with GitOps creates a powerful synergy for infrastructure management. Both embrace a declarative approach. You specify the desired end state, and the tools handle the implementation details.

The pairing is particularly effective for the following reasons:

  • Declarative Nature
  • Version Control
  • Automation Friendly
  • State Management
  • Modularity

Declarative Nature: Both GitOps and Terraform embrace a declarative approach. You specify the desired end state, and the tools handle the implementation details.

Version Control: Terraform configurations can be easily stored in Git repositories, aligning perfectly with GitOps principles.

Automation Friendly: Terraform's command-line interface makes it ideal for integration with CI/CD pipelines, a key component of GitOps workflows.

State Management: Terraform's state files provide a clear picture of the current infrastructure, which can be compared against the desired state defined in Git.

Modularity: Terraform's module system allows for reusable, composable infrastructure components, making it easier to manage complex setups through GitOps practices.

By leveraging Terraform within a GitOps framework, organizations can achieve a high degree of automation, consistency, and traceability in their infrastructure management processes.

A powerful way to enhance Terraform's workflow is by integrating it with GitOps, where infrastructure is managed through version-controlled repositories. This approach ensures that infrastructure updates follow the same pull-request-driven process as application coding, improving traceability and reducing human error. With automation pipelines applying changes based on repository updates, infrastructure stays aligned with the desired states.

The GitOps Workflow With Terraform

The GitOps workflow for Terraform adds continuous reconciliation to standard CI/CD.

Developer creates feature branch

Developer modifies Terraform files

Developer opens pull request

CI automatically runs: fmt, validate, plan

Team reviews code + plan output

PR is approved and merged to main

CI automatically runs: terraform apply

Scheduled job detects drift and auto-reconciles

The key difference from regular CI/CD is the last step: continuous reconciliation.

A common implementation uses branches to represent environments. The architecture can use GitHub branches—dev and prod—to represent actual environments. These environments are defined by Virtual Private Cloud networks—dev and prod, respectively—into a Google Cloud project.

The process starts when you push Terraform code to either the dev or prod branch.

GitOps in action is illustrated by pipelines that combine Terraform and Google Cloud Platform, leveraging GitHub Actions to automate infrastructure provisioning. The term GitOps was first coined by Weaveworks, and its key concept is using a Git repository to store the environment state that you want.

Terraform is a HashiCorp tool that enables you to predictably create, change, and improve your cloud infrastructure by using code. In this tutorial you use Cloud Build, a Google Cloud continuous integration service, to automatically apply Terraform manifests to your environment.

Implementation Considerations

Implementing GitOps with Terraform requires aligning tooling around repository structure, CI validation, and automated apply.

The four-step process to implement GitOps with Terraform enables teams to streamline workflows, improve consistency, and enhance reliability of infrastructure deployments across various environments.

Leverage GitOps for Terraform or OpenTofu. As DevOps has taken hold in software development, infrastructure management has become a critical aspect of software development. We need cloud infrastructure to be agile and dependable. To meet these, two powerful concepts have emerged: GitOps and Infrastructure-as-Code. When combined, these approaches create a robust framework for managing infrastructure as code.

This article will explore how to implement GitOps with the IaC tool Terraform/OpenTofu, providing a streamlined approach to infrastructure management.

Harness is sponsor of the OpenTofu project and believes you should use OpenTofu over Terraform.

Practical examples show how to implement and configure Terraform to create cloud infrastructure in Azure with an integrated git repository and pipeline. If you're new to Terraform or unfamiliar with GitOps, this approach gets teams up and running.

The State of DevOps reports identified capabilities that drive software delivery performance. This tutorial helps with capabilities for architecture and process.

To demonstrate how this tutorial applies GitOps practices for managing Terraform executions, consider the following architecture diagram. Note that it uses GitHub branches—dev and prod—to represent actual environments. These environments are defined by Virtual Private Cloud networks—dev and prod, respectively—into a Google Cloud project.

Benefits and Collaboration

GitOps with Terraform improves collaboration and security. Infrastructure updates follow the same pull-request-driven process as application coding, improving traceability and reducing human error. With automation pipelines applying changes based on repository updates, infrastructure stays aligned with the desired states.

The GitOps workflow ensures any change to infrastructure is made through a Git repository. Automated processes then sync these changes with actual infrastructure, ensuring deployed state always matches desired state defined in repository. This method enhances transparency, improves security, and streamlines change management.

By combining these approaches, organizations can achieve automated, version-controlled, and collaborative infrastructure management.

Benefit Area Outcome
Transparency Clear audit trail via Git history
Security Changes reviewed via pull requests before apply
Consistency Declarative state ensures repeatable deployments
Collaboration Team members work on infrastructure as code
Reliability Continuous reconciliation corrects drift

Conclusion

Terraform GitOps represents a mature pattern for managing infrastructure as code with automation, version control, and continuous reconciliation. Terraform provides declarative configuration and state management for multi-cloud resources. GitOps provides the operational model of Git as source of truth, pull-based delivery, and automated sync.

Together they enforce a workflow where developers create feature branches, modify Terraform files, open pull requests for fmt, validate, and plan review, merge to main for automated apply, and rely on scheduled reconciliation to detect and correct drift. The result is infrastructure that is versioned, auditable, and consistently aligned with the desired state defined in code.

Organizations that adopt this pairing achieve higher automation, consistency, and traceability in infrastructure management while reducing manual errors and improving collaboration across teams.

Sources

  1. Harness Blog
  2. OneUptime
  3. LinkedIn
  4. Spacelift Blog
  5. Google Cloud Documentation

Related Posts