Define Cloud Infrastructure with Pulumi Using Programming Language Constructs
Generate a complete Pulumi infrastructure program with reusable components, stack configs, and cloud best practices in your language.
๐ The Prompt
Write a Pulumi program in [LANGUAGE (e.g., TypeScript, Python, Go, C#)] that provisions [INFRASTRUCTURE_DESCRIPTION (e.g., a Kubernetes cluster with managed node groups, a serverless API with Lambda and API Gateway, a data pipeline with S3, Glue, and Athena)] on [CLOUD_PROVIDER (e.g., AWS, Azure, GCP)].
Project Setup:
- Pulumi project name: [PROJECT_NAME]
- Stack names: [STACKS (e.g., dev, staging, prod)]
- State backend: [BACKEND (e.g., Pulumi Cloud, S3, Azure Blob)]
Infrastructure Requirements:
1. **Networking**: [NETWORK_DETAILS (e.g., VPC with 3 public and 3 private subnets across AZs, or use existing VPC ID)].
2. **Compute/Service**: [COMPUTE_DETAILS (e.g., EKS 1.28 with t3.medium nodes, min 2 max 10 autoscaling)].
3. **Storage**: [STORAGE_DETAILS (e.g., encrypted S3 bucket with lifecycle rules, RDS with automated backups)].
4. **Security**: [SECURITY_DETAILS (e.g., IAM roles with OIDC, security groups, KMS keys for encryption)].
Code Structure โ generate the following:
1. **Pulumi.yaml** and **Pulumi.[STACK].yaml**: Project definition and per-stack configuration values.
2. **index.ts / __main__.py / main.go**: Main infrastructure code organized with:
- ComponentResource classes/functions that encapsulate related resources into reusable abstractions.
- Pulumi Config for stack-specific settings (instance sizes, feature flags, domain names).
- Strong typing for all resource inputs and outputs.
- Proper use of `pulumi.Output` transformations and `apply()` for derived values.
3. **config.ts / config.py**: Centralized configuration loading with defaults and validation.
4. **Stack outputs**: Export all critical endpoints, ARNs, and connection strings.
Best Practices to Follow:
- Use `ComponentResource` to group related resources and avoid monolithic code.
- Implement tagging via a helper function that applies standard tags to all resources.
- Handle secrets using `pulumi.secret()` and never hardcode credentials.
- Add `protect: true` for stateful resources (databases, storage).
- Include error handling for missing configuration values.
Provide the complete, runnable code with comments explaining the Pulumi-specific patterns used (Output chaining, ComponentResource lifecycle, stack references).
๐ก Tips for Better Results
Specify your preferred language in [LANGUAGE] since Pulumi's patterns differ significantly between TypeScript and Python. Use ComponentResource to break infrastructure into testable units โ treat them like classes in application code. Always set `protect: true` on databases and persistent storage to prevent accidental deletion during stack updates.
๐ฏ Use Cases
Platform engineers and full-stack developers use this when they prefer defining infrastructure with familiar programming languages instead of DSLs, especially for complex deployments requiring loops, conditionals, and reusable abstractions.