Writing infrastructure code

From ancoleman/ai-design-components · View interactive version

Writing infrastructure code is an AI agent skill from the GitHub repository ancoleman/ai-design-components. Managing cloud infrastructure using declarative and imperative IaC tools. Use when provisioning cloud resources (Terraform/OpenTofu for multi-cloud, Pulumi for developer-centric workflows, AWS CDK for AWS-native infrastructure), designing reusable modules, implementing state management patterns, or

How to use this skill

Skill file

Infrastructure as Code

Provision and manage cloud infrastructure using code-based automation tools. This skill covers tool selection, state management, module design, and operational patterns across Terraform/OpenTofu, Pulumi, and AWS CDK.

When to Use

Use this skill when:

Common requests:

Core Concepts

Infrastructure as Code Fundamentals

Key Principles:

  1. Declarative vs Imperative - Describe desired state (Terraform) or program infrastructure (Pulumi)
  2. Idempotency - Same input produces same output, safe to re-run
  3. Version Control - Infrastructure changes tracked in Git
  4. State Management - Track actual infrastructure state
  5. Module Composition - Reusable, versioned infrastructure components

Benefits:

Tool Selection Framework

Choose IaC tools based on team composition and cloud strategy:

Terraform/OpenTofu - Declarative, HCL-based

Pulumi - Imperative, programming language-based

AWS CDK - AWS-native, programming language-based

Decision Tree:

Multi-cloud required?
├─ YES → Team composition?
│  ├─ Ops/SRE focused → Terraform/OpenTofu
│  └─ Developer focused → Pulumi
└─ NO → AWS only?
   ├─ YES → Language preference?
   │  ├─ HCL/declarative → Terraform
   │  ├─ TypeScript/Python → AWS CDK
   │  └─ YAML/simple → CloudFormation
   └─ NO → GCP/Azure only?
      └─ Terraform or Pulumi

State Management Architecture

Remote state with locking enables team collaboration:

Backend Selection:

| Cloud Provider | Recommended Backend | Locking Mechanism |

|----------------|---------------------|-------------------|

| AWS | S3 + DynamoDB | DynamoDB table |

| GCP | Google Cloud Storage | Native |

| Azure | Azure Blob Storage | Lease-based |

| Multi-cloud | Terraform Cloud/Enterprise | Built-in |

| Pulumi | Pulumi Service | Built-in |

State Isolation Strategies:

  1. Directory Separation (recommended for most teams)
  1. Workspaces
  1. Layered Architecture

Critical State Management Rules:

Module Design Patterns

Composable Module Structure:

modules/
├── vpc/              # Network foundation
├── security-group/   # Reusable security group patterns
├── rds/              # Database with backups, encryption
├── ecs-cluster/      # Container orchestration base
├── ecs-service/      # Individual microservice
└── alb/              # Application load balancer

Module Versioning:

Module Design Principles:

When to Create a Module:

When to Keep Monolithic:

Quick Reference

Terraform/OpenTofu Commands

# Initialize providers and backend
terraform init

# Plan changes (preview)
terraform plan

# Apply changes
terraform apply

# Destroy infrastructure
terraform destroy

# Format HCL files
terraform fmt

# Validate syntax
terraform validate

# Show state
terraform state list
terraform state show <resource>

# Import existing resources
terraform import <resource.name> <id>

# Workspace management
terraform workspace list
terraform workspace new staging
terraform workspace select prod

Pulumi Commands

# Initialize new project
pulumi new aws-typescript

# Preview changes
pulumi preview

# Apply changes
pulumi up

# Destroy infrastructure
pulumi destroy

# Show stack outputs
pulumi stack output

# Manage stacks
pulumi stack ls
pulumi stack select prod

# Import existing resources
pulumi import <type> <name> <id>

# Export/import state
pulumi stack export > state.json
pulumi stack import < state.json

AWS CDK Commands

# Initialize new app
cdk init app --language typescript

# Synthesize CloudFormation
cdk synth

# Preview changes
cdk diff

# Deploy stack
cdk deploy

# Destroy stack
cdk destroy

# Bootstrap account/region
cdk bootstrap

# List stacks
cdk list

Common Patterns Checklist

Infrastructure Provisioning:

Module Development:

Operational Readiness:

Detailed Documentation

For comprehensive patterns and implementation details:

Tool-Specific Patterns:

Architecture and Design:

Operations:

Working Examples

Practical implementations demonstrating IaC patterns:

Terraform Examples:

Pulumi Examples:

AWS CDK Examples:

Utility Scripts

Automated validation and operational tools:

Integration with Other Skills

Deployment Pipeline:

Platform Engineering:

Security:

Operations:

Data Platform:

Best Practices

Development Workflow:

  1. Write infrastructure code in feature branches
  2. Run terraform plan / pulumi preview locally
  3. Submit pull request with plan output
  4. Code review focuses on security, cost, blast radius
  5. CI runs automated tests and security scans
  6. Apply only after approval and CI passes
  7. Monitor for drift post-deployment

State Management:

Module Development:

Security:

Cost Management:

Operational Excellence:

Common Pitfalls

State File Issues:

Module Design:

Operations:

Security:

Troubleshooting Guide

State Lock Issues:

terraform force-unlock <lock-id>  # Use only if certain no other process running

Import Existing Resources:

terraform import aws_vpc.main vpc-12345678
pulumi import aws:ec2/vpc:Vpc main vpc-12345678

Drift Detection:

terraform plan -detailed-exitcode  # Exit 2 = drift detected
pulumi preview --diff

For detailed drift remediation, see references/drift-detection.md.

State Recovery:

# Terraform: Restore from S3 versioning
aws s3 cp s3://bucket/backup/terraform.tfstate terraform.tfstate

# Pulumi: Restore from checkpoint
pulumi stack export --version <timestamp> | pulumi stack import

Related Skills

For cloud-specific implementations:

For infrastructure operations:

For security and compliance:

For deployment automation:

For cost and observability:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.