In Advantages of using GitFlow for Terraform, we covered how GitFlow helps teams manage infrastructure changes with better organization, feature branch isolation, and automated validation. But applying GitFlow to Terraform isn’t as simple as following a standard Git workflow. Infrastructure is stateful, changes have cascading effects, and Terraform state files don’t merge like application code.
Successfully implementing GitFlow for infrastructure code requires careful attention to tooling and process. While the core GitFlow patterns remain the same, infrastructure code introduces some wrinkles to deployment; stateful infrastructure and the resultant feedback loops are quite different from vanilla software deploys. The following best practices should be helpful in helping you adapt GitFlow for infrastructure deployments.
The foundation of successful GitFlow adoption for infrastructure code is proper state management. Start by configuring unique backend states for each branch type and implementing consistent workspace naming patterns. This approach allows feature branches to maintain isolated states without affecting shared environments.
Here's a practical example of how you might configure a workspace for a feature branch working on network changes:
While terraform plan outputs provide basic validation, integrating security scanning into your pull request process catches potential misconfigurations early. Tools like tfsec can identify security issues before they reach shared environments. Here's a straightforward GitHub Actions workflow to automate these checks:
When working with shared modules across multiple branches, explicit version pinning prevents unexpected changes during development. Instead of combining module updates with infrastructure changes, handle version updates through dedicated pull requests. Here's an example of how to reference modules in a feature branch:
Effective branch protection goes beyond basic code review requirements. A robust setup incorporates terraform plan verification and security scan results. Here's an implementation using GitHub's API to set up these guardrails:
To identify unauthorized changes quickly, implement regular infrastructure drift checks. This automated process helps spot modifications made outside of Git by running checks against your main branch. Here's a practical drift detection workflow:
Terragrunt simplifies environment-specific configurations while maintaining consistent module code. This tool helps teams separate environment differences into clear configuration files rather than duplicating module code. Here's how a typical Terragrunt environment configuration might look:
A well-structured pull request template streamlines infrastructure reviews by gathering important information upfront. Include plan outputs, resource changes, and rollback procedures. Here's a practical template:
These practices reflect real-world adaptations of GitFlow for infrastructure management. Each organization typically modifies these patterns based on their scale, compliance requirements, and team structure. Many teams start with basic branch protection and automated planning, then incrementally add security scanning and advanced workflows as their infrastructure complexity grows.
There's a whole ecosystem of tools available to help make GitFlow work smoothly with infrastructure code. In this section we’ll look at some of the core pieces in the ecosystem that will help with GitFlow implementation.
TACOS like Terrateam automate pull request workflows with plan outputs, security policies, and team notifications. These tools integrate directly with GitHub or GitLab, handling the heavy lifting of state management and access control while teams focus on infrastructure changes.
Multiple tools work together to create comprehensive security coverage. Here's a GitHub Actions workflow that combines several popular scanning tools:
TFLint helps catch issues that Terraform's built-in validators might miss. Here's a configuration that covers common problems:
CI/CD pipelines form the foundation of automated infrastructure testing and deployment with GitFlow. GitHub Actions workflows handle initialization, validation, planning, and optional apply stages based on branch context. A production-grade pipeline includes distinct configurations for feature branches, development, and main:
Pre-commit hooks catch issues before they reach your repository. Pre-commit hooks automate formatting checks and basic validation, preventing common issues from reaching the remote repository. A sample pre-commit configuration might include:
For teams not using a TACOS platform, here's how to configure S3 with DynamoDB locking:
These tools work together to automate security checks, enforce standards, and coordinate team changes. While you don't need every tool to get started, having the right combination helps teams focus on infrastructure improvements while maintaining quality and security controls.
Team size and operational complexity are the main factors in deciding whether GitFlow adoption makes sense for an engineering organization. Infrastructure teams with 10 or more engineers typically encounter coordination challenges that GitFlow addresses effectively, while smaller teams might find simpler branching strategies more appropriate. The complexity of your software deployments also influence this decision - organizations deploying across multiple regions or cloud providers often benefit from GitFlow's structured approach, but teams requiring multiple daily deployments may find it creates unnecessary overhead.
Compliance requirements and change control processes can tip the balance toward GitFlow adoption. Its clear separation of development and production branches, combined with structured release management, aligns well with audit requirements while maintaining development velocity. However, getting the implementation right means making a significant investment in automation, tooling, and team training. Open-source tools like Terrateam can help reduce this overhead, but organizations still need to evaluate their deployment frequency, compliance requirements, and existing automation capabilities to determine if GitFlow’s benefits justify its complexity.
Ready to streamline your GitFlow workflow for Terraform? Managing infrastructure changes securely and efficiently starts with the right tooling. Doppler helps teams centralize and automate secrets management, reducing the risks tied to state files, API keys, and environment variables. Try a demo and see how Doppler keeps your infrastructure secure and scalable.
Trusted by the world’s best DevOps and security teams. Doppler is the secrets manager developers love.