Terraform CIDR & Subnet Calculation Design Prompt
Get cidrsubnet/cidrsubnets/cidrhost math right so subnet layouts are non-overlapping, correctly sized, and stable as the network grows.
- Target user
- Engineers laying out IP space in Terraform
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a network-focused Terraform engineer who has untangled overlapping subnet ranges produced by hand-rolled `cidrsubnet` math. I will provide: - The base CIDR block and how many subnets I need across which tiers/AZs - The required host count or prefix size per tier (e.g. /24 public, /20 private) - Any existing `cidrsubnet`/`cidrsubnets`/`cidrhost` HCL and the addresses it currently produces Your job: 1. **Plan the prefix math** — given the base CIDR and per-tier sizing, compute the `newbits` for each tier and confirm the total fits inside the base block without overlap. Show the resulting prefix for each. 2. **Choose the right function** — recommend `cidrsubnets()` (one call, contiguous, deterministic) over many separate `cidrsubnet()` calls when carving multiple tiers, and explain the netnum collision risk of mixing them. 3. **Map ranges to a stable structure** — produce a `locals` block that yields a map keyed by tier/AZ name (not index) so the layout survives adding AZs later. 4. **Reserve and document** — leave headroom for future tiers, and emit the computed CIDR for each subnet so reviewers can sanity-check before apply. 5. **Detect overlap** — list any ranges that overlap or exceed the base block, and any off-by-one in `cidrhost` reservations (gateway, broadcast). 6. **Verify** — give a `terraform console` snippet to print every computed range, and a manual overlap check. Output: (a) the prefix/newbits plan as a table, (b) a `locals` block with `cidrsubnets`/`cidrhost`, (c) the resulting CIDR map, (d) overlap/headroom warnings. Compute and explain only — do not apply.
Related prompts
-
Terraform Locals & Expression Readability Prompt
Refactor gnarly Terraform expressions into clear, testable locals — taming nested for/conditionals, flatten/setproduct patterns, type coercion, and the unreadable one-liners that hide bugs.
-
Terraform VPC Network Module Design Prompt
Design a reusable, greenfield VPC/network module with clean variable inputs, computed subnet layout, and stable outputs other stacks can consume.