sisyo 1.7.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +16 -16
- package/README.md +167 -166
- package/bin/known-hashes.json +6 -1
- package/bin/sisyo.js +285 -285
- package/package.json +30 -30
- package/templates/.claude/rules/agents-and-tests.md +32 -24
- package/templates/.claude/rules/docs.md +19 -19
- package/templates/.claude/skills/fastapi-best-practices/ASYNC-PATTERNS.md +121 -121
- package/templates/.claude/skills/fastapi-best-practices/DATABASE.md +153 -153
- package/templates/.claude/skills/fastapi-best-practices/DEPENDENCIES.md +140 -140
- package/templates/.claude/skills/fastapi-best-practices/PROJECT-STRUCTURE.md +90 -90
- package/templates/.claude/skills/fastapi-best-practices/PYDANTIC-GUIDE.md +155 -155
- package/templates/.claude/skills/fastapi-best-practices/SKILL.md +123 -123
- package/templates/.claude/skills/g6-style/SKILL.md +125 -125
- package/templates/.claude/skills/g6-style/references/checks.md +58 -58
- package/templates/.claude/skills/g6-style/references/imagery.md +103 -103
- package/templates/.claude/skills/g6-style/references/motion.md +121 -121
- package/templates/.claude/skills/g6-style/references/sections.md +263 -263
- package/templates/.claude/skills/g6-style/references/tokens.md +197 -197
- package/templates/.claude/skills/hindsight/SKILL.md +175 -175
- package/templates/.claude/skills/hoang-auth/SKILL.md +188 -0
- package/templates/.claude/skills/html-docs/SKILL.md +207 -207
- package/templates/.claude/skills/i18n-multilang/SKILL.md +153 -153
- package/templates/.claude/skills/i18n-multilang/assets/languages.json +47 -47
- package/templates/.claude/skills/i18n-multilang/assets/plural-vectors.json +46 -46
- package/templates/.claude/skills/i18n-multilang/assets/project/README.md +17 -17
- package/templates/.claude/skills/i18n-multilang/assets/project/gitattributes.txt +6 -6
- package/templates/.claude/skills/i18n-multilang/assets/project/gitignore.txt +4 -4
- package/templates/.claude/skills/i18n-multilang/assets/project/glossary.json +6 -6
- package/templates/.claude/skills/i18n-multilang/assets/project/i18n.config.json +22 -22
- package/templates/.claude/skills/i18n-multilang/assets/project/locales/__source__/common.json +10 -10
- package/templates/.claude/skills/i18n-multilang/assets/project/notes/common.json +5 -5
- package/templates/.claude/skills/i18n-multilang/assets/project/style.md +20 -20
- package/templates/.claude/skills/i18n-multilang/references/format.md +100 -100
- package/templates/.claude/skills/i18n-multilang/references/languages.md +97 -97
- package/templates/.claude/skills/i18n-multilang/references/layout-and-versioning.md +256 -256
- package/templates/.claude/skills/i18n-multilang/references/numbers.md +161 -161
- package/templates/.claude/skills/i18n-multilang/references/platform-unity.md +142 -142
- package/templates/.claude/skills/i18n-multilang/references/platform-web.md +107 -107
- package/templates/.claude/skills/i18n-multilang/references/translation.md +143 -143
- package/templates/.claude/skills/i18n-multilang/references/voice.md +271 -271
- package/templates/.claude/skills/i18n-multilang/scripts/i18n.py +1041 -1041
- package/templates/.claude/skills/i18n-multilang/scripts/numwords.py +1013 -1013
- package/templates/.claude/skills/i18n-multilang/scripts/voice.py +478 -478
- package/templates/.claude/skills/neon-game-ui-ux-skill/COMPONENTS.md +490 -490
- package/templates/.claude/skills/neon-game-ui-ux-skill/GAMIFICATION.md +246 -246
- package/templates/.claude/skills/neon-game-ui-ux-skill/ICONS.md +249 -249
- package/templates/.claude/skills/neon-game-ui-ux-skill/LAYOUT-NAV.md +362 -362
- package/templates/.claude/skills/neon-game-ui-ux-skill/MOTION-A11Y.md +168 -168
- package/templates/.claude/skills/neon-game-ui-ux-skill/SKILL.md +137 -137
- package/templates/.claude/skills/neon-game-ui-ux-skill/TOKENS.md +323 -323
- package/templates/.claude/skills/react-page-oriented/SKILL.md +407 -407
- package/templates/.claude/skills/react-page-oriented/triggers.md +12 -12
- package/templates/.claude/skills/terraform-best-practices/SKILL.md +341 -341
- package/templates/.claude/skills/typesafe-jev/SKILL.md +148 -148
- package/templates/.claude/skills/vibe-docs/SKILL.md +242 -242
- package/templates/SISYO.md +37 -37
- package/templates/docs/99_progress/features.md +7 -7
- package/templates/docs/99_progress/handoff.md +19 -19
- package/templates/docs/99_progress/todo.md +15 -15
- package/templates/docs/MAP.md +26 -26
|
@@ -1,341 +1,341 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: terraform-best-practices
|
|
3
|
-
description: "Terraform/AWS workflow rules: never auto-apply, always confirm before high-cost resources (Private CA ~$400, NAT GW, Aurora, KMS…), plan summary with add/change/destroy + drift, reusable module layout (env/<env>/<service>.tf + modules/<svc>/<component>.tf, network module groups VPC/SG/SGR/NAT), lifecycle prevent_destroy on stateful resources (Aurora, S3, KMS, SSM), no plaintext secrets (random_password + SSM SecureString), least-privilege IAM, SGR must justify who/from/to/protocol/why, naming {env}-{service}-{purpose}[-{version}], common tags via default_tags, minimal outputs. Triggers on .tf edits and on user saying 'check' / 'plan' / 'validate'."
|
|
4
|
-
license: MIT
|
|
5
|
-
metadata:
|
|
6
|
-
author: HoangThang
|
|
7
|
-
version: '1.0.0'
|
|
8
|
-
---
|
|
9
|
-
|
|
10
|
-
# Terraform Best Practices
|
|
11
|
-
|
|
12
|
-
Team rules for Terraform on AWS. Read these before editing any `.tf` file.
|
|
13
|
-
|
|
14
|
-
## When to trigger
|
|
15
|
-
|
|
16
|
-
- Editing any `.tf` / `.tfvars` file
|
|
17
|
-
- User says **"check"**, **"plan"**, **"validate"**, **"fmt"**
|
|
18
|
-
- Creating or modifying infrastructure
|
|
19
|
-
- Reviewing drift, cost, or security of cloud resources
|
|
20
|
-
|
|
21
|
-
## CRITICAL — never apply
|
|
22
|
-
|
|
23
|
-
**Do NOT run `terraform apply`. Do NOT suggest the user run it.** Apply is always the user's decision, on the user's machine, with their credentials. Agent only edits files and runs read-only commands (`fmt`, `validate`, `plan`, `show`, `state list`, `output`).
|
|
24
|
-
|
|
25
|
-
If user explicitly types "apply" / "run apply" → still pause and ask once to confirm, then let them run it themselves unless they insist.
|
|
26
|
-
|
|
27
|
-
## High-cost resources — confirm before writing
|
|
28
|
-
|
|
29
|
-
Stop and ask the user before creating any of these. Quote the rough monthly cost so they can decide:
|
|
30
|
-
|
|
31
|
-
| Resource | Rough cost | Why expensive |
|
|
32
|
-
|---|---|---|
|
|
33
|
-
| `aws_acmpca_certificate_authority` | ~$400/mo per CA | flat monthly fee |
|
|
34
|
-
| `aws_nat_gateway` | ~$35/mo + data | per AZ, per GB |
|
|
35
|
-
| `aws_transit_gateway` | ~$36/mo + attach + data | per attachment too |
|
|
36
|
-
| `aws_rds_cluster` (Aurora) | varies, often >$100/mo | per ACU/hour |
|
|
37
|
-
| `aws_elasticache_*` | varies | per node-hour |
|
|
38
|
-
| `aws_opensearch_domain` | varies | per node-hour + storage |
|
|
39
|
-
| `aws_vpc_endpoint` (Interface) | ~$7/mo per endpoint per AZ | per ENI-hour |
|
|
40
|
-
| `aws_kms_key` (customer-managed) | $1/mo + req | adds up across services |
|
|
41
|
-
| `aws_eks_cluster` | $73/mo control plane | + node costs |
|
|
42
|
-
| `aws_msk_cluster` | varies, often >$200/mo | per broker-hour |
|
|
43
|
-
| `aws_directconnect_*` | $$$ | port + data |
|
|
44
|
-
| `aws_globalaccelerator_*` | ~$18/mo + data | flat + traffic |
|
|
45
|
-
|
|
46
|
-
Pattern: list what will be created → estimated monthly cost → wait for explicit OK. Don't write the `.tf` until confirmed.
|
|
47
|
-
|
|
48
|
-
## The "check" command
|
|
49
|
-
|
|
50
|
-
When user says **"check"**, run sequentially and report:
|
|
51
|
-
|
|
52
|
-
1. `terraform fmt -recursive` — fix formatting, list any files changed
|
|
53
|
-
2. `terraform validate` — surface syntax/type errors
|
|
54
|
-
3. `terraform plan -no-color` — capture the plan
|
|
55
|
-
|
|
56
|
-
Then summarize the plan in this format:
|
|
57
|
-
|
|
58
|
-
```
|
|
59
|
-
PLAN SUMMARY
|
|
60
|
-
|
|
61
|
-
ADD (N): <type.name> × N — list types
|
|
62
|
-
CHANGE (N): <type.name> — flag any in-place change that recreates / drops data
|
|
63
|
-
DESTROY (N): <type.name> — flag DANGER for aurora, s3, kms, ssm, dynamodb
|
|
64
|
-
DRIFT (N): <type.name> — suggest fix (terraform import / update .tf / ignore_changes)
|
|
65
|
-
|
|
66
|
-
NOTES:
|
|
67
|
-
- High-cost adds (if any)
|
|
68
|
-
- Destructive actions on stateful resources
|
|
69
|
-
- Resources missing prevent_destroy that should have it
|
|
70
|
-
- IAM widening (Resource = "*", new wildcard actions)
|
|
71
|
-
- New SGR without description or with 0.0.0.0/0 ingress
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
Keep it under 30 lines. Link to the full plan output if user wants more.
|
|
75
|
-
|
|
76
|
-
## Project layout — reusable modules
|
|
77
|
-
|
|
78
|
-
```
|
|
79
|
-
infra/
|
|
80
|
-
env/
|
|
81
|
-
dev/
|
|
82
|
-
providers.tf # provider + default_tags
|
|
83
|
-
backend.tf # remote state config
|
|
84
|
-
variables.tf
|
|
85
|
-
terraform.tfvars
|
|
86
|
-
network.tf # calls modules/network
|
|
87
|
-
ec2.tf # calls modules/ec2 (one file per service)
|
|
88
|
-
rds.tf
|
|
89
|
-
s3.tf
|
|
90
|
-
kms.tf
|
|
91
|
-
iam.tf # env-level roles only
|
|
92
|
-
staging/
|
|
93
|
-
prod/
|
|
94
|
-
modules/
|
|
95
|
-
network/ # VPC + subnets + SG + SGR + NAT + route tables
|
|
96
|
-
vpc.tf
|
|
97
|
-
subnets.tf
|
|
98
|
-
sg.tf
|
|
99
|
-
sgr.tf
|
|
100
|
-
nat.tf
|
|
101
|
-
route_tables.tf
|
|
102
|
-
variables.tf
|
|
103
|
-
outputs.tf
|
|
104
|
-
ec2/ # module groups everything the EC2 service needs
|
|
105
|
-
main.tf # instance / launch template / ASG
|
|
106
|
-
iam.tf # instance profile + role + policy
|
|
107
|
-
sg.tf # service-specific SG
|
|
108
|
-
s3.tf # (optional) bucket the app reads/writes
|
|
109
|
-
ssm.tf # (optional) params the app loads at boot
|
|
110
|
-
kms.tf # (optional) key used to encrypt its own data
|
|
111
|
-
variables.tf
|
|
112
|
-
outputs.tf
|
|
113
|
-
rds/
|
|
114
|
-
network/
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
Rules — guidelines, not laws:
|
|
118
|
-
|
|
119
|
-
- **One `.tf` per service** at the env level (`ec2.tf`, `rds.tf`, …). Easier to grep, easier to PR.
|
|
120
|
-
- **Inside a module, split files by component**, not by AWS service category. The point is readability — keep each file focused on one concern of the module.
|
|
121
|
-
- **A module owns the resources its task needs.** If the `ec2` module's app reads from an S3 bucket and loads SSM params, that bucket and those params live **inside** `modules/ec2/` (`s3.tf`, `ssm.tf`). Don't force them out into a separate `modules/s3/` just because they're S3 — separation by AWS service is artificial.
|
|
122
|
-
- **When to extract a separate module instead:** the resource is genuinely shared by 2+ unrelated callers, has its own lifecycle, or belongs to a different team's domain. Otherwise keep it co-located with the consumer.
|
|
123
|
-
- **Network module aggregates** VPC, subnets, SG, SGR, NAT, route tables — because everyone consumes its outputs. Other modules call into it for `vpc_id`, `subnet_ids`, base `sg_ids`.
|
|
124
|
-
- Module must be **env-agnostic** — env-specific values come in as variables.
|
|
125
|
-
- No `count` / `for_each` gymnastics in the env layer when a separate file would be clearer.
|
|
126
|
-
|
|
127
|
-
Heuristic for "module or env file?": if the resource is only meaningful **with** the service (its IAM role, its bucket, its SSM param), put it in the module. If it's a foundational layer that many services share (VPC, central KMS key, shared S3 data lake), put it in its own module or at the env layer.
|
|
128
|
-
|
|
129
|
-
## Lifecycle — prevent destroy on stateful
|
|
130
|
-
|
|
131
|
-
Always add to data-bearing or hard-to-recreate resources:
|
|
132
|
-
|
|
133
|
-
```hcl
|
|
134
|
-
lifecycle {
|
|
135
|
-
prevent_destroy = true
|
|
136
|
-
}
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
Required on:
|
|
140
|
-
- `aws_rds_cluster`, `aws_db_instance` (Aurora/RDS)
|
|
141
|
-
- `aws_s3_bucket`
|
|
142
|
-
- `aws_kms_key`, `aws_kms_alias`
|
|
143
|
-
- `aws_ssm_parameter` (SecureString)
|
|
144
|
-
- `aws_dynamodb_table` (if used as primary store)
|
|
145
|
-
- `aws_acmpca_certificate_authority`
|
|
146
|
-
- `aws_efs_file_system`, `aws_backup_vault`
|
|
147
|
-
|
|
148
|
-
For volatile attributes that change outside Terraform, also use `ignore_changes` (e.g. autoscaling-managed desired_capacity).
|
|
149
|
-
|
|
150
|
-
## Secrets — no plaintext, ever
|
|
151
|
-
|
|
152
|
-
❌ Never put real secrets in `.tf` or `.tfvars`. Never commit to git.
|
|
153
|
-
|
|
154
|
-
✅ Pattern: generate with `random_password`, store in SSM SecureString or Secrets Manager, reference via data source or attribute.
|
|
155
|
-
|
|
156
|
-
```hcl
|
|
157
|
-
resource "random_password" "rds_master" {
|
|
158
|
-
length = 32
|
|
159
|
-
special = true
|
|
160
|
-
min_special = 4
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
resource "aws_ssm_parameter" "rds_master" {
|
|
164
|
-
name = "/${var.env}/rds/${var.service}/master-password"
|
|
165
|
-
type = "SecureString"
|
|
166
|
-
key_id = aws_kms_key.ssm.arn
|
|
167
|
-
value = random_password.rds_master.result
|
|
168
|
-
|
|
169
|
-
lifecycle { prevent_destroy = true }
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
resource "aws_rds_cluster" "main" {
|
|
173
|
-
master_password = random_password.rds_master.result
|
|
174
|
-
# ...
|
|
175
|
-
}
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
For rotation: prefer **Secrets Manager** with `rotation_lambda_arn`. For static lookups from existing secrets: `data "aws_ssm_parameter"` with `with_decryption = true`.
|
|
179
|
-
|
|
180
|
-
## IAM — least privilege
|
|
181
|
-
|
|
182
|
-
- Start with the **minimum action set**. When AccessDenied appears in logs, add the specific action.
|
|
183
|
-
- Prefer **explicit resource ARNs** over `Resource = "*"`. Wildcards only when AWS itself requires (e.g. some `iam:PassRole` cases).
|
|
184
|
-
- Prefer **AWS-managed policies** only for well-known service roles (`AmazonSSMManagedInstanceCore`, `AmazonEC2ContainerRegistryReadOnly`).
|
|
185
|
-
- Custom policies > inline policies (easier to reuse and review).
|
|
186
|
-
- Use **conditions** when scope can be tightened: `aws:SourceVpc`, `aws:SourceArn`, `StringEquals: aws:ResourceTag/...`.
|
|
187
|
-
- Separate **trust policy** from **permission policy** clearly.
|
|
188
|
-
|
|
189
|
-
## Security group rules — must justify
|
|
190
|
-
|
|
191
|
-
Every `aws_security_group_rule` (or inline `ingress`/`egress`) must have a `description` answering all 5:
|
|
192
|
-
|
|
193
|
-
1. **Who** calls (source service/team)
|
|
194
|
-
2. **From where** (CIDR or source sg)
|
|
195
|
-
3. **To what port** / port range
|
|
196
|
-
4. **Protocol** (tcp/udp/icmp)
|
|
197
|
-
5. **Why** it's needed
|
|
198
|
-
|
|
199
|
-
```hcl
|
|
200
|
-
resource "aws_security_group_rule" "api_to_rds" {
|
|
201
|
-
type = "ingress"
|
|
202
|
-
from_port = 5432
|
|
203
|
-
to_port = 5432
|
|
204
|
-
protocol = "tcp"
|
|
205
|
-
security_group_id = module.network.sg_rds_id
|
|
206
|
-
source_security_group_id = module.ec2_api.sg_id
|
|
207
|
-
description = "api service (api-sg) -> rds postgres on tcp/5432 — app reads/writes user data"
|
|
208
|
-
}
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
Red flags to call out in plan summary:
|
|
212
|
-
- `0.0.0.0/0` on ingress for anything non-80/443
|
|
213
|
-
- `protocol = "-1"` (all protocols) — only OK for egress in dev
|
|
214
|
-
- Missing description
|
|
215
|
-
- Source SG referenced by name not by `id` (creates ordering issues)
|
|
216
|
-
|
|
217
|
-
## Naming convention
|
|
218
|
-
|
|
219
|
-
Pattern: `{env}-{service}-{purpose}[-{version}]` — lowercase, dashes, no underscores in resource names.
|
|
220
|
-
|
|
221
|
-
Examples:
|
|
222
|
-
- `prod-api-asg-v2`
|
|
223
|
-
- `dev-rds-aurora-main`
|
|
224
|
-
- `staging-s3-userdata`
|
|
225
|
-
- `prod-kms-rds-master`
|
|
226
|
-
- `prod-iam-role-api-task`
|
|
227
|
-
|
|
228
|
-
Rules:
|
|
229
|
-
- `env` ∈ {`dev`, `staging`, `prod`} (or your team's set, but keep it closed)
|
|
230
|
-
- `service` ∈ {`api`, `web`, `worker`, `rds`, `s3`, `kms`, …}
|
|
231
|
-
- `purpose` = short tag (`asg`, `main`, `userdata`, `master`)
|
|
232
|
-
- `version` = optional, only when running multiple variants (blue/green, v1/v2)
|
|
233
|
-
|
|
234
|
-
For Terraform **resource block names** (the local identifier), use snake_case to match HCL convention — `aws_iam_role.api_task` — and put the dashed name in the `name`/`tags.Name` argument.
|
|
235
|
-
|
|
236
|
-
## Common tags
|
|
237
|
-
|
|
238
|
-
Set once via provider `default_tags`, inherited by every taggable resource:
|
|
239
|
-
|
|
240
|
-
```hcl
|
|
241
|
-
provider "aws" {
|
|
242
|
-
region = var.region
|
|
243
|
-
default_tags {
|
|
244
|
-
tags = {
|
|
245
|
-
Environment = var.env # dev / staging / prod
|
|
246
|
-
Project = var.project
|
|
247
|
-
Owner = var.team_email
|
|
248
|
-
ManagedBy = "terraform"
|
|
249
|
-
CostCenter = var.cost_center
|
|
250
|
-
Repo = var.repo_url
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
```
|
|
255
|
-
|
|
256
|
-
Per-resource tags only when adding something the default doesn't cover: `Component = "api"`, `BackupPolicy = "daily-30d"`, `DataClass = "pii"`.
|
|
257
|
-
|
|
258
|
-
Cost tags must be **stable** — don't change CostCenter mid-quarter; create a new one and migrate.
|
|
259
|
-
|
|
260
|
-
## Outputs — minimal
|
|
261
|
-
|
|
262
|
-
- Export only what other modules / env files actually consume.
|
|
263
|
-
- Don't re-export everything "just in case" — adds noise, slows refresh.
|
|
264
|
-
- Add `description` to every output.
|
|
265
|
-
- Mark secrets with `sensitive = true`.
|
|
266
|
-
- If a consumer needs something later, add it then — not preemptively.
|
|
267
|
-
|
|
268
|
-
```hcl
|
|
269
|
-
output "vpc_id" {
|
|
270
|
-
description = "VPC ID for downstream modules"
|
|
271
|
-
value = aws_vpc.main.id
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
output "rds_endpoint" {
|
|
275
|
-
description = "Aurora writer endpoint"
|
|
276
|
-
value = aws_rds_cluster.main.endpoint
|
|
277
|
-
sensitive = false
|
|
278
|
-
}
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
## Workflow
|
|
282
|
-
|
|
283
|
-
```
|
|
284
|
-
edit .tf
|
|
285
|
-
↓
|
|
286
|
-
user: "check"
|
|
287
|
-
↓
|
|
288
|
-
agent: fmt → validate → plan → SUMMARY (add/change/destroy/drift/dangers)
|
|
289
|
-
↓
|
|
290
|
-
user reviews
|
|
291
|
-
↓
|
|
292
|
-
user runs `terraform apply` ← never the agent
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
If plan shows destroys on stateful resources or expensive new resources, agent **must** pause and re-confirm before user proceeds.
|
|
296
|
-
|
|
297
|
-
## Anti-patterns
|
|
298
|
-
|
|
299
|
-
- Running or suggesting `terraform apply`
|
|
300
|
-
- Plaintext password / API key in `.tf` or `.tfvars`
|
|
301
|
-
- `Resource = "*"` on IAM without justification
|
|
302
|
-
- `cidr_blocks = ["0.0.0.0/0"]` on private services
|
|
303
|
-
- SGR with no description, or description like `"allow"`
|
|
304
|
-
- One giant `main.tf` at env level — split per service
|
|
305
|
-
- Module that branches on `var.env` — push env-specific values up to the env layer
|
|
306
|
-
- Hardcoded account IDs / ARNs — use `data "aws_caller_identity"` / variables
|
|
307
|
-
- Creating expensive resource without quoting cost first
|
|
308
|
-
- Missing `prevent_destroy` on Aurora / S3 / KMS / SSM SecureString
|
|
309
|
-
- Outputting everything from a module
|
|
310
|
-
- Mixing snake_case and dashes inconsistently in `Name` tags
|
|
311
|
-
|
|
312
|
-
## Quick reference
|
|
313
|
-
|
|
314
|
-
```
|
|
315
|
-
Q: User says "check" — what do I run?
|
|
316
|
-
A: terraform fmt -recursive → terraform validate → terraform plan → summarize.
|
|
317
|
-
|
|
318
|
-
Q: Plan shows destroy of aws_s3_bucket — what do I do?
|
|
319
|
-
A: STOP. Flag in summary as DANGER. Confirm with user it's intentional. Suggest prevent_destroy if missing.
|
|
320
|
-
|
|
321
|
-
Q: User wants to add ACM Private CA?
|
|
322
|
-
A: Quote ~$400/mo, confirm before writing tf.
|
|
323
|
-
|
|
324
|
-
Q: Where does VPC live?
|
|
325
|
-
A: modules/network/vpc.tf — called from env/<env>/network.tf.
|
|
326
|
-
|
|
327
|
-
Q: Where do I put a new EC2 service?
|
|
328
|
-
A: env/<env>/<service>.tf calling modules/ec2/, with service-specific vars.
|
|
329
|
-
|
|
330
|
-
Q: My EC2 module's app needs an S3 bucket + a KMS key for its own data. Separate modules?
|
|
331
|
-
A: No — put s3.tf and kms.tf inside modules/ec2/. The bucket/key only exist to serve this service. Extract to its own module only when 2+ unrelated callers share it.
|
|
332
|
-
|
|
333
|
-
Q: New SGR, what fields?
|
|
334
|
-
A: type, from_port, to_port, protocol, sg id, source, AND description (who/from/to/proto/why).
|
|
335
|
-
|
|
336
|
-
Q: Storing a DB password?
|
|
337
|
-
A: random_password + aws_ssm_parameter SecureString with prevent_destroy.
|
|
338
|
-
|
|
339
|
-
Q: Should I run apply?
|
|
340
|
-
A: No. Never. User runs apply.
|
|
341
|
-
```
|
|
1
|
+
---
|
|
2
|
+
name: terraform-best-practices
|
|
3
|
+
description: "Terraform/AWS workflow rules: never auto-apply, always confirm before high-cost resources (Private CA ~$400, NAT GW, Aurora, KMS…), plan summary with add/change/destroy + drift, reusable module layout (env/<env>/<service>.tf + modules/<svc>/<component>.tf, network module groups VPC/SG/SGR/NAT), lifecycle prevent_destroy on stateful resources (Aurora, S3, KMS, SSM), no plaintext secrets (random_password + SSM SecureString), least-privilege IAM, SGR must justify who/from/to/protocol/why, naming {env}-{service}-{purpose}[-{version}], common tags via default_tags, minimal outputs. Triggers on .tf edits and on user saying 'check' / 'plan' / 'validate'."
|
|
4
|
+
license: MIT
|
|
5
|
+
metadata:
|
|
6
|
+
author: HoangThang
|
|
7
|
+
version: '1.0.0'
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# Terraform Best Practices
|
|
11
|
+
|
|
12
|
+
Team rules for Terraform on AWS. Read these before editing any `.tf` file.
|
|
13
|
+
|
|
14
|
+
## When to trigger
|
|
15
|
+
|
|
16
|
+
- Editing any `.tf` / `.tfvars` file
|
|
17
|
+
- User says **"check"**, **"plan"**, **"validate"**, **"fmt"**
|
|
18
|
+
- Creating or modifying infrastructure
|
|
19
|
+
- Reviewing drift, cost, or security of cloud resources
|
|
20
|
+
|
|
21
|
+
## CRITICAL — never apply
|
|
22
|
+
|
|
23
|
+
**Do NOT run `terraform apply`. Do NOT suggest the user run it.** Apply is always the user's decision, on the user's machine, with their credentials. Agent only edits files and runs read-only commands (`fmt`, `validate`, `plan`, `show`, `state list`, `output`).
|
|
24
|
+
|
|
25
|
+
If user explicitly types "apply" / "run apply" → still pause and ask once to confirm, then let them run it themselves unless they insist.
|
|
26
|
+
|
|
27
|
+
## High-cost resources — confirm before writing
|
|
28
|
+
|
|
29
|
+
Stop and ask the user before creating any of these. Quote the rough monthly cost so they can decide:
|
|
30
|
+
|
|
31
|
+
| Resource | Rough cost | Why expensive |
|
|
32
|
+
|---|---|---|
|
|
33
|
+
| `aws_acmpca_certificate_authority` | ~$400/mo per CA | flat monthly fee |
|
|
34
|
+
| `aws_nat_gateway` | ~$35/mo + data | per AZ, per GB |
|
|
35
|
+
| `aws_transit_gateway` | ~$36/mo + attach + data | per attachment too |
|
|
36
|
+
| `aws_rds_cluster` (Aurora) | varies, often >$100/mo | per ACU/hour |
|
|
37
|
+
| `aws_elasticache_*` | varies | per node-hour |
|
|
38
|
+
| `aws_opensearch_domain` | varies | per node-hour + storage |
|
|
39
|
+
| `aws_vpc_endpoint` (Interface) | ~$7/mo per endpoint per AZ | per ENI-hour |
|
|
40
|
+
| `aws_kms_key` (customer-managed) | $1/mo + req | adds up across services |
|
|
41
|
+
| `aws_eks_cluster` | $73/mo control plane | + node costs |
|
|
42
|
+
| `aws_msk_cluster` | varies, often >$200/mo | per broker-hour |
|
|
43
|
+
| `aws_directconnect_*` | $$$ | port + data |
|
|
44
|
+
| `aws_globalaccelerator_*` | ~$18/mo + data | flat + traffic |
|
|
45
|
+
|
|
46
|
+
Pattern: list what will be created → estimated monthly cost → wait for explicit OK. Don't write the `.tf` until confirmed.
|
|
47
|
+
|
|
48
|
+
## The "check" command
|
|
49
|
+
|
|
50
|
+
When user says **"check"**, run sequentially and report:
|
|
51
|
+
|
|
52
|
+
1. `terraform fmt -recursive` — fix formatting, list any files changed
|
|
53
|
+
2. `terraform validate` — surface syntax/type errors
|
|
54
|
+
3. `terraform plan -no-color` — capture the plan
|
|
55
|
+
|
|
56
|
+
Then summarize the plan in this format:
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
PLAN SUMMARY
|
|
60
|
+
|
|
61
|
+
ADD (N): <type.name> × N — list types
|
|
62
|
+
CHANGE (N): <type.name> — flag any in-place change that recreates / drops data
|
|
63
|
+
DESTROY (N): <type.name> — flag DANGER for aurora, s3, kms, ssm, dynamodb
|
|
64
|
+
DRIFT (N): <type.name> — suggest fix (terraform import / update .tf / ignore_changes)
|
|
65
|
+
|
|
66
|
+
NOTES:
|
|
67
|
+
- High-cost adds (if any)
|
|
68
|
+
- Destructive actions on stateful resources
|
|
69
|
+
- Resources missing prevent_destroy that should have it
|
|
70
|
+
- IAM widening (Resource = "*", new wildcard actions)
|
|
71
|
+
- New SGR without description or with 0.0.0.0/0 ingress
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Keep it under 30 lines. Link to the full plan output if user wants more.
|
|
75
|
+
|
|
76
|
+
## Project layout — reusable modules
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
infra/
|
|
80
|
+
env/
|
|
81
|
+
dev/
|
|
82
|
+
providers.tf # provider + default_tags
|
|
83
|
+
backend.tf # remote state config
|
|
84
|
+
variables.tf
|
|
85
|
+
terraform.tfvars
|
|
86
|
+
network.tf # calls modules/network
|
|
87
|
+
ec2.tf # calls modules/ec2 (one file per service)
|
|
88
|
+
rds.tf
|
|
89
|
+
s3.tf
|
|
90
|
+
kms.tf
|
|
91
|
+
iam.tf # env-level roles only
|
|
92
|
+
staging/
|
|
93
|
+
prod/
|
|
94
|
+
modules/
|
|
95
|
+
network/ # VPC + subnets + SG + SGR + NAT + route tables
|
|
96
|
+
vpc.tf
|
|
97
|
+
subnets.tf
|
|
98
|
+
sg.tf
|
|
99
|
+
sgr.tf
|
|
100
|
+
nat.tf
|
|
101
|
+
route_tables.tf
|
|
102
|
+
variables.tf
|
|
103
|
+
outputs.tf
|
|
104
|
+
ec2/ # module groups everything the EC2 service needs
|
|
105
|
+
main.tf # instance / launch template / ASG
|
|
106
|
+
iam.tf # instance profile + role + policy
|
|
107
|
+
sg.tf # service-specific SG
|
|
108
|
+
s3.tf # (optional) bucket the app reads/writes
|
|
109
|
+
ssm.tf # (optional) params the app loads at boot
|
|
110
|
+
kms.tf # (optional) key used to encrypt its own data
|
|
111
|
+
variables.tf
|
|
112
|
+
outputs.tf
|
|
113
|
+
rds/
|
|
114
|
+
network/
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Rules — guidelines, not laws:
|
|
118
|
+
|
|
119
|
+
- **One `.tf` per service** at the env level (`ec2.tf`, `rds.tf`, …). Easier to grep, easier to PR.
|
|
120
|
+
- **Inside a module, split files by component**, not by AWS service category. The point is readability — keep each file focused on one concern of the module.
|
|
121
|
+
- **A module owns the resources its task needs.** If the `ec2` module's app reads from an S3 bucket and loads SSM params, that bucket and those params live **inside** `modules/ec2/` (`s3.tf`, `ssm.tf`). Don't force them out into a separate `modules/s3/` just because they're S3 — separation by AWS service is artificial.
|
|
122
|
+
- **When to extract a separate module instead:** the resource is genuinely shared by 2+ unrelated callers, has its own lifecycle, or belongs to a different team's domain. Otherwise keep it co-located with the consumer.
|
|
123
|
+
- **Network module aggregates** VPC, subnets, SG, SGR, NAT, route tables — because everyone consumes its outputs. Other modules call into it for `vpc_id`, `subnet_ids`, base `sg_ids`.
|
|
124
|
+
- Module must be **env-agnostic** — env-specific values come in as variables.
|
|
125
|
+
- No `count` / `for_each` gymnastics in the env layer when a separate file would be clearer.
|
|
126
|
+
|
|
127
|
+
Heuristic for "module or env file?": if the resource is only meaningful **with** the service (its IAM role, its bucket, its SSM param), put it in the module. If it's a foundational layer that many services share (VPC, central KMS key, shared S3 data lake), put it in its own module or at the env layer.
|
|
128
|
+
|
|
129
|
+
## Lifecycle — prevent destroy on stateful
|
|
130
|
+
|
|
131
|
+
Always add to data-bearing or hard-to-recreate resources:
|
|
132
|
+
|
|
133
|
+
```hcl
|
|
134
|
+
lifecycle {
|
|
135
|
+
prevent_destroy = true
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Required on:
|
|
140
|
+
- `aws_rds_cluster`, `aws_db_instance` (Aurora/RDS)
|
|
141
|
+
- `aws_s3_bucket`
|
|
142
|
+
- `aws_kms_key`, `aws_kms_alias`
|
|
143
|
+
- `aws_ssm_parameter` (SecureString)
|
|
144
|
+
- `aws_dynamodb_table` (if used as primary store)
|
|
145
|
+
- `aws_acmpca_certificate_authority`
|
|
146
|
+
- `aws_efs_file_system`, `aws_backup_vault`
|
|
147
|
+
|
|
148
|
+
For volatile attributes that change outside Terraform, also use `ignore_changes` (e.g. autoscaling-managed desired_capacity).
|
|
149
|
+
|
|
150
|
+
## Secrets — no plaintext, ever
|
|
151
|
+
|
|
152
|
+
❌ Never put real secrets in `.tf` or `.tfvars`. Never commit to git.
|
|
153
|
+
|
|
154
|
+
✅ Pattern: generate with `random_password`, store in SSM SecureString or Secrets Manager, reference via data source or attribute.
|
|
155
|
+
|
|
156
|
+
```hcl
|
|
157
|
+
resource "random_password" "rds_master" {
|
|
158
|
+
length = 32
|
|
159
|
+
special = true
|
|
160
|
+
min_special = 4
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
resource "aws_ssm_parameter" "rds_master" {
|
|
164
|
+
name = "/${var.env}/rds/${var.service}/master-password"
|
|
165
|
+
type = "SecureString"
|
|
166
|
+
key_id = aws_kms_key.ssm.arn
|
|
167
|
+
value = random_password.rds_master.result
|
|
168
|
+
|
|
169
|
+
lifecycle { prevent_destroy = true }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
resource "aws_rds_cluster" "main" {
|
|
173
|
+
master_password = random_password.rds_master.result
|
|
174
|
+
# ...
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
For rotation: prefer **Secrets Manager** with `rotation_lambda_arn`. For static lookups from existing secrets: `data "aws_ssm_parameter"` with `with_decryption = true`.
|
|
179
|
+
|
|
180
|
+
## IAM — least privilege
|
|
181
|
+
|
|
182
|
+
- Start with the **minimum action set**. When AccessDenied appears in logs, add the specific action.
|
|
183
|
+
- Prefer **explicit resource ARNs** over `Resource = "*"`. Wildcards only when AWS itself requires (e.g. some `iam:PassRole` cases).
|
|
184
|
+
- Prefer **AWS-managed policies** only for well-known service roles (`AmazonSSMManagedInstanceCore`, `AmazonEC2ContainerRegistryReadOnly`).
|
|
185
|
+
- Custom policies > inline policies (easier to reuse and review).
|
|
186
|
+
- Use **conditions** when scope can be tightened: `aws:SourceVpc`, `aws:SourceArn`, `StringEquals: aws:ResourceTag/...`.
|
|
187
|
+
- Separate **trust policy** from **permission policy** clearly.
|
|
188
|
+
|
|
189
|
+
## Security group rules — must justify
|
|
190
|
+
|
|
191
|
+
Every `aws_security_group_rule` (or inline `ingress`/`egress`) must have a `description` answering all 5:
|
|
192
|
+
|
|
193
|
+
1. **Who** calls (source service/team)
|
|
194
|
+
2. **From where** (CIDR or source sg)
|
|
195
|
+
3. **To what port** / port range
|
|
196
|
+
4. **Protocol** (tcp/udp/icmp)
|
|
197
|
+
5. **Why** it's needed
|
|
198
|
+
|
|
199
|
+
```hcl
|
|
200
|
+
resource "aws_security_group_rule" "api_to_rds" {
|
|
201
|
+
type = "ingress"
|
|
202
|
+
from_port = 5432
|
|
203
|
+
to_port = 5432
|
|
204
|
+
protocol = "tcp"
|
|
205
|
+
security_group_id = module.network.sg_rds_id
|
|
206
|
+
source_security_group_id = module.ec2_api.sg_id
|
|
207
|
+
description = "api service (api-sg) -> rds postgres on tcp/5432 — app reads/writes user data"
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Red flags to call out in plan summary:
|
|
212
|
+
- `0.0.0.0/0` on ingress for anything non-80/443
|
|
213
|
+
- `protocol = "-1"` (all protocols) — only OK for egress in dev
|
|
214
|
+
- Missing description
|
|
215
|
+
- Source SG referenced by name not by `id` (creates ordering issues)
|
|
216
|
+
|
|
217
|
+
## Naming convention
|
|
218
|
+
|
|
219
|
+
Pattern: `{env}-{service}-{purpose}[-{version}]` — lowercase, dashes, no underscores in resource names.
|
|
220
|
+
|
|
221
|
+
Examples:
|
|
222
|
+
- `prod-api-asg-v2`
|
|
223
|
+
- `dev-rds-aurora-main`
|
|
224
|
+
- `staging-s3-userdata`
|
|
225
|
+
- `prod-kms-rds-master`
|
|
226
|
+
- `prod-iam-role-api-task`
|
|
227
|
+
|
|
228
|
+
Rules:
|
|
229
|
+
- `env` ∈ {`dev`, `staging`, `prod`} (or your team's set, but keep it closed)
|
|
230
|
+
- `service` ∈ {`api`, `web`, `worker`, `rds`, `s3`, `kms`, …}
|
|
231
|
+
- `purpose` = short tag (`asg`, `main`, `userdata`, `master`)
|
|
232
|
+
- `version` = optional, only when running multiple variants (blue/green, v1/v2)
|
|
233
|
+
|
|
234
|
+
For Terraform **resource block names** (the local identifier), use snake_case to match HCL convention — `aws_iam_role.api_task` — and put the dashed name in the `name`/`tags.Name` argument.
|
|
235
|
+
|
|
236
|
+
## Common tags
|
|
237
|
+
|
|
238
|
+
Set once via provider `default_tags`, inherited by every taggable resource:
|
|
239
|
+
|
|
240
|
+
```hcl
|
|
241
|
+
provider "aws" {
|
|
242
|
+
region = var.region
|
|
243
|
+
default_tags {
|
|
244
|
+
tags = {
|
|
245
|
+
Environment = var.env # dev / staging / prod
|
|
246
|
+
Project = var.project
|
|
247
|
+
Owner = var.team_email
|
|
248
|
+
ManagedBy = "terraform"
|
|
249
|
+
CostCenter = var.cost_center
|
|
250
|
+
Repo = var.repo_url
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
Per-resource tags only when adding something the default doesn't cover: `Component = "api"`, `BackupPolicy = "daily-30d"`, `DataClass = "pii"`.
|
|
257
|
+
|
|
258
|
+
Cost tags must be **stable** — don't change CostCenter mid-quarter; create a new one and migrate.
|
|
259
|
+
|
|
260
|
+
## Outputs — minimal
|
|
261
|
+
|
|
262
|
+
- Export only what other modules / env files actually consume.
|
|
263
|
+
- Don't re-export everything "just in case" — adds noise, slows refresh.
|
|
264
|
+
- Add `description` to every output.
|
|
265
|
+
- Mark secrets with `sensitive = true`.
|
|
266
|
+
- If a consumer needs something later, add it then — not preemptively.
|
|
267
|
+
|
|
268
|
+
```hcl
|
|
269
|
+
output "vpc_id" {
|
|
270
|
+
description = "VPC ID for downstream modules"
|
|
271
|
+
value = aws_vpc.main.id
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
output "rds_endpoint" {
|
|
275
|
+
description = "Aurora writer endpoint"
|
|
276
|
+
value = aws_rds_cluster.main.endpoint
|
|
277
|
+
sensitive = false
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## Workflow
|
|
282
|
+
|
|
283
|
+
```
|
|
284
|
+
edit .tf
|
|
285
|
+
↓
|
|
286
|
+
user: "check"
|
|
287
|
+
↓
|
|
288
|
+
agent: fmt → validate → plan → SUMMARY (add/change/destroy/drift/dangers)
|
|
289
|
+
↓
|
|
290
|
+
user reviews
|
|
291
|
+
↓
|
|
292
|
+
user runs `terraform apply` ← never the agent
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
If plan shows destroys on stateful resources or expensive new resources, agent **must** pause and re-confirm before user proceeds.
|
|
296
|
+
|
|
297
|
+
## Anti-patterns
|
|
298
|
+
|
|
299
|
+
- Running or suggesting `terraform apply`
|
|
300
|
+
- Plaintext password / API key in `.tf` or `.tfvars`
|
|
301
|
+
- `Resource = "*"` on IAM without justification
|
|
302
|
+
- `cidr_blocks = ["0.0.0.0/0"]` on private services
|
|
303
|
+
- SGR with no description, or description like `"allow"`
|
|
304
|
+
- One giant `main.tf` at env level — split per service
|
|
305
|
+
- Module that branches on `var.env` — push env-specific values up to the env layer
|
|
306
|
+
- Hardcoded account IDs / ARNs — use `data "aws_caller_identity"` / variables
|
|
307
|
+
- Creating expensive resource without quoting cost first
|
|
308
|
+
- Missing `prevent_destroy` on Aurora / S3 / KMS / SSM SecureString
|
|
309
|
+
- Outputting everything from a module
|
|
310
|
+
- Mixing snake_case and dashes inconsistently in `Name` tags
|
|
311
|
+
|
|
312
|
+
## Quick reference
|
|
313
|
+
|
|
314
|
+
```
|
|
315
|
+
Q: User says "check" — what do I run?
|
|
316
|
+
A: terraform fmt -recursive → terraform validate → terraform plan → summarize.
|
|
317
|
+
|
|
318
|
+
Q: Plan shows destroy of aws_s3_bucket — what do I do?
|
|
319
|
+
A: STOP. Flag in summary as DANGER. Confirm with user it's intentional. Suggest prevent_destroy if missing.
|
|
320
|
+
|
|
321
|
+
Q: User wants to add ACM Private CA?
|
|
322
|
+
A: Quote ~$400/mo, confirm before writing tf.
|
|
323
|
+
|
|
324
|
+
Q: Where does VPC live?
|
|
325
|
+
A: modules/network/vpc.tf — called from env/<env>/network.tf.
|
|
326
|
+
|
|
327
|
+
Q: Where do I put a new EC2 service?
|
|
328
|
+
A: env/<env>/<service>.tf calling modules/ec2/, with service-specific vars.
|
|
329
|
+
|
|
330
|
+
Q: My EC2 module's app needs an S3 bucket + a KMS key for its own data. Separate modules?
|
|
331
|
+
A: No — put s3.tf and kms.tf inside modules/ec2/. The bucket/key only exist to serve this service. Extract to its own module only when 2+ unrelated callers share it.
|
|
332
|
+
|
|
333
|
+
Q: New SGR, what fields?
|
|
334
|
+
A: type, from_port, to_port, protocol, sg id, source, AND description (who/from/to/proto/why).
|
|
335
|
+
|
|
336
|
+
Q: Storing a DB password?
|
|
337
|
+
A: random_password + aws_ssm_parameter SecureString with prevent_destroy.
|
|
338
|
+
|
|
339
|
+
Q: Should I run apply?
|
|
340
|
+
A: No. Never. User runs apply.
|
|
341
|
+
```
|