patternbashpulumiTip
Pulumi TypeScript: define infrastructure as typed code with real programming constructs
Viewed 0 times
pulumi typescriptOutput<T>applyprogramming modeliacpulumi configresource names
nodejs
Problem
HCL-based tools like Terraform lack true programming constructs: no functions with logic, no for loops over computed values, no native type safety, and no IDE autocomplete. Complex infrastructure configurations become hard to abstract and test.
Solution
Use Pulumi with TypeScript to define infrastructure using real language features. Resources are classes, outputs are typed promises, and you can use loops, conditionals, and functions naturally.
import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";
const config = new pulumi.Config();
const environment = config.require("environment");
// Create multiple subnets with a loop
const azs = ["us-east-1a", "us-east-1b", "us-east-1c"];
const subnets = azs.map((az, i) =>
new aws.ec2.Subnet(`subnet-${az}`, {
vpcId: vpc.id,
availabilityZone: az,
cidrBlock: `10.0.${i + 1}.0/24`,
tags: { Environment: environment, Name: `subnet-${az}` },
})
);
// Conditional resource creation
const eip = environment === "prod"
? new aws.ec2.Eip("nat-eip", {})
: undefined;Why
Pulumi's programming model enables abstraction, reuse, and testing that are difficult or impossible in HCL. TypeScript provides compile-time type checking, IDE support, and access to the full npm ecosystem.
Gotchas
- Pulumi Output<T> types are not plain values — you must use .apply() or pulumi.interpolate to unwrap them
- Resource names must be unique within a stack — duplicate names cause errors at runtime, not compile time
- State is stored in Pulumi Service by default — configure a self-managed backend for on-prem use
- pulumi preview is the equivalent of terraform plan — always run it before pulumi up
Context
Teams comfortable with TypeScript who need richer abstraction than HCL provides
Revisions (0)
No revisions yet.