---
name: building-terraform-modules
description: 'Execute this skill empowers AI assistant to build reusable terraform modules based on user specifications. it leverages the terraform-module-builder plugin to generate production-ready, well-documented terraform module code, incorporating best practices for sec... Use when appropriate context detected. Trigger with relevant phrases based on skill purpose.'
allowed-tools: Read, Write, Edit, Grep, Glob, Bash(cmd:*)
version: 1.23.0
author: Jeremy Longshore <jeremy@intentsolutions.io>
license: MIT
tags:
  - devops
  - terraform
  - terraform-modules
compatibility: Designed for Claude Code
title: building-terraform-modules
canonical_url: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/building-terraform-modules
last_updated: 2026-10-07T06:06:28.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [assets/README.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/building-terraform-modules/assets/README.md), [assets/validation_rules.json](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/building-terraform-modules/assets/validation_rules.json), [references/README.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/building-terraform-modules/references/README.md), [scripts/README.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/building-terraform-modules/scripts/README.md).
>
> If the user asked to install this Skill, run `npx skilld install jeremylongshore/tons-of-skills-marketplace/building-terraform-modules`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/building-terraform-modules`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/jeremylongshore/tons-of-skills-marketplace/building-terraform-modules) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/building-terraform-modules`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/building-terraform-modules --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/building-terraform-modules --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Building Terraform Modules

## Overview

Build reusable, production-ready Terraform modules with proper variable definitions, outputs, validation rules, documentation, and examples. Generate modules following HashiCorp's standard module structure for AWS, GCP, and Azure resources with security best practices, tagging conventions, and lifecycle management.

## Prerequisites

- Terraform 1.0+ installed (`terraform version`)
- Cloud provider credentials configured for the target platform
- Understanding of the infrastructure resources the module will manage
- Familiarity with HCL syntax and Terraform module conventions
- `terraform-docs` installed for automated documentation generation (optional)

## Instructions

1. Define the module scope: determine which cloud resources the module manages and its input/output contract
2. Create the standard module file structure: `main.tf`, `variables.tf`, `outputs.tf`, `versions.tf`, `locals.tf`
3. Write `versions.tf` with `required_version` and `required_providers` blocks pinned to stable versions
4. Define input variables in `variables.tf` with descriptions, types, defaults, and validation rules
5. Implement resources in `main.tf` using variables for all configurable values; use `locals` for computed values
6. Add meaningful outputs in `outputs.tf` for resource IDs, ARNs, endpoints, and connection strings
7. Implement security defaults: encryption enabled, public access blocked, least-privilege IAM, logging enabled
8. Create an `examples/` directory with at least one complete usage example showing module invocation
9. Generate documentation with `terraform-docs markdown table . > README.md`
10. Validate the module with `terraform init && terraform validate` and test with `terraform plan` using the example

## Output

- Module files: `main.tf`, `variables.tf`, `outputs.tf`, `versions.tf`, `locals.tf`
- Example usage in `examples/basic/main.tf` with realistic variable values
- Auto-generated `README.md` with inputs, outputs, and usage documentation
- `.terraform-docs.yml` configuration for documentation generation
- Optional: test files using Terratest or `terraform test` (HCL-based)

## Error Handling

| Error | Cause | Solution |
|-------|-------|---------|
| `variable validation failed` | Input value does not meet validation rule | Check the `validation` block in `variables.tf`; adjust the value or the validation regex |
| `provider not found` | Missing or wrong provider source in `versions.tf` | Add the provider to `required_providers` with correct source and version constraint |
| `circular dependency` | Resources referencing each other in a loop | Refactor to break the cycle; use `depends_on` or separate into sub-modules |
| `output references undeclared resource` | Typo in resource name or resource removed | Verify resource names in `main.tf` match output references exactly |
| `module source not found` | Incorrect module path or registry reference | Verify the `source` path is relative (e.g., `./modules/vpc`) or a valid registry address |

## Examples

- "Build a Terraform module for an AWS VPC with configurable CIDR, public/private subnets across 3 AZs, NAT gateway, and flow logs."
- "Create a GCP Cloud Run module with custom domain, IAM bindings, and auto-scaling configuration as input variables."
- "Generate a Terraform module for an S3 bucket with versioning, encryption, lifecycle rules, and access logging, with all settings as optional variables with secure defaults."

## Resources

- Terraform module registry: https://registry.terraform.io/
- Module structure guide: https://developer.hashicorp.com/terraform/language/modules/develop/structure
- terraform-docs: https://terraform-docs.io/
- Module best practices: https://developer.hashicorp.com/terraform/language/modules/develop
