liveBook · Manning

Software template

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A template repository is a repository whose contents serve as the starting point for creating new repositories.

Starting every Terraform project by hand is repetitive and makes it easy for teams to forget a tool or configure it differently. A software template solves that problem by keeping shared boilerplate in a repository that can be copied into each new project. GitHub Template Repositories support this directly. section 7.2.1

A copied repository still needs project-specific edits, such as its name. When those edits multiply, Cookiecutter can ask for values and generate files conditionally. For a Terraform module, the chosen provider can determine which TFLint plugin appears in the generated configuration. section 7.2.1

Listing 7.2 Calling Cookiecutter with terraform-module-cookiecutter
$ cookiecutter gh:TerraformInDepth/terraform-module-cookiecutter #1
  [1/6] name (): terraform-aws-test-module                       #2
  [2/6] Select license
    1 - All Rights Reserved
    2 - MIT license
    3 - BSD license
    4 - Apache Software License 2.0
    5 - GNU General Public License v3
    Choose from [1/2/3/4/5] (1):
  [3/6] author (): Robert Hafner
  [4/6] primary_provider (hashicorp/aws):
  [5/6] provider_min_version (5.0):
  [6/6] private_registry_url ():                                 #3
$ ls -1a terraform-aws-test-module                               #4
.
..
.checkov.yml
.github
.gitignore
.opentofu-version
.pre-commit-config.yml
.terraform-docs.yml
.terraform-version
.tflint.hcl
LICENSE
README.md
main.tf
makefile
outputs.tf
providers.tf
variables.tf

The Cookiecutter invocation prompts for a module name, license, provider, minimum provider version, and registry URL, then creates a project containing Terraform files and tooling configuration. section 7.2.1

Put reusable CI workflows in the template as well. A GitHub Actions workflow can run TFLint through `make test_tflint` on pushes and pull requests. This works across modules that share the expected makefile; failed tests are visible on the pull request and can block it. section 7.7.2

Listing 7.40 TFLint on GitHub Actions
name: Lint
 
on:                                             #1
  push:
  pull_request:
 
jobs:
  tflint:
    runs-on: ubuntu-latest
 
    steps:
      - name: Checkout source code              #2
        uses: actions/checkout@v4               #3
 
      - name: Setup TFLint
        uses: terraform-linters/setup-tflint@v4 #4
 
      - name: Run TFLint
        run: make test_tflint                   #5

This workflow checks out the repository, sets up TFLint, and calls the makefile target—the pieces a new module needs to inherit together. section 7.7.2

Adapting a provider template

A provider starter repository may not use Cookiecutter, so cloning it requires manual cleanup. Read its README and check the supported Go and Terraform versions. Replace organization-specific ownership and conduct files, update Dependabot configuration, remove the template maintainer’s internal file, and revise the README. Rename the Go module with `go mod edit -module github.com/YOUR_ORGANIZATION/terraform-provider-YOUR_SERVICE_NAME`, run `go mod tidy`, and update `main.go` so `go generate` uses the new name. section 12.2.1

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains repository copying, conditional generation, and manual provider-template cleanup; stops short of detailing how to create a GitHub Template Repository.
Examples
Shows Cookiecutter generating a Terraform module, a reusable TFLint workflow, and manual adaptation of a Terraform provider starter.
What it assumes
Assumes familiarity with repositories and Terraform projects, while defining repository copying as the basic template approach.
What you can do after
Apply it via GitHub Template Repositories or Cookiecutter to start projects with shared files, then adapt names, policies, and tooling.
How it connects
Connects software templates to Cookiecutter, Terraform modules, TFLint, GitHub Actions, and pull-request checks.

Also explained in

Compare how 3 books explain software template →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.