Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

Practice: Reviewing and Fixing Pulumi Programs

Practice Pulumi skills: find bugs in flawed programs, write a small review linter, predict previews and work through incident drills on drift, locks, replacements and secrets.

Reading about infrastructure code only gets you so far. The skills that matter in practice are review (spotting the bug before it ships) and diagnosis (knowing what to do when a deployment misbehaves). This final lesson of the chapter gives you both: bug-hunt exercises, a linter you can extend, a "predict the preview" drill and incident scenarios with model answers.

Pulumi itself cannot run on this site, so the flawed programs are read-only and the tools you run are JavaScript stand-ins.

Exercise 1: spot the problems

Read this program as a reviewer. There are eight problems. Write them down before reading on.

TypeScript
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const config = new pulumi.Config();
const dbPassword = "Sup3rS3cret!";                                    // (1)

export class Site extends pulumi.ComponentResource {
  constructor(name: string, args: any) {
    super("aws:web:Site", name, {}, undefined);                       // (2)

    const bucket = new aws.s3.BucketV2("bucket", {                    // (3)
      acl: "public-read",                                             // (4)
    });

    new aws.ec2.SecurityGroup(`${name}-ssh`, {
      ingress: [{ protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] }],   // (5)
    }, { parent: this });

    bucket.bucket.apply(b => {
      new aws.s3.BucketObjectv2("index", { bucket: b, content: "<h1>hi</h1>" });             // (6)
      console.log("password is " + dbPassword);                                              // (7)
    });
  }                                                                    // (8)
}

Answers:

  1. Hard-coded secret. Use config.requireSecret("dbPassword") and pulumi config set --secret.
  2. Type token uses the aws package (not yours) and opts is dropped; callers cannot pass provider, dependsOn or parent. Use "acme:web:Site" and pass opts.
  3. Child name "bucket" is not prefixed, so two Site instances collide, and the bucket has no parent: this.
  4. Public ACL: the bucket is open to the world. Keep it private and use a CDN with origin access control.
  5. SSH open to the internet. Restrict the CIDR or use SSM Session Manager.
  6. Resource created inside apply: it will not appear in previews. Pass bucket: bucket.bucket directly (an Input).
  7. Logging a secret, and console.log inside apply. Never print secrets.
  8. No registerOutputs call at the end of the constructor.

Exercise 2: write a review linter

Teams often automate the cheap review checks. This stand-in scans source text for known bad patterns; extend it with your own rules. It is a blunt instrument (regular expressions), but good for catching the obvious before a human looks. Real projects use policy packs on the resources, plus ESLint, for the same job.

Node.js Stand-in: a tiny infrastructure-code linter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Try adding a rule for a missing parent: this or a resource without tags. You will notice the limit of text matching quickly, which is why checks on the real resource inputs (policy packs, mock-based tests) are the long-term answer.

Exercise 3: predict the preview

Given recorded state and a changed program, predict what pulumi preview shows before running the stand-in below. Rules to apply: a changed mutable property is an update, a changed immutable property is a replace, a new resource is a create, and a resource missing from the program is a delete. In the stand-in, ami and engine are immutable.

Node.js Stand-in: check your preview prediction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check yourself: web is a replace (AMI is immutable), db is an update (instance class resizes in place for most engines), logs is a replace (bucket name is immutable), cache is a create and queue is a delete. Which of these would you want a human to approve? The replaced instance, and the replaced bucket if it holds data.

Exercise 4: incident drills

For each scenario, decide the next steps before reading the model answer.

Drill A: "The pipeline says the stack is locked." A deploy job was cancelled an hour ago. Answer: confirm no update is running (check CI and Pulumi Cloud history), run pulumi cancel, then pulumi refresh (resources may have been created without being recorded), review the preview, re-run up.

Drill B: "Preview wants to replace the production database." The PR only bumped a provider version. Answer: do not merge. Read the provider changelog; check --diff for the property that forces replacement; pin the old version or adjust the code to match the new behavior; confirm protect is on so an accidental apply would fail. Test the upgrade in a lower environment first.

Drill C: "Someone changed a security group in the console during an outage." Answer: pulumi preview --refresh --diff to see the drift; if the change was right, add it to the code and merge; if not, run pulumi up --refresh to revert. Add a scheduled drift check and tighten console permissions.

Drill D: "A secret appeared in a CI log." Answer: treat it as leaked: rotate the credential immediately, then find the leak (a console.log, an unmasked apply, --show-secrets output), fix it and consider a log scrubber. Rotating comes before cleaning up.

Drill E: "We renamed a resource in code and the preview shows delete and create for a stateful resource." Answer: revert the rename or add an alias pointing at the old name; run preview until it shows no replacement.

Drill F: "A teammate deleted a bucket in the console. The next pulumi up fails." Answer: run pulumi refresh to remove the missing resource from state; the next up recreates it (empty). Restore data from backups or versioning.

Node.js Stand-in: score your drill answers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

A personal checklist

Before you open a pull request for infrastructure:

  • Does pulumi preview show only the changes I expect?
  • Are there deletes or replaces I can explain?
  • Are secrets only in encrypted config and never printed?
  • Are resources tagged and named uniquely?
  • Are components given parent: this, registerOutputs and stable child names?
  • Do tests (unit, policy) pass, and did I add one for the new rule?
  • Do I know how to roll this back?

You now have the pieces to build, share, test, secure and operate Pulumi infrastructure. Revisit the chapters on components, state, testing and operations whenever a real problem shows up; they were written to be used as a reference.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is creating a resource inside apply a review finding?

    Resources created in apply callbacks are invisible to previews, defeating the main safety check.
  2. 2. A preview shows +- for a database after a provider upgrade. What is the first step?

    An unexpected replacement of stateful resources must be understood before any apply.
  3. 3. A secret was printed in a CI log. What comes first?

    Once exposed, the secret must be treated as compromised, so rotation comes before cleanup.
  4. 4. What is the safest way to rename a stateful resource's logical name?

    Aliases tell Pulumi the new name is the same resource, avoiding delete and create.

Summary

  • Review infrastructure with a checklist: secrets, public exposure, naming and parents, apply misuse, registerOutputs and previews.
  • Automate the cheap checks with a linter, and the deep ones with policy packs and mock-based tests.
  • Predict previews: mutable changes update, immutable changes replace, missing resources delete.
  • Drill the incident playbooks: locks, drift, unexpected replacements, leaked secrets, renames and manual deletes.
  • Keep a short personal pre-PR checklist and use the earlier lessons as a reference.

Next: back to the Pulumi track page