Create reusable Terraform modules with variables and outputs
✓Works with OpenClaudeYou are a Terraform infrastructure engineer. The user wants to create a reusable Terraform module with input variables, outputs, and best-practice structure.
What to check first
- Verify Terraform is installed:
terraform version - Confirm your working directory structure supports a modules subdirectory
- Check that you have a root module or plan to create one alongside your module
Steps
- Create a module directory structure:
mkdir -p modules/my_module/{main,variables,outputs,terraform.tfvars} - Define input variables in
modules/my_module/variables.tfwith type, description, and optional default values - Write resource configurations in
modules/my_module/main.tfthat reference variables usingvar.variable_name - Export values from resources in
modules/my_module/outputs.tfusing theoutputblock withvalueanddescription - Create a root
main.tfthat calls the module usingmodule "block_name" { source = "./modules/my_module" } - Pass input variables to the module in the root module by setting them inside the
moduleblock - Reference module outputs in root with
module.block_name.output_namesyntax - Run
terraform initto initialize the working directory and download modules - Run
terraform planto validate variable passing and resource generation - Run
terraform applyto provision resources defined by the module
Code
# modules/my_module/variables.tf
variable "environment" {
type = string
description = "Environment name (dev, staging, prod)"
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Environment must be dev, staging, or prod."
}
}
variable "instance_count" {
type = number
description = "Number of instances to create"
default = 2
}
variable "tags" {
type = map(string)
description = "Common tags for all resources"
default = {}
}
variable "vpc_cidr" {
type = string
description = "CIDR block for VPC"
validation {
condition = can(cidrhost(var.vpc_cidr, 0))
error_message = "VPC CIDR must be valid CIDR notation."
}
}
# modules/my_module/main.tf
terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
tags = merge(
var.tags,
{
Name = "${var.environment}-vpc"
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Terraform Skills
Other Claude Code skills in the same category — free to download.
Terraform State
Manage Terraform state with remote backends (S3, Azure, GCS)
Terraform Workspace
Configure Terraform workspaces for multi-environment management
Terraform Provider
Write custom Terraform providers with Go
Terraform Import
Import existing infrastructure into Terraform state
Terraform Testing
Write Terraform tests with Terratest and terraform test
Terraform CI/CD
Set up Terraform CI/CD with GitHub Actions and Atlantis
Terraform Security
Scan Terraform for security issues with tfsec and Checkov
Want a Terraform skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.