I've been running Claude Code through its paces on our infrastructure-as-code repositories for the past month, specifically for Terraform and Kubernetes manifests. The biggest initial headache wasn't getting it to write *correct* code—it's surprisingly good at that—but getting it to adhere to our team's painfully specific, non-negotiable internal style guide. It kept reverting to its own defaults, which caused our linters to scream and wasted time in review.
After a lot of failed experiments with simple instructions, I found a method that finally works reliably. The key is to treat the style guide not as a suggestion, but as a non-bypassable system directive. You need to structure the prompt to make deviation impossible.
Here is the exact prompt template I now use at the start of every session or in my custom instructions. The critical part is the explicit "DO NOT DEVIATE" list and the structured examples.
```
You are now an engineer bound by our internal style guide. All code you generate must strictly comply with the following rules. Treat these as system-level constraints.
**HARD CONSTRAINTS (DO NOT DEVIATE):**
- **Indentation:** Use 2 spaces for Terraform/HCL, 4 spaces for Python, 2 spaces for YAML (Kubernetes).
- **Naming:** All resources must use snake_case, including `local_name` and output values. No hyphens.
- **Structure:** Terraform modules must have: `variables.tf`, `main.tf`, `outputs.tf`. Do not combine.
- **Comments:** Every Terraform resource requires a preceding comment explaining its role. No inline `#` comments on the same line.
- **Kubernetes:** Container specs must have explicit `resources.limits` and `resources.requests`. Omit `latest` tags.
**EXAMPLE OF COMPLIANT CODE:**
```hcl
# Provisions the primary application database instance.
resource "aws_db_instance" "application_database" {
identifier = "prod_app_db"
engine = "postgres"
instance_class = "db.t3.large"
allocated_storage = 100
# ... other args
}
output "database_endpoint" {
value = aws_db_instance.application_database.endpoint
}
```
**EXAMPLE OF NON-COMPLIANT CODE (DO NOT GENERATE THIS):**
```hcl
resource "aws-db-instance" "appDb" { // main DB
identifier = "prod-app-db"
engine = "postgres"
instance_class = "db.t3.large"
}
```
```
The results have been consistent. When I simply said "follow our style guide," Claude would occasionally slip, especially with naming conventions. By providing the negative example—showing it exactly what *not* to do—and framing the rules as "HARD CONSTRAINTS," the compliance rate went to near 100%.
A concrete before-and-after from my last task:
**Before (generic prompt):**
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-gateway
spec:
template:
spec:
containers:
- name: gateway
image: nginx:latest
ports:
- containerPort: 80
```
**After (using the constraint prompt):**
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api_gateway
spec:
template:
spec:
containers:
- name: gateway
image: nginx:1.23
resources:
limits:
memory: "256Mi"
cpu: "500m"
requests:
memory: "128Mi"
cpu: "250m"
ports:
- containerPort: 80
```
The difference is night and day for our automation. The generated code now passes `terraform fmt`, `tflint`, `kube-linter`, and our pre-commit hooks on the first try. This has cut my review cycles for AI-generated code drafts in half. If your team is serious about style compliance, you need to be this explicit and adversarial with the instructions.
-- as