|
| 1 | +--- |
| 2 | +title: Building a Terraform Module Scaffold with structkit |
| 3 | +date: 2026-04-08 |
| 4 | +tags: |
| 5 | + - terraform |
| 6 | + - infrastructure-as-code |
| 7 | + - scaffolding |
| 8 | + - devops |
| 9 | +authors: |
| 10 | + - httpdss |
| 11 | +--- |
| 12 | + |
| 13 | +# Building a Terraform Module Scaffold with structkit |
| 14 | + |
| 15 | +> **SEO:** terraform module template generator, infrastructure as code scaffolding |
| 16 | +
|
| 17 | +## TL;DR |
| 18 | + |
| 19 | +If you maintain multiple Terraform modules, you know the pain: every new module starts with copy-pasting the same `main.tf`, `variables.tf`, `outputs.tf`, and `.github/` setup. [structkit](https://github.com/httpdss/structkit) lets you define that structure once in YAML and generate it consistently across your team. |
| 20 | + |
| 21 | +<!-- more --> |
| 22 | + |
| 23 | +## The Problem: Terraform Module Sprawl |
| 24 | + |
| 25 | +As your infrastructure grows, so does your module library. Two things are always true: |
| 26 | + |
| 27 | +1. **New modules start with copy-paste** — someone grabs an old module and strips it down |
| 28 | +2. **Consistency degrades over time** — modules end up with different structures, naming conventions, CI configurations |
| 29 | + |
| 30 | +This causes real pain: |
| 31 | +- Onboarding engineers spend hours understanding why Module A has a `providers.tf` but Module B does not |
| 32 | +- Security reviews find inconsistent IAM patterns across modules |
| 33 | +- CI pipelines fail because some modules have `terratest`, others do not |
| 34 | + |
| 35 | +## What structkit Does |
| 36 | + |
| 37 | +structkit is a YAML-first scaffolding tool that generates project structures from templates. You define the shape of a project once and structkit materializes it. |
| 38 | + |
| 39 | +```bash |
| 40 | +pip install structkit |
| 41 | +``` |
| 42 | + |
| 43 | +## Scaffolding a Terraform Module |
| 44 | + |
| 45 | +Here is a complete `structkit.yaml` for a standard Terraform module: |
| 46 | + |
| 47 | +```yaml |
| 48 | +structure: |
| 49 | + - name: "{{module_name}}" |
| 50 | + type: directory |
| 51 | + children: |
| 52 | + - name: main.tf |
| 53 | + type: file |
| 54 | + content: | |
| 55 | + # {{module_name}} -- {{module_description}} |
| 56 | + terraform { |
| 57 | + required_version = ">= 1.5" |
| 58 | + required_providers { |
| 59 | + aws = { |
| 60 | + source = "hashicorp/aws" |
| 61 | + version = "~> 5.0" |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +
|
| 66 | + - name: variables.tf |
| 67 | + type: file |
| 68 | + content: | |
| 69 | + variable "name" { |
| 70 | + description = "Name prefix for all resources" |
| 71 | + type = string |
| 72 | + } |
| 73 | +
|
| 74 | + - name: outputs.tf |
| 75 | + type: file |
| 76 | + content: | |
| 77 | + # Add module outputs here |
| 78 | +
|
| 79 | + - name: README.md |
| 80 | + type: file |
| 81 | + content: | |
| 82 | + # {{module_name}} |
| 83 | + {{module_description}} |
| 84 | +
|
| 85 | + ## Usage |
| 86 | + module "{{module_name}}" { |
| 87 | + source = "./{{module_name}}" |
| 88 | + name = "my-resource" |
| 89 | + } |
| 90 | +
|
| 91 | + - name: .github/workflows/terraform.yml |
| 92 | + type: file |
| 93 | + content: | |
| 94 | + name: Terraform |
| 95 | + on: |
| 96 | + pull_request: |
| 97 | + branches: [main] |
| 98 | + jobs: |
| 99 | + validate: |
| 100 | + runs-on: ubuntu-latest |
| 101 | + steps: |
| 102 | + - uses: actions/checkout@v4 |
| 103 | + - uses: hashicorp/setup-terraform@v3 |
| 104 | + - run: terraform init -backend=false |
| 105 | + - run: terraform validate |
| 106 | + - run: terraform fmt -check |
| 107 | +``` |
| 108 | +
|
| 109 | +## Generating the Module |
| 110 | +
|
| 111 | +```bash |
| 112 | +structkit generate \ |
| 113 | + --config structkit.yaml \ |
| 114 | + --var module_name=aws-s3-bucket \ |
| 115 | + --var module_description="Opinionated S3 bucket module with versioning and encryption" |
| 116 | +``` |
| 117 | + |
| 118 | +Output: |
| 119 | + |
| 120 | +``` |
| 121 | +aws-s3-bucket/ |
| 122 | +├── main.tf |
| 123 | +├── variables.tf |
| 124 | +├── outputs.tf |
| 125 | +├── README.md |
| 126 | +└── .github/workflows/terraform.yml |
| 127 | +``` |
| 128 | + |
| 129 | +Every module your team creates has the same CI setup, the same pre-commit hooks, the same README structure. |
| 130 | + |
| 131 | +## Remote Templates |
| 132 | + |
| 133 | +structkit supports remote content fetching. Host your template in a central Git repo: |
| 134 | + |
| 135 | +```yaml |
| 136 | +structure: |
| 137 | + remote: https://github.com/yourorg/structkit-templates/terraform-module/structkit.yaml |
| 138 | +``` |
| 139 | +
|
| 140 | +Now any engineer can scaffold a compliant module without knowing the internal structure: |
| 141 | +
|
| 142 | +```bash |
| 143 | +structkit generate \ |
| 144 | + --remote https://github.com/yourorg/structkit-templates/terraform-module/structkit.yaml \ |
| 145 | + --var module_name=aws-rds-postgres |
| 146 | +``` |
| 147 | + |
| 148 | +## Team-Wide Standardization |
| 149 | + |
| 150 | +Store templates in a central repository: |
| 151 | + |
| 152 | +``` |
| 153 | +yourorg/structkit-templates/ |
| 154 | +├── terraform-module/structkit.yaml |
| 155 | +├── terraform-root/structkit.yaml |
| 156 | +├── python-service/structkit.yaml |
| 157 | +└── github-action/structkit.yaml |
| 158 | +``` |
| 159 | + |
| 160 | +Platform teams own the templates. Product teams consume them. Everyone gets consistency without a manual review process. |
| 161 | + |
| 162 | +## Comparing Approaches |
| 163 | + |
| 164 | +| Approach | Consistency | Learning curve | |
| 165 | +|---|---|---| |
| 166 | +| Manual copy-paste | Degrades over time | Very low | |
| 167 | +| cookiecutter | Good | Medium | |
| 168 | +| copier | Good (with updates) | Medium-high | |
| 169 | +| structkit | Good | Low (just YAML) | |
| 170 | + |
| 171 | +structkit is YAML-first. If you can write YAML (which you already do for Terraform), you can write structkit templates. |
| 172 | + |
| 173 | +## What is Next |
| 174 | + |
| 175 | +- **AI/MCP integration**: structkit supports Claude MCP for natural-language scaffolding |
| 176 | +- **GitHub Actions**: run structkit in CI to enforce that new modules follow templates |
| 177 | + |
| 178 | +## Get Started |
| 179 | + |
| 180 | +```bash |
| 181 | +pip install structkit |
| 182 | +``` |
| 183 | + |
| 184 | +- GitHub: [httpdss/structkit](https://github.com/httpdss/structkit) |
| 185 | +- PyPI: [structkit](https://pypi.org/project/structkit/) |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +*Did this help? Star [httpdss/structkit](https://github.com/httpdss/structkit) and share your use case in the comments!* |
0 commit comments