
Terraform Cookbook: Development Environment Recipe
A step-by-step guide for a complete Terraform development env, including version management, documentation generation, linting, security scanning, and more.
A quick and easy recipe to set up a killer development environment for writing Terraform modules. Tested on Linux Ubuntu 24.04.1 LTS for x86_64 architecture (should work on other debian-based systems. For other Linux distributions, some tweaks are required).
- Linux utilities (curl, jq, tar, unzip, git)
- terraform CLI: HashiCorp's Terraform CLI
- terraform-docs: tool for generating module documentation
- TFLint: terraform linter
- Pre-commit: git hook manager
- Pre-commit-terraform: terraform git hooks
- Checkov: static analysis for IaC
Manage multiple Terraform versions like a pro. Simplifies the management of multiple Terraform versions, ensuring compatibility with different projects.
tenv
. Every good chef needs a well-organized kitchen. When it comes to managing different versions of Terraform, tenv
will keep your environment neatly organized and easily accessible. It also supports OpenTofu, Terragrunt, and Atmos. Let's get it installed:tenv
is always available when we start cooking terraform modules.terraform
CLI. This is the workhorse of our infrastructure kitchen – the pan we'll use to cook up all our infrastructure recipes. We'll use tenv
to fetch and install the perfect pan for the job. If you wish, use Opentofu, which is an "more open" source altenative.Automatically generate documentation for your terraform modules (inputs, outputs, etc.).
A linter for Terraform code. Catch syntax errors, enforce standards, and keep your Terraform configs clean.
TFLint
scans our Terraform code for syntax errors and potential issues, ensuring our code is sound. Let's add this to our kitchen:.tflint.hcl
file is the configuration file for TFLint. Its purpose is to define the rules, plugins, and configurations that TFLint will use when analyzing your Terraform code. This allows you to enforce best practices, identify issues, and customize the linter's behavior to align with your project requirements. The configuration I currently use for terraform AWS resources is like the bellow example.Trivy scan your IaC files, container images, and dependencies for vulnerabilities.
Checkov detects misconfigurations, security risks, and compliance violations in Terraform and other IaC templates.
Trivy
and Checkov
to filter out potential vulnerabilities and misconfigurations from our infrastructure code. Think of them as two different types of strainers, each catching different kinds of unwanted elements. Let's add them:Trivy
and Checkov
in our kitchen, we can confidently cook a safe and secure code. This double-straining approach provides a more comprehensive security check, catching both specific vulnerabilities and broader misconfigurations.Ensures consistent code quality by running checks (e.g., linting, formatting, security scans) before committing code.
pre-commit
acts as our kitchen workflow checklist, ensuring we systematically employ all our quality control and tools (like TFLint
, Trivy
, and Checkov
) before we "serve" (commit) our infrastructure code.pre-commit
uses git hooks, which are scripts that run automatically at specific points in the Git workflow. In this case, we're going to use pre-commit
hooks, which run before each git commit. This ensures that our code is always checked before it's added to the project's history. Let's set up our kitchen workflow checklist:.pre-commit-config.yaml
file is the configuration file for the Pre-commit framework that define the pre-commit hook.terraform fmt
and others pre-commit
hooks to format our code in a consistent way. This makes our code easier to read, understand, and maintain, both for ourselves and our colleagues.A template that serves as a foundation for creating Terraform modules.
- An S3 Bucket for State Storage. It includes server-side encryption, versioning, and lifecycle rules for noncurrent object transition and expiration, ensuring the safety and management of our stored data. It also blocks public access, preventing unwanted access.
- A DynamoDB Table for State Locking. This acts as a coordination system in our kitchen, preventing conflicts when we have multiple chefs.
pre-commit
, you can run all the quality and security checks we set up earlier, ensuring a perfect and secure final dish.