
Table of Contents
By Khimananda Oli | Last reviewed: August 2026
Generating production-grade infrastructure or application logic via AI requires more than casual conversation; it demands structured prompt patterns for writing better code that constrain the model’s output space. Without explicit context, version pinning, and security guardrails, Large Language Models (LLMs) frequently hallucinate deprecated flags or invent insecure configurations that fail in real environments. This guide provides battle-tested templates and architectural mental models specifically for DevOps engineers and developers who need reliable, audit-ready code rather than generic snippets. If you are new to this domain, start with my overview on prompt engineering for DevOps engineers before applying these advanced patterns.
How do you structure prompt patterns for writing better code?
The most common failure mode I see when teams adopt AI-assisted coding is treating the LLM as a search engine rather than a reasoning engine. To get usable output, you must front-load context. A robust prompt pattern always includes four distinct components: Persona, Context, Constraints, and Output Format. When I ask an LLM to generate Terraform modules, I never say "create an S3 bucket." Instead, I specify: "Act as a Senior Cloud Architect specializing in SOC 2 compliance. Generate a Terraform module for AWS S3 using provider version ~> 5.0. Enforce server-side encryption with KMS, block public access, enable versioning, and tag all resources with 'Environment' and 'CostCenter'. Output only HCL code with inline comments explaining security decisions."
This level of specificity serves two purposes. First, it anchors the model's probabilistic generation to a specific subset of its training data related to modern, secure practices. Second, it creates a verifiable contract. You can immediately check if the output meets the stated constraints. For teams adopting generating IaC with AI guardrails, this structured approach is mandatory. It transforms subjective code review into objective constraint validation. Always pin versions explicitly; an LLM trained on data up to 2024 might default to Terraform 1.5 syntax even if your state file requires 1.9 features, causing subtle plan drift.
The Context-Constraint Matrix
I recommend maintaining a personal or team-level "context library" — a repository of pre-vetted prompt blocks. When you need to generate code, assemble these blocks rather than typing from scratch. Include your organization’s standard tagging policy, approved CIDR ranges, naming conventions, and banned services. This reduces token waste and ensures consistency across different team members’ prompts. Remember that prompt patterns for writing better code are reusable assets, not disposable chat messages.
What are the best prompt patterns for writing better code in Infrastructure as Code?
Infrastructure as Code presents unique challenges because bugs manifest as security vulnerabilities or billing disasters rather than simple runtime errors. The most effective pattern here is the "Secure-by-Default Skeleton." Never ask the LLM to write infrastructure from zero. Instead, provide a secure skeleton or reference architecture and ask it to fill in specific business logic within those boundaries.
<!-- Example: Secure EKS Module Prompt Pattern -->
ROLE: Principal DevOps Engineer, AWS EKS Specialist
TASK: Create an EKS cluster module for a fintech workload
CONSTRAINTS:
- EKS version: 1.31
- Node groups: Managed, t3.medium, min 2 max 5
- Networking: Private subnets only, no public API endpoint
- Security: IRSA enabled, Pod Identity Agent installed
- Compliance: All logs to CloudWatch, encryption at rest/transit
- DO NOT: Use default VPC, enable public endpoint, use node IAM roles
OUTPUT: Single main.tf with validation blocks and security comments This pattern forces the model to operate within a safety corridor. Notice the explicit "DO NOT" constraints. LLMs are sycophantic; they tend to agree with implicit assumptions unless explicitly forbidden. By listing anti-patterns, you actively suppress common misconfigurations. In my experience helping Nepal-based fintech companies achieve compliance, this negative-constraint technique has prevented more issues than positive instructions alone.
Handling State and Dependencies
When generating code that interacts with existing infrastructure, always paste relevant state excerpts or dependency graphs into the prompt context window. An LLM cannot infer your VPC ID or existing IAM role ARNs. Providing these values prevents placeholder hallucinations like vpc-12345678. For complex dependencies, consider using AI to write Terraform and Kubernetes YAML in conjunction with tools like terraform plan -json fed back into the chat as context for correction.
How do prompt patterns for writing better code differ between application and infrastructure development?
While the core principles remain consistent, the tolerance for ambiguity differs drastically. Application code often benefits from creative exploration, whereas infrastructure demands deterministic precision. Understanding these differences helps you select the right pattern variant.
| Dimension | Application Code Prompts | Infrastructure Code Prompts |
|---|---|---|
| Primary Goal | Functionality, readability, performance | Security, compliance, cost-efficiency, stability |
| Version Sensitivity | Moderate (framework APIs evolve) | Critical (provider breaking changes cause outages) |
| Context Requirement | Business logic, data models, UI specs | Network topology, IAM policies, compliance frameworks |
| Validation Method | Unit tests, linting, type checking | Plan analysis, policy-as-code (OPA), security scanning |
| Risk Profile | Bugs, tech debt, UX issues | Data breaches, regulatory fines, service downtime |
| Recommended Pattern | Test-Driven Generation, Refactoring Chains | Secure Skeleton, Constraint Lists, Audit Trails |
In application development, I frequently use "Test-First Prompting." Ask the LLM to write failing tests for a feature before generating the implementation. This aligns the model’s understanding of requirements with executable specifications. For infrastructure, however, tests are slower and more expensive. Instead, use "Policy-First Prompting": ask the model to generate the OPA Rego policy or Sentinel rule first, then generate the Terraform that passes it. This inverts the traditional workflow but dramatically increases confidence in the output.
How can you validate outputs from prompt patterns for writing better code?
Trust but verify is insufficient for AI-generated code; you must automate verification. Never copy-paste LLM output directly into production repositories without passing through automated gates. Integrate validation tools into your IDE or CI pipeline that specifically target LLM failure modes. Tools like tflint, kubeval, checkov, and semgrep should run immediately after generation.
A powerful meta-pattern is "Self-Correction Loops." After receiving initial code, prompt the model: "Review this code against CIS Benchmark v2.0 for EKS. List every violation, explain why it fails, and provide the corrected code block." This leverages the model’s broader knowledge base for auditing while keeping the generation focused. Be aware that self-correction is not foolproof; always pair it with external tooling. For teams implementing AI code review in CI pipelines, this dual-layer validation catches both syntactic errors and semantic security flaws.
Documentation as Validation
Require the LLM to generate documentation alongside code. Ask for Architecture Decision Records (ADRs) or threat models as part of the same prompt. If the model cannot coherently explain why a security group allows port 22 from 0.0.0.0/0, that is a signal the configuration is likely wrong. Documentation generation acts as a reasoning stress test. In regulated environments, this documentation also serves as audit evidence, killing two birds with one stone.
When should you avoid using prompt patterns for writing better code?
Despite their power, these patterns have limits. Do not rely on LLMs for novel cryptographic implementations, proprietary protocol reverse-engineering, or highly sensitive credential handling. The model’s training data cutoff means it lacks awareness of zero-day vulnerabilities disclosed last week. Always cross-reference generated security configurations against current CVE databases and vendor advisories. Additionally, avoid using prompts for legacy systems with undocumented behavior; the model will confidently generate plausible-looking but completely incorrect COBOL or mainframe JCL based on generalized patterns rather than your specific environment’s quirks.
For teams in Nepal working with local payment gateways like eSewa or Khalti, be especially cautious. Global LLMs have minimal training data on these specific APIs. In such cases, use the LLM for structural scaffolding and boilerplate, but manually implement the integration logic using official SDK documentation. Supplementing prompts with retrieved documentation via RAG can help, but human verification remains non-negotiable for region-specific integrations.
Building Your Practice with Prompt Patterns for Writing Better Code
Mastering prompt patterns for writing better code is an iterative discipline, not a one-time setup. Start by documenting your successful prompts in a shared team wiki or repository. Treat prompts as code: version them, review them, and retire them when underlying platforms change. Measure effectiveness by tracking reduction in review cycles and post-deployment incidents. As you refine your patterns, you will find that the quality of AI-generated code becomes predictable and trustworthy. If you need help establishing these workflows for your team or auditing your current AI-assisted development practices, reach out to discuss your specific infrastructure challenges.