Pulumi

Infrastructure as code written in general-purpose languages like TypeScript, Python or Go, with real loops, functions and tests.

Intermediate · about 10 hours · difficulty 3/5

Learn first: Infrastructure as Code Principles

Pulumi takes the same declarative model as Terraform (desired state, a preview, then an update) but lets you write it in TypeScript, Python, Go, C# or Java instead of HCL.

Why teams pick it

  • Real programming constructs: functions, classes, loops, packages from your language's ecosystem.
  • Unit tests with your normal test framework.
  • Component resources package patterns (a "service" with its load balancer, DNS and alarms) as ordinary classes.
  • Developers who already write TypeScript or Python can contribute without learning a new language.

Core concepts

  • A project holds code; a stack is one deployed instance of it (dev, staging, prod) with its own config.
  • pulumi preview shows the diff, pulumi up applies it.
  • Outputs are values known only after deployment; use apply to transform them.
  • Secrets in stack config are encrypted.
  • State lives in Pulumi Cloud or a self-managed backend such as a bucket.

Trade-offs

Flexibility cuts both ways: code can become too clever to review. Keep infrastructure programs boring, avoid side effects, and still review the preview in pull requests. The ecosystem and hiring pool are smaller than Terraform's, though Pulumi can use many Terraform providers.

Good enough to move on: you can create a stack per environment, build a reusable component in your language of choice, and write a unit test that asserts a bucket is not public.

Key concepts

  • Projects and stacks
  • preview and up
  • Inputs, outputs and apply
  • Component resources
  • Stack config and secrets
  • Unit testing infrastructure

Other options for this step

  • Terraform and OpenTofu: Provision cloud resources declaratively with HCL, providers, state and modules, using the most widely adopted IaC tool.
  • CloudFormation, CDK and Bicep: The cloud providers' own IaC tools: AWS CloudFormation and CDK, and Azure Bicep, deeply integrated with a single cloud.

Mini project

Write a Pulumi program in TypeScript or Python that creates a static website bucket with a CDN, packaged as a component, plus a unit test that fails if the bucket allows public writes.

Check yourself

Why can you not simply print a resource's ID inside a Pulumi program before deployment, and what mechanism handles that?

Free resources