Feedback

Chat Icon

DevSecOps in Practice

A Hands-On Guide to Operationalizing DevSecOps at Scale

IaC, Security and Secure Collaboration
62%

Versioning Best Practices and Security

In a DevSecOps environment, version control is crucial for tracking changes, collaborating, and ensuring reproducibility. Since security is not a one-time task but an ongoing process, versioning Terraform code helps in integrating continuous security checks and improvements. Based on the popular saying "If it's not in version control, it doesn't exist," here are some best practices for managing Terraform code in a versioned, collaborative, and secure manner.

Use Git and a Clear Workflow

Treat your Terraform configuration as you would application code. Keep it in a version control system (e.g., Git) and require changes to go through pull requests and security checks.

Adopt a branching strategy that suits your team (feature branches, trunk-based, etc.), and encourage everyone to write descriptive commit messages to document what infrastructure change is being made. If there are any potential security implications, make sure to share them in the commit message or PR description. Every change should be done via pull requests so that teammates can review Terraform code diffs and catch issues or suggest improvements. Enforce code reviews for all Terraform changes – this helps share knowledge and prevent errors and potential security issues from reaching your live infrastructure.

Using Git not only provides history and accountability but also enables integration with CI (so that, for example, every PR triggers a terraform plan).

Manage Provider Versions with Lock Files

Terraform automatically generates a terraform.lock.hcl dependency lock file, which pins provider versions used. Commit this lock file to Git, just as you would commit a package-lock.json for application code. Keeping the lock file in VCS ensures that everyone on the team (and the CI pipeline) uses the same provider versions, avoiding the "works on my machine" problem. From a security perspective, this prevents unexpected provider updates that might introduce breaking changes or vulnerabilities.

When you want to intentionally upgrade providers, run terraform init -upgrade and review the changes to terraform.lock.hcl in your pull request to discuss any provider updates. This way, provider changes are tracked and reviewed. Never ignore or delete the lock file, or else each Terraform init could pull the latest provider allowed by your version constraints, which might introduce breaking changes unexpectedly. In short, treat terraform.lock.hcl as part of your codebase for reliable, reproducible Terraform runs.

Structure Repositories and Modules Logically

Organize your Terraform code to simplify collaboration and reuse. There are some common strategies for structuring Terraform code:

  • Monorepo

DevSecOps in Practice

A Hands-On Guide to Operationalizing DevSecOps at Scale

Enroll now to unlock all content and receive all future updates for free.