mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
hanzoskills.com/llms.txt now opens with the install line, the API reference index and the company index, so an agent that lands on any one of the three can find the rest. robots.txt claimed 537 documents while llms.txt counts 920. The number is gone from robots.txt; llms.txt derives it. public/ is regenerated from scripts/site.py, which it had fallen behind: the copy-prompt menu and agent bar were in the generator and not in the committed pages. tests/site.mjs passes against the result. Co-authored-by: Hanzo Dev <[email protected]>
886 lines
34 KiB
HTML
886 lines
34 KiB
HTML
<!doctype html>
|
|
<html lang="en"><head>
|
|
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>infrastructure-terraform-patterns</title>
|
|
<meta name="color-scheme" content="dark">
|
|
<style>
|
|
/* Committed to one visual world: a dark instrument panel. No light variant —
|
|
the dot field and the hairline rules only read on black, and a naive
|
|
inversion would wash both out. Every colour is painted explicitly so the page
|
|
never borrows a host background. */
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
|
|
|
|
:root{
|
|
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
|
|
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
|
|
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
|
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
|
|
--font-zen-pixel-square:'Zen Pixel Square',monospace;
|
|
--mono:var(--font-zen-mono);
|
|
--sans:var(--font-zen-sans);
|
|
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
|
|
--r:999px; --r-soft:14px;
|
|
}
|
|
*{box-sizing:border-box}
|
|
html{-webkit-text-size-adjust:100%}
|
|
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
|
|
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
|
|
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
|
|
|
|
/* The dot field. A halftone made of one repeating radial-gradient, faded out
|
|
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
|
|
/* No overflow clip here: the dot field is inset to the box already, and a clip
|
|
would cut off the copy-prompt menu. */
|
|
.hero{position:relative;border-bottom:1px solid var(--line)}
|
|
.hero::before{content:"";position:absolute;inset:0;
|
|
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
|
|
background-size:7px 7px;
|
|
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
pointer-events:none}
|
|
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
|
|
|
|
nav{border-bottom:1px solid var(--line)}
|
|
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
|
|
justify-content:space-between;align-items:center;gap:1rem}
|
|
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--ink);text-decoration:none;
|
|
display:inline-flex;align-items:center;gap:.6rem}
|
|
.brand i{font-style:normal;color:var(--faint)}
|
|
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
|
|
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--soft);text-decoration:none}
|
|
nav a.up:hover{color:var(--ink)}
|
|
|
|
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
|
|
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
|
|
h1 em{font-style:normal;color:var(--faint)}
|
|
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
|
|
|
|
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
|
|
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
|
|
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
|
|
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
|
|
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
|
|
line-height:1.2;font-weight:400}
|
|
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
|
|
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
|
|
.btn.solid:hover{background:#d8d8d4}
|
|
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
|
|
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
|
|
|
|
/* Copy-prompt selector. Each row copies the prompt for its own target. */
|
|
.copy{position:relative;display:inline-flex}
|
|
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
|
|
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
|
|
padding:.4rem;display:flex;flex-direction:column;
|
|
box-shadow:0 12px 32px rgba(0,0,0,.75)}
|
|
.menu[hidden]{display:none}
|
|
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
|
|
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
|
|
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
|
|
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
|
|
white-space:nowrap}
|
|
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
|
|
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
|
|
.mi:hover .mark{color:var(--ink)}
|
|
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
|
|
letter-spacing:.1em;text-transform:uppercase}
|
|
|
|
/* Works-with bar: one scrolling row, name and mark per agent. */
|
|
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
|
|
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
|
|
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
|
|
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
|
|
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
|
|
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
|
|
display:flex;align-items:center}
|
|
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
|
|
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
|
|
-webkit-overflow-scrolling:touch;
|
|
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
|
|
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
|
|
.bar::-webkit-scrollbar{display:none}
|
|
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
|
|
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
|
|
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
|
|
text-decoration:none;background:var(--ground)}
|
|
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
|
|
.agent .mark{width:.95rem;height:.95rem}
|
|
|
|
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
|
|
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
|
|
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
|
|
p{margin:0 0 1rem;max-width:70ch}
|
|
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
|
|
li{margin:.22rem 0}
|
|
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
|
|
text-decoration-color:var(--line-hi)}
|
|
a:hover{text-decoration-color:var(--ink)}
|
|
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
|
|
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
|
|
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
|
|
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
|
|
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
|
|
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
|
|
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
|
|
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
|
|
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
|
|
display:block;overflow-x:auto}
|
|
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
|
|
|
|
.curl{display:block;background:var(--panel);border:1px solid var(--line);
|
|
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
|
|
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
|
|
white-space:pre;line-height:1.9}
|
|
.curl b{color:var(--ink);font-weight:400}
|
|
.curl i{font-style:normal;color:var(--faint)}
|
|
|
|
/* index */
|
|
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
|
|
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
|
|
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
|
|
#q:focus{outline:none;border-color:var(--soft)}
|
|
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
|
|
text-transform:uppercase;margin:0 0 2rem}
|
|
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
|
|
padding-bottom:.45rem}
|
|
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
|
|
border-radius:var(--r-soft);text-decoration:none}
|
|
.row:hover{background:#0a0a0a}
|
|
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
|
|
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
|
|
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
|
|
color:var(--faint);font-size:.85rem}
|
|
footer a{color:var(--soft)}
|
|
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
|
|
margin:0 0 2rem;font-size:.85rem}
|
|
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
|
|
border-bottom:1px solid var(--line);padding-bottom:.4rem}
|
|
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
|
|
.fmap a:hover{text-decoration:underline}
|
|
.fmap div{color:var(--faint);line-height:1.5}
|
|
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
|
|
|
|
@media (max-width:640px){
|
|
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
|
|
.btns{gap:.5rem}
|
|
.btn{padding:.75rem 1.15rem;font-size:.68rem}
|
|
.copy,.copy .btn{width:100%;justify-content:center}
|
|
.menu{left:0;right:0;min-width:0}
|
|
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
|
|
.strip b{border-right:0;padding-right:0}
|
|
.bar{width:100%}
|
|
}
|
|
</style>
|
|
<link rel="alternate" type="text/markdown" href="/infrastructure-terraform-patterns.md">
|
|
|
|
<link rel="icon" href="/favicon.svg">
|
|
<link rel="canonical" href="https://hanzoskills.com/infrastructure-terraform-patterns">
|
|
</head>
|
|
<!-- Reading this with a machine? Every page here is also plain markdown.
|
|
This one: https://hanzoskills.com/infrastructure-terraform-patterns.md
|
|
Index: https://hanzoskills.com/llms.txt
|
|
Corpus: https://hanzoskills.com/all.md
|
|
Append .md to any path for the markdown twin. -->
|
|
<body>
|
|
<nav><div class="in">
|
|
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
|
|
<a class="up" href="/">All skills</a>
|
|
</div></nav>
|
|
|
|
<div class="wrap">
|
|
<h1>infrastructure-terraform-patterns</h1><p class="lede">Provisioning cloud infrastructure (AWS, GCP, Azure, Cloudflare)</p><h1>Terraform Patterns</h1>
|
|
<p><strong>Scope</strong>: Infrastructure as Code with Terraform - modules, state management, workspaces, backends <strong>Lines</strong>: 387 <strong>Last Updated</strong>: 2025-10-18 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<hr>
|
|
<h2>When to Use This Skill</h2>
|
|
<p><strong>Activate when</strong>:</p>
|
|
<ul><li>Provisioning cloud infrastructure (AWS, GCP, Azure, Cloudflare)</li><li>Managing infrastructure state across environments</li><li>Creating reusable infrastructure modules</li><li>Implementing infrastructure as code workflows</li><li>Managing multi-environment deployments</li><li>Migrating from manual infrastructure management</li></ul>
|
|
<p><strong>Prerequisites</strong>:</p>
|
|
<ul><li>Terraform CLI installed (<code>brew install terraform</code> or download from terraform.io)</li><li>Cloud provider credentials configured</li><li>Basic understanding of target cloud platform</li><li>Version control system (Git) for state tracking</li></ul>
|
|
<p><strong>Common scenarios</strong>:</p>
|
|
<ul><li>Setting up staging/production environments</li><li>Creating repeatable infrastructure patterns</li><li>Managing infrastructure across multiple regions</li><li>Implementing infrastructure versioning and rollback</li><li>Collaborating on infrastructure changes with teams</li></ul>
|
|
<hr>
|
|
<h2>Core Concepts</h2>
|
|
<h3>1. Provider Configuration</h3>
|
|
<pre><code class="lang-hcl"># versions.tf
|
|
terraform {
|
|
required_version = ">= 1.6"
|
|
|
|
required_providers {
|
|
aws = {
|
|
source = "hashicorp/aws"
|
|
version = "~> 5.0"
|
|
}
|
|
cloudflare = {
|
|
source = "cloudflare/cloudflare"
|
|
version = "~> 4.0"
|
|
}
|
|
}
|
|
|
|
backend "s3" {
|
|
bucket = "my-terraform-state"
|
|
key = "production/terraform.tfstate"
|
|
region = "us-east-1"
|
|
encrypt = true
|
|
dynamodb_table = "terraform-lock"
|
|
}
|
|
}
|
|
|
|
provider "aws" {
|
|
region = var.aws_region
|
|
|
|
default_tags {
|
|
tags = {
|
|
Environment = var.environment
|
|
ManagedBy = "Terraform"
|
|
Project = var.project_name
|
|
}
|
|
}
|
|
}</code></pre>
|
|
<h3>2. Variable Definitions</h3>
|
|
<pre><code class="lang-hcl"># variables.tf
|
|
variable "environment" {
|
|
description = "Environment name (dev, staging, production)"
|
|
type = string
|
|
validation {
|
|
condition = contains(["dev", "staging", "production"], var.environment)
|
|
error_message = "Environment must be dev, staging, or production"
|
|
}
|
|
}
|
|
|
|
variable "aws_region" {
|
|
description = "AWS region for resources"
|
|
type = string
|
|
default = "us-east-1"
|
|
}
|
|
|
|
variable "instance_config" {
|
|
description = "EC2 instance configuration"
|
|
type = object({
|
|
instance_type = string
|
|
volume_size = number
|
|
enable_monitoring = bool
|
|
})
|
|
default = {
|
|
instance_type = "t3.micro"
|
|
volume_size = 20
|
|
enable_monitoring = false
|
|
}
|
|
}
|
|
|
|
variable "tags" {
|
|
description = "Additional tags for resources"
|
|
type = map(string)
|
|
default = {}
|
|
}</code></pre>
|
|
<h3>3. State Management</h3>
|
|
<pre><code class="lang-hcl"># Remote state with S3 backend
|
|
terraform {
|
|
backend "s3" {
|
|
bucket = "company-terraform-state"
|
|
key = "env/${var.environment}/terraform.tfstate"
|
|
region = "us-east-1"
|
|
encrypt = true
|
|
dynamodb_table = "terraform-state-lock"
|
|
|
|
# Enable versioning for state file recovery
|
|
versioning = true
|
|
}
|
|
}
|
|
|
|
# State locking with DynamoDB
|
|
resource "aws_dynamodb_table" "terraform_lock" {
|
|
name = "terraform-state-lock"
|
|
billing_mode = "PAY_PER_REQUEST"
|
|
hash_key = "LockID"
|
|
|
|
attribute {
|
|
name = "LockID"
|
|
type = "S"
|
|
}
|
|
|
|
tags = {
|
|
Name = "Terraform State Lock"
|
|
}
|
|
}</code></pre>
|
|
<h3>4. Data Sources</h3>
|
|
<pre><code class="lang-hcl"># Fetch existing resources
|
|
data "aws_vpc" "main" {
|
|
id = var.vpc_id
|
|
}
|
|
|
|
data "aws_subnets" "private" {
|
|
filter {
|
|
name = "vpc-id"
|
|
values = [data.aws_vpc.main.id]
|
|
}
|
|
|
|
tags = {
|
|
Tier = "private"
|
|
}
|
|
}
|
|
|
|
data "aws_ami" "amazon_linux" {
|
|
most_recent = true
|
|
owners = ["amazon"]
|
|
|
|
filter {
|
|
name = "name"
|
|
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
|
|
}
|
|
}
|
|
|
|
# Remote state data source
|
|
data "terraform_remote_state" "networking" {
|
|
backend = "s3"
|
|
|
|
config = {
|
|
bucket = "company-terraform-state"
|
|
key = "networking/terraform.tfstate"
|
|
region = "us-east-1"
|
|
}
|
|
}</code></pre>
|
|
<hr>
|
|
<h2>Patterns</h2>
|
|
<h3>Module Structure</h3>
|
|
<pre><code class="lang-hcl"># modules/web-app/main.tf
|
|
resource "aws_security_group" "web" {
|
|
name = "${var.name}-web-sg"
|
|
description = "Security group for ${var.name} web tier"
|
|
vpc_id = var.vpc_id
|
|
|
|
ingress {
|
|
from_port = 443
|
|
to_port = 443
|
|
protocol = "tcp"
|
|
cidr_blocks = var.allowed_cidrs
|
|
}
|
|
|
|
egress {
|
|
from_port = 0
|
|
to_port = 0
|
|
protocol = "-1"
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
}
|
|
|
|
tags = merge(
|
|
var.tags,
|
|
{
|
|
Name = "${var.name}-web-sg"
|
|
}
|
|
)
|
|
}
|
|
|
|
resource "aws_lb" "web" {
|
|
name = "${var.name}-alb"
|
|
internal = false
|
|
load_balancer_type = "application"
|
|
security_groups = [aws_security_group.web.id]
|
|
subnets = var.subnet_ids
|
|
|
|
enable_deletion_protection = var.environment == "production"
|
|
|
|
tags = merge(
|
|
var.tags,
|
|
{
|
|
Name = "${var.name}-alb"
|
|
}
|
|
)
|
|
}
|
|
|
|
# modules/web-app/variables.tf
|
|
variable "name" {
|
|
description = "Application name"
|
|
type = string
|
|
}
|
|
|
|
variable "environment" {
|
|
description = "Environment name"
|
|
type = string
|
|
}
|
|
|
|
variable "vpc_id" {
|
|
description = "VPC ID"
|
|
type = string
|
|
}
|
|
|
|
variable "subnet_ids" {
|
|
description = "Subnet IDs for load balancer"
|
|
type = list(string)
|
|
}
|
|
|
|
variable "allowed_cidrs" {
|
|
description = "Allowed CIDR blocks for ingress"
|
|
type = list(string)
|
|
default = ["0.0.0.0/0"]
|
|
}
|
|
|
|
variable "tags" {
|
|
description = "Additional tags"
|
|
type = map(string)
|
|
default = {}
|
|
}
|
|
|
|
# modules/web-app/outputs.tf
|
|
output "alb_dns_name" {
|
|
description = "DNS name of the load balancer"
|
|
value = aws_lb.web.dns_name
|
|
}
|
|
|
|
output "alb_arn" {
|
|
description = "ARN of the load balancer"
|
|
value = aws_lb.web.arn
|
|
}
|
|
|
|
output "security_group_id" {
|
|
description = "Security group ID"
|
|
value = aws_security_group.web.id
|
|
}</code></pre>
|
|
<h3>Using Modules</h3>
|
|
<pre><code class="lang-hcl"># main.tf
|
|
module "web_app" {
|
|
source = "./modules/web-app"
|
|
|
|
name = "my-app"
|
|
environment = var.environment
|
|
vpc_id = data.aws_vpc.main.id
|
|
subnet_ids = data.aws_subnets.public.ids
|
|
|
|
allowed_cidrs = var.environment == "production" ? var.allowed_cidrs : ["0.0.0.0/0"]
|
|
|
|
tags = {
|
|
Team = "Platform"
|
|
Owner = "DevOps"
|
|
}
|
|
}
|
|
|
|
# Use module outputs
|
|
resource "aws_route53_record" "web" {
|
|
zone_id = var.route53_zone_id
|
|
name = "app.${var.domain}"
|
|
type = "A"
|
|
|
|
alias {
|
|
name = module.web_app.alb_dns_name
|
|
zone_id = module.web_app.alb_zone_id
|
|
evaluate_target_health = true
|
|
}
|
|
}</code></pre>
|
|
<h3>Workspace Management</h3>
|
|
<pre><code class="lang-bash"># Create workspaces for environments
|
|
terraform workspace new dev
|
|
terraform workspace new staging
|
|
terraform workspace new production
|
|
|
|
# Switch between workspaces
|
|
terraform workspace select dev
|
|
|
|
# List workspaces
|
|
terraform workspace list
|
|
|
|
# Use workspace in configuration
|
|
locals {
|
|
environment = terraform.workspace
|
|
|
|
instance_counts = {
|
|
dev = 1
|
|
staging = 2
|
|
production = 5
|
|
}
|
|
|
|
instance_count = local.instance_counts[terraform.workspace]
|
|
}
|
|
|
|
resource "aws_instance" "app" {
|
|
count = local.instance_count
|
|
ami = data.aws_ami.amazon_linux.id
|
|
instance_type = var.instance_type
|
|
|
|
tags = {
|
|
Name = "${terraform.workspace}-app-${count.index + 1}"
|
|
Environment = terraform.workspace
|
|
}
|
|
}</code></pre>
|
|
<h3>Dynamic Blocks</h3>
|
|
<pre><code class="lang-hcl"># Dynamic ingress rules
|
|
variable "ingress_rules" {
|
|
type = list(object({
|
|
from_port = number
|
|
to_port = number
|
|
protocol = string
|
|
cidr_blocks = list(string)
|
|
description = string
|
|
}))
|
|
default = [
|
|
{
|
|
from_port = 443
|
|
to_port = 443
|
|
protocol = "tcp"
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
description = "HTTPS"
|
|
},
|
|
{
|
|
from_port = 80
|
|
to_port = 80
|
|
protocol = "tcp"
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
description = "HTTP"
|
|
}
|
|
]
|
|
}
|
|
|
|
resource "aws_security_group" "dynamic" {
|
|
name = "dynamic-sg"
|
|
description = "Security group with dynamic rules"
|
|
vpc_id = var.vpc_id
|
|
|
|
dynamic "ingress" {
|
|
for_each = var.ingress_rules
|
|
content {
|
|
from_port = ingress.value.from_port
|
|
to_port = ingress.value.to_port
|
|
protocol = ingress.value.protocol
|
|
cidr_blocks = ingress.value.cidr_blocks
|
|
description = ingress.value.description
|
|
}
|
|
}
|
|
}</code></pre>
|
|
<h3>For Each Patterns</h3>
|
|
<pre><code class="lang-hcl"># Create resources from map
|
|
variable "s3_buckets" {
|
|
type = map(object({
|
|
versioning = bool
|
|
encryption = bool
|
|
}))
|
|
default = {
|
|
"logs" = {
|
|
versioning = true
|
|
encryption = true
|
|
}
|
|
"backups" = {
|
|
versioning = true
|
|
encryption = true
|
|
}
|
|
"assets" = {
|
|
versioning = false
|
|
encryption = false
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "aws_s3_bucket" "buckets" {
|
|
for_each = var.s3_buckets
|
|
|
|
bucket = "${var.project}-${each.key}-${var.environment}"
|
|
|
|
tags = {
|
|
Name = each.key
|
|
Type = each.key
|
|
}
|
|
}
|
|
|
|
resource "aws_s3_bucket_versioning" "buckets" {
|
|
for_each = {
|
|
for k, v in var.s3_buckets : k => v if v.versioning
|
|
}
|
|
|
|
bucket = aws_s3_bucket.buckets[each.key].id
|
|
|
|
versioning_configuration {
|
|
status = "Enabled"
|
|
}
|
|
}</code></pre>
|
|
<h3>Conditional Resources</h3>
|
|
<pre><code class="lang-hcl"># Create resource only in production
|
|
resource "aws_cloudwatch_dashboard" "main" {
|
|
count = var.environment == "production" ? 1 : 0
|
|
dashboard_name = "${var.project}-dashboard"
|
|
|
|
dashboard_body = jsonencode({
|
|
widgets = [
|
|
{
|
|
type = "metric"
|
|
properties = {
|
|
metrics = [
|
|
["AWS/Lambda", "Invocations"]
|
|
]
|
|
}
|
|
}
|
|
]
|
|
})
|
|
}
|
|
|
|
# Conditional values
|
|
locals {
|
|
instance_type = var.environment == "production" ? "t3.large" : "t3.micro"
|
|
|
|
backup_retention = {
|
|
dev = 7
|
|
staging = 14
|
|
production = 30
|
|
}[var.environment]
|
|
}</code></pre>
|
|
<hr>
|
|
<h2>Quick Reference</h2>
|
|
<h3>Essential Commands</h3>
|
|
<pre><code class="lang-bash"># Initialize Terraform
|
|
terraform init
|
|
|
|
# Validate configuration
|
|
terraform validate
|
|
|
|
# Format configuration
|
|
terraform fmt -recursive
|
|
|
|
# Plan changes
|
|
terraform plan -out=tfplan
|
|
|
|
# Apply changes
|
|
terraform apply tfplan
|
|
|
|
# Destroy infrastructure
|
|
terraform destroy
|
|
|
|
# Show current state
|
|
terraform show
|
|
|
|
# List resources in state
|
|
terraform state list
|
|
|
|
# Remove resource from state
|
|
terraform state rm aws_instance.example
|
|
|
|
# Import existing resource
|
|
terraform import aws_instance.example i-1234567890abcdef0
|
|
|
|
# Refresh state
|
|
terraform refresh
|
|
|
|
# Output values
|
|
terraform output
|
|
terraform output -json</code></pre>
|
|
<h3>State Management</h3>
|
|
<pre><code class="lang-bash"># Pull remote state
|
|
terraform state pull > terraform.tfstate.backup
|
|
|
|
# Push local state to remote
|
|
terraform state push terraform.tfstate
|
|
|
|
# Move resource in state
|
|
terraform state mv aws_instance.old aws_instance.new
|
|
|
|
# Replace provider in state
|
|
terraform state replace-provider hashicorp/aws registry.terraform.io/hashicorp/aws</code></pre>
|
|
<h3>Workspace Commands</h3>
|
|
<pre><code class="lang-bash"># Create workspace
|
|
terraform workspace new staging
|
|
|
|
# Select workspace
|
|
terraform workspace select production
|
|
|
|
# List workspaces
|
|
terraform workspace list
|
|
|
|
# Show current workspace
|
|
terraform workspace show
|
|
|
|
# Delete workspace
|
|
terraform workspace delete dev</code></pre>
|
|
<h3>Module Commands</h3>
|
|
<pre><code class="lang-bash"># Initialize and download modules
|
|
terraform init
|
|
|
|
# Update modules
|
|
terraform get -update
|
|
|
|
# Show module tree
|
|
terraform providers</code></pre>
|
|
<hr>
|
|
<h2>Anti-Patterns</h2>
|
|
<h3>Critical Violations</h3>
|
|
<pre><code class="lang-hcl"># ❌ NEVER: Hardcode credentials
|
|
provider "aws" {
|
|
access_key = "AKIAIOSFODNN7EXAMPLE" # NEVER do this
|
|
secret_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
|
|
}
|
|
|
|
# ✅ CORRECT: Use environment variables or instance profiles
|
|
provider "aws" {
|
|
# Uses AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
|
|
region = var.aws_region
|
|
}</code></pre>
|
|
<pre><code class="lang-hcl"># ❌ NEVER: Store state locally for team projects
|
|
terraform {
|
|
# No backend configuration - uses local state
|
|
}
|
|
|
|
# ✅ CORRECT: Use remote backend with locking
|
|
terraform {
|
|
backend "s3" {
|
|
bucket = "terraform-state"
|
|
key = "prod/terraform.tfstate"
|
|
region = "us-east-1"
|
|
encrypt = true
|
|
dynamodb_table = "terraform-lock"
|
|
}
|
|
}</code></pre>
|
|
<pre><code class="lang-hcl"># ❌ NEVER: Use count for resources that might be reordered
|
|
resource "aws_instance" "app" {
|
|
count = 3 # Risky - reordering changes indices
|
|
# ...
|
|
}
|
|
|
|
# ✅ CORRECT: Use for_each with stable keys
|
|
resource "aws_instance" "app" {
|
|
for_each = toset(["web-1", "web-2", "web-3"])
|
|
|
|
tags = {
|
|
Name = each.key
|
|
}
|
|
}</code></pre>
|
|
<h3>Common Mistakes</h3>
|
|
<pre><code class="lang-hcl"># ❌ Don't ignore .terraform directory in git
|
|
# .gitignore missing .terraform/
|
|
|
|
# ✅ CORRECT: Proper .gitignore
|
|
# .gitignore
|
|
.terraform/
|
|
*.tfstate
|
|
*.tfstate.backup
|
|
.terraform.lock.hcl # or commit for version locking
|
|
tfplan</code></pre>
|
|
<pre><code class="lang-hcl"># ❌ Don't create circular dependencies
|
|
resource "aws_security_group" "a" {
|
|
ingress {
|
|
security_groups = [aws_security_group.b.id]
|
|
}
|
|
}
|
|
|
|
resource "aws_security_group" "b" {
|
|
ingress {
|
|
security_groups = [aws_security_group.a.id]
|
|
}
|
|
}
|
|
|
|
# ✅ CORRECT: Use separate rules
|
|
resource "aws_security_group_rule" "a_to_b" {
|
|
type = "ingress"
|
|
security_group_id = aws_security_group.b.id
|
|
source_security_group_id = aws_security_group.a.id
|
|
from_port = 443
|
|
to_port = 443
|
|
protocol = "tcp"
|
|
}</code></pre>
|
|
<h3>Best Practices</h3>
|
|
<pre><code class="lang-hcl"># Use consistent naming
|
|
locals {
|
|
name_prefix = "${var.project}-${var.environment}"
|
|
|
|
common_tags = {
|
|
Project = var.project
|
|
Environment = var.environment
|
|
ManagedBy = "Terraform"
|
|
Timestamp = timestamp()
|
|
}
|
|
}
|
|
|
|
# Version pin providers
|
|
terraform {
|
|
required_providers {
|
|
aws = {
|
|
source = "hashicorp/aws"
|
|
version = "~> 5.0" # Allow minor updates only
|
|
}
|
|
}
|
|
}
|
|
|
|
# Use lifecycle rules for safety
|
|
resource "aws_db_instance" "main" {
|
|
# ...
|
|
|
|
lifecycle {
|
|
prevent_destroy = true # Prevent accidental deletion
|
|
create_before_destroy = true # Zero-downtime updates
|
|
ignore_changes = [
|
|
tags["LastModified"] # Ignore external changes
|
|
]
|
|
}
|
|
}</code></pre>
|
|
<hr>
|
|
<h2>Level 3: Resources</h2>
|
|
<p><strong>Location</strong>: <code>~/.hanzo/skills/infrastructure/terraform-best-practices/resources/</code></p>
|
|
<h3>Comprehensive Reference</h3>
|
|
<p><strong><code>REFERENCE.md</code></strong> (1,875 lines)</p>
|
|
<ul><li>Complete Terraform best practices guide</li><li>State management strategies and troubleshooting</li><li>Module design patterns and composition</li><li>Variable management and validation</li><li>Security best practices and encryption</li><li>Cost optimization techniques</li><li>CI/CD integration patterns</li><li>Multi-environment strategies</li><li>Performance optimization</li><li>Disaster recovery procedures</li></ul>
|
|
<h3>Executable Scripts</h3>
|
|
<p><strong><code>scripts/analyze_terraform.py</code></strong> - Terraform code analyzer</p>
|
|
<ul><li>Detects security vulnerabilities and anti-patterns</li><li>Checks for hardcoded secrets, unencrypted resources</li><li>Validates naming conventions and tagging</li><li>Identifies overly permissive IAM policies and security groups</li><li>Suggests improvements and best practices</li><li>Supports JSON and table output formats</li></ul>
|
|
<pre><code class="lang-bash">./analyze_terraform.py .
|
|
./analyze_terraform.py --json --output report.json terraform/
|
|
./analyze_terraform.py --severity high --format table .</code></pre>
|
|
<p><strong><code>scripts/validate_state.py</code></strong> - State file health checker</p>
|
|
<ul><li>Validates state file integrity and structure</li><li>Detects orphaned resources and duplicates</li><li>Checks for circular dependencies</li><li>Analyzes state lineage and serial numbers</li><li>Identifies sensitive data exposure</li><li>Optionally checks for infrastructure drift</li></ul>
|
|
<pre><code class="lang-bash">./validate_state.py
|
|
./validate_state.py --remote --json
|
|
./validate_state.py --check-drift --output state-report.json</code></pre>
|
|
<p><strong><code>scripts/cost_estimate.sh</code></strong> - Infrastructure cost estimator</p>
|
|
<ul><li>Estimates monthly costs from Terraform plans</li><li>Integrates with Infracost for accurate pricing</li><li>Provides cost breakdowns by resource type</li><li>Compares costs across environments</li><li>Supports budget limits and threshold alerts</li><li>Outputs in table, JSON, or CSV formats</li></ul>
|
|
<pre><code class="lang-bash">./cost_estimate.sh
|
|
./cost_estimate.sh --json --output costs.json
|
|
./cost_estimate.sh --budget 5000 --threshold 80
|
|
./cost_estimate.sh --compare production-costs.json</code></pre>
|
|
<h3>Production Examples</h3>
|
|
<p><strong>Reusable Modules</strong>:</p>
|
|
<ul><li><code>examples/modules/vpc/</code> - Production-ready VPC with multi-AZ, NAT gateways, flow logs</li><li><code>examples/modules/eks/</code> - EKS cluster with node groups and IRSA</li><li><code>examples/modules/rds/</code> - RDS with encryption, backups, monitoring</li></ul>
|
|
<p><strong>Environment Configurations</strong>:</p>
|
|
<ul><li><code>examples/environments/dev/</code> - Development environment (cost-optimized)</li><li><code>examples/environments/prod/</code> - Production environment (HA, redundant)</li></ul>
|
|
<p><strong>Best Practices Examples</strong>:</p>
|
|
<ul><li><code>examples/good-practices/security.tf</code> - Encryption, least privilege IAM, secure networking</li><li><code>examples/bad-practices/anti-patterns.tf</code> - Common mistakes with explanations and fixes</li></ul>
|
|
<h3>Usage</h3>
|
|
<ol><li><strong>Code Quality Analysis</strong>:</li></ol>
|
|
<p> ``<code>bash cd /path/to/terraform/project /path/to/analyze_terraform.py . --severity high </code>``</p>
|
|
<ol><li><strong>State Validation</strong>:</li></ol>
|
|
<p> ``<code>bash cd /path/to/terraform/project /path/to/validate_state.py --check-drift </code>``</p>
|
|
<ol><li><strong>Cost Estimation</strong>:</li></ol>
|
|
<p> ``<code>bash cd /path/to/terraform/project /path/to/cost_estimate.sh --budget 10000 --json </code>``</p>
|
|
<ol><li><strong>Module Reuse</strong>:</li></ol>
|
|
<p> ```hcl module "vpc" { source = "/path/to/terraform-best-practices/resources/examples/modules/vpc"</p>
|
|
<p> name = "production" cidr_block = "10.0.0.0/16" availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c"] } ```</p>
|
|
<p>All scripts support <code>--help</code> for detailed usage information.</p>
|
|
<hr>
|
|
<h2>Related Skills</h2>
|
|
<p><strong>Infrastructure</strong>:</p>
|
|
<ul><li><code>aws-serverless.md</code> - AWS Lambda, API Gateway, DynamoDB integration</li><li><code>kubernetes-basics.md</code> - Container orchestration with Terraform Kubernetes provider</li><li><code>infrastructure-security.md</code> - IAM roles, security groups, secrets management</li><li><code>cost-optimization.md</code> - Resource sizing, reserved instances</li></ul>
|
|
<p><strong>Development</strong>:</p>
|
|
<ul><li><code>modal-functions-basics.md</code> - Alternative serverless platform</li><li><code>cloudflare-workers.md</code> - Edge computing with Terraform Cloudflare provider</li></ul>
|
|
<p><strong>Standards from CLAUDE.md</strong>:</p>
|
|
<ul><li>Use Terraform for infrastructure as code</li><li>Remote state with S3 backend mandatory for team projects</li><li>Version pin all providers</li><li>Tag all resources consistently</li><li>Enable state locking with DynamoDB</li></ul>
|
|
<hr>
|
|
<p><strong>Last Updated</strong>: 2025-10-18 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<footer>
|
|
<div class="fmap">
|
|
<div><b>Build</b>
|
|
<a href="https://hanzo.app">hanzo.app</a> app builder
|
|
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
|
|
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
|
|
<div><b>Run</b>
|
|
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
|
|
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
|
|
<div><b>Account</b>
|
|
<a href="https://hanzo.id">hanzo.id</a> sign in
|
|
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
|
|
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
|
|
<div><b>Company</b>
|
|
<a href="https://hanzo.ai">hanzo.ai</a> canonical
|
|
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
|
|
</div>
|
|
<div class="fend"><a href="/infrastructure-terraform-patterns.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
|
</footer>
|
|
</div></body></html> |