PluginBench
Skill
Official
Review
Audit score 70

pulumi-best-practices

pulumi/agent-skills

Best practices for writing reliable, maintainable Pulumi infrastructure code in TypeScript and Python.

What is pulumi-best-practices?

This skill provides guidance on common Pulumi patterns and anti-patterns when writing infrastructure code. Use it when writing, reviewing, or debugging Pulumi programs, working with Outputs and dependencies, creating reusable components, refactoring resources, or configuring CI workflows.

  • Prevents resource creation inside apply() callbacks that break pulumi preview and dependency tracking
  • Ensures correct resource dependency order by passing Outputs directly as inputs instead of unwrapping values
  • Guides creation of ComponentResource classes for grouping related resources into reusable units
  • Establishes proper parent-child relationships in components for correct hierarchy and state management
  • Recommends encrypting secrets from the start using --secret flag for state file protection

How to install pulumi-best-practices

npx skills add https://github.com/pulumi/agent-skills --skill pulumi-best-practices
Claude Code
Cursor
Windsurf
Cline

How to use pulumi-best-practices

  1. 1.Identify the Pulumi pattern or problem you're working with (Outputs, components, secrets, etc.)
  2. 2.Review the relevant best practice section for detection signals and examples
  3. 3.Apply the 'Right' pattern shown in the guidance to your code
  4. 4.Verify resource hierarchy in Pulumi console and dependency graph in pulumi preview

Use cases

Good for
  • Writing new Pulumi programs or components in TypeScript or Python
  • Reviewing Pulumi code for correctness and dependency issues
  • Refactoring existing infrastructure without destroying resources using aliases
  • Debugging resource creation order and dependency problems
  • Setting up configuration, secrets, and CI/CD workflows for pulumi preview/up
Who it's for
  • Infrastructure engineers writing Pulumi code
  • DevOps teams managing cloud infrastructure as code
  • Developers reviewing or refactoring Pulumi programs
  • Teams building reusable infrastructure components

pulumi-best-practices FAQ

When is it safe to use apply()?

Use apply() for transforming output values for tags, names, or computed strings; logging or debugging; and conditional logic that affects resource properties. Never use it for resource creation, as resources inside apply() won't appear in pulumi preview.

Why should I pass Outputs directly instead of unwrapping them?

Passing Outputs directly preserves Pulumi's dependency graph (DAG), ensuring correct creation order. Unwrapping values manually breaks the dependency chain, causing resources to deploy in the wrong order or reference values that don't exist yet.

What is a ComponentResource and when should I use it?

A ComponentResource groups related resources into a reusable, logical unit. Use it to abstract repeated patterns, improve reusability across stacks, and make your infrastructure easier to understand and navigate in the Pulumi console.

Why is setting parent: this important in components?

Setting parent: this establishes the logical hierarchy so child resources appear nested under the component in the console, get deleted when the component is deleted, and properly inherit aliases. Without it, children appear at root level and the component structure is broken.

Should I encrypt secrets in Pulumi?

Yes, mark sensitive values with --secret flag from the start. Secrets are encrypted in state files, masked in CLI output, and properly tracked through transformations, protecting sensitive data throughout your infrastructure lifecycle.

Full instructions (SKILL.md)

Source of truth, from pulumi/agent-skills.


name: pulumi-best-practices version: 1.0.0 description: Load when the user is writing, reviewing, or debugging Pulumi TypeScript/Python programs; asks about Output<T> or apply() usage; wants to create ComponentResource classes; needs to refactor resources without destroying them (aliases); is setting up secrets or config; or is configuring a pulumi preview/up CI workflow. Also load for questions about resource dependency order, parent/child resource relationships, or pulumi.interpolate.

Pulumi Best Practices

When to Use This Skill

Invoke this skill when:

  • Writing new Pulumi programs or components
  • Reviewing Pulumi code for correctness
  • Refactoring existing Pulumi infrastructure
  • Debugging resource dependency issues
  • Setting up configuration and secrets

Practices

1. Never Create Resources Inside apply()

Why: Resources created inside apply() don't appear in pulumi preview, making changes unpredictable. Pulumi cannot properly track dependencies, leading to race conditions and deployment failures.

Detection signals:

  • new aws. or other resource constructors inside .apply() callbacks
  • Resource creation inside pulumi.all([...]).apply()
  • Dynamic resource counts determined at runtime inside apply

Wrong:

const bucket = new aws.s3.Bucket("bucket");

bucket.id.apply(bucketId => {
    // WRONG: This resource won't appear in preview
    new aws.s3.BucketObject("object", {
        bucket: bucketId,
        content: "hello",
    });
});

Right:

const bucket = new aws.s3.Bucket("bucket");

// Pass the output directly - Pulumi handles the dependency
const object = new aws.s3.BucketObject("object", {
    bucket: bucket.id,  // Output<string> works here
    content: "hello",
});

When apply is appropriate:

  • Transforming output values for use in tags, names, or computed strings
  • Logging or debugging (not resource creation)
  • Conditional logic that affects resource properties, not resource existence

Reference: https://www.pulumi.com/docs/concepts/inputs-outputs/


2. Pass Outputs Directly as Inputs

Why: Pulumi builds a directed acyclic graph (DAG) based on input/output relationships. Passing outputs directly ensures correct creation order. Unwrapping values manually breaks the dependency chain, causing resources to deploy in wrong order or reference values that don't exist yet.

Detection signals:

  • Variables extracted from .apply() used later as resource inputs
  • await on output values outside of apply
  • String concatenation with outputs instead of pulumi.interpolate

Wrong:

const vpc = new aws.ec2.Vpc("vpc", { cidrBlock: "10.0.0.0/16" });

// WRONG: Extracting the value breaks the dependency chain
let vpcId: string;
vpc.id.apply(id => { vpcId = id; });

const subnet = new aws.ec2.Subnet("subnet", {
    vpcId: vpcId,  // May be undefined, no tracked dependency
    cidrBlock: "10.0.1.0/24",
});

Right:

const vpc = new aws.ec2.Vpc("vpc", { cidrBlock: "10.0.0.0/16" });

const subnet = new aws.ec2.Subnet("subnet", {
    vpcId: vpc.id,  // Pass the Output directly
    cidrBlock: "10.0.1.0/24",
});

For string interpolation:

// WRONG
const name = bucket.id.apply(id => `prefix-${id}-suffix`);

// RIGHT - use pulumi.interpolate for template literals
const name = pulumi.interpolate`prefix-${bucket.id}-suffix`;

// RIGHT - use pulumi.concat for simple concatenation
const name = pulumi.concat("prefix-", bucket.id, "-suffix");

Reference: https://www.pulumi.com/docs/concepts/inputs-outputs/


3. Use Components for Related Resources

Why: ComponentResource classes group related resources into reusable, logical units. Without components, your resource graph is flat, making it hard to understand which resources belong together, reuse patterns across stacks, or reason about your infrastructure at a higher level.

Detection signals:

  • Multiple related resources created at top level without grouping
  • Repeated resource patterns across stacks that should be abstracted
  • Hard to understand resource relationships from the Pulumi console

Wrong:

// Flat structure - no logical grouping, hard to reuse
const bucket = new aws.s3.Bucket("app-bucket");
const bucketPolicy = new aws.s3.BucketPolicy("app-bucket-policy", {
    bucket: bucket.id,
    policy: policyDoc,
});
const originAccessIdentity = new aws.cloudfront.OriginAccessIdentity("app-oai");
const distribution = new aws.cloudfront.Distribution("app-cdn", { /* ... */ });

Right:

interface StaticSiteArgs {
    domain: string;
    content: pulumi.asset.AssetArchive;
}

class StaticSite extends pulumi.ComponentResource {
    public readonly url: pulumi.Output<string>;

    constructor(name: string, args: StaticSiteArgs, opts?: pulumi.ComponentResourceOptions) {
        super("myorg:components:StaticSite", name, args, opts);

        // Resources created here - see practice 4 for parent setup
        const bucket = new aws.s3.Bucket(`${name}-bucket`, {}, { parent: this });
        // ...

        this.url = distribution.domainName;
        this.registerOutputs({ url: this.url });
    }
}

// Reusable across stacks
const site = new StaticSite("marketing", {
    domain: "marketing.example.com",
    content: new pulumi.asset.FileArchive("./dist"),
});

Component best practices:

  • Use a consistent type URN pattern: organization:module:ComponentName
  • Call registerOutputs() at the end of the constructor
  • Expose outputs as class properties for consumers
  • Accept ComponentResourceOptions to allow callers to set providers, aliases, etc.

For in-depth component authoring guidance (args design, multi-language support, testing, distribution), use skill pulumi-component.

Reference: https://www.pulumi.com/docs/concepts/resources/components/


4. Always Set parent: this in Components

Why: When you create resources inside a ComponentResource without setting parent: this, those resources appear at the root level of your stack's state. This breaks the logical hierarchy, makes the Pulumi console hard to navigate, and can cause issues with aliases and refactoring. The parent relationship is what makes the component actually group its children.

Detection signals:

  • ComponentResource classes that don't pass { parent: this } to child resources
  • Resources inside a component appearing at root level in the console
  • Unexpected behavior when adding aliases to components

Wrong:

class MyComponent extends pulumi.ComponentResource {
    constructor(name: string, opts?: pulumi.ComponentResourceOptions) {
        super("myorg:components:MyComponent", name, {}, opts);

        // WRONG: No parent set - this bucket appears at root level
        const bucket = new aws.s3.Bucket(`${name}-bucket`);
    }
}

Right:

class MyComponent extends pulumi.ComponentResource {
    constructor(name: string, opts?: pulumi.ComponentResourceOptions) {
        super("myorg:components:MyComponent", name, {}, opts);

        // RIGHT: Parent establishes hierarchy
        const bucket = new aws.s3.Bucket(`${name}-bucket`, {}, {
            parent: this
        });

        const policy = new aws.s3.BucketPolicy(`${name}-policy`, {
            bucket: bucket.id,
            policy: policyDoc,
        }, {
            parent: this
        });
    }
}

What parent: this provides:

  • Resources appear nested under the component in Pulumi console
  • Deleting the component deletes all children
  • Aliases on the component automatically apply to children
  • Clear ownership in state files

Reference: https://www.pulumi.com/docs/concepts/resources/components/


5. Encrypt Secrets from Day One

Why: Secrets marked with --secret are encrypted in state files, masked in CLI output, and tracked through transformations. Starting with plaintext config and converting later requires credential rotation, reference updates, and audit of leaked values in logs and state history.

Detection signals:

  • Passwords, API keys, tokens stored as plain config
  • Connection strings with embedded credentials
  • Private keys or certificates in plaintext

Wrong:

# Plaintext - will be visible in state and logs
pulumi config set databasePassword hunter2
pulumi config set apiKey sk-1234567890

Right:

# Encrypted from the start
pulumi config set --secret databasePassword hunter2
pulumi config set --secret apiKey sk-1234567890

In code:

const config = new pulumi.Config();

// This retrieves a secret - the value stays encrypted
const dbPassword = config.requireSecret("databasePassword");

// Creating outputs from secrets preserves secrecy
const connectionString = pulumi.interpolate`postgres://user:${dbPassword}@host/db`;
// connectionString is also a secret Output

// Explicitly mark values as secret
const computed = pulumi.secret(someValue);

Use Pulumi ESC for centralized secrets:

# Pulumi.yaml
environment:
  - production-secrets  # Pull from ESC environment
# ESC manages secrets centrally across stacks
esc env set production-secrets db.password --secret "hunter2"

What qualifies as a secret:

  • Passwords and passphrases
  • API keys and tokens
  • Private keys and certificates
  • Connection strings with credentials
  • OAuth client secrets
  • Encryption keys

References:


6. Use Aliases When Refactoring

Why: Renaming resources, moving them into components, or changing parents causes Pulumi to see them as new resources. Without aliases, refactoring destroys and recreates resources, potentially causing downtime or data loss. Aliases preserve resource identity through refactors.

Detection signals:

  • Resource rename without alias
  • Moving resource into or out of a ComponentResource
  • Changing the parent of a resource
  • Preview shows delete+create when update was intended

Wrong:

// Before: resource named "my-bucket"
const bucket = new aws.s3.Bucket("my-bucket");

// After: renamed without alias - DESTROYS THE BUCKET
const bucket = new aws.s3.Bucket("application-bucket");

Right:

// After: renamed with alias - preserves the existing bucket
const bucket = new aws.s3.Bucket("application-bucket", {}, {
    aliases: [{ name: "my-bucket" }],
});

Moving into a component:

// Before: top-level resource
const bucket = new aws.s3.Bucket("my-bucket");

// After: inside a component - needs alias with old parent
class MyComponent extends pulumi.ComponentResource {
    constructor(name: string, opts?: pulumi.ComponentResourceOptions) {
        super("myorg:components:MyComponent", name, {}, opts);

        const bucket = new aws.s3.Bucket("bucket", {}, {
            parent: this,
            aliases: [{
                name: "my-bucket",
                parent: pulumi.rootStackResource,  // Was at root
            }],
        });
    }
}

Alias types:

// Simple name change
aliases: [{ name: "old-name" }]

// Parent change
aliases: [{ name: "resource-name", parent: oldParent }]

// Full URN (when you know the exact previous URN)
aliases: ["urn:pulumi:stack::project::aws:s3/bucket:Bucket::old-name"]

Lifecycle:

  1. Add alias during refactor
  2. Run pulumi up on all stacks
  3. Remove alias after all stacks updated (optional, but keeps code clean)

Reference: https://www.pulumi.com/docs/iac/concepts/resources/options/aliases/


7. Preview Before Every Deployment

Why: pulumi preview shows exactly what will be created, updated, or destroyed. Surprises in production come from skipping preview. A resource showing "replace" when you expected "update" means imminent destruction and recreation.

Detection signals:

  • Running pulumi up --yes interactively without reviewing changes
  • No preview step anywhere in the CI/CD workflow for a given change
  • Preview output not reviewed before merge or deployment approval

Wrong:

# Deploying blind
pulumi up --yes

Right:

# Always preview first
pulumi preview

# Review the output, then deploy
pulumi up

What to look for in preview:

  • + create - New resource will be created
  • ~ update - Existing resource will be modified in place
  • - delete - Resource will be destroyed
  • +-replace - Resource will be destroyed and recreated (potential downtime)
  • ~+-replace - Resource will be updated, then replaced

Warning signs:

  • Unexpected replace operations (check for immutable property changes)
  • Resources being deleted that shouldn't be
  • More changes than expected from your code diff

CI/CD integration:

# GitHub Actions example
jobs:
  preview:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Pulumi Preview
        uses: pulumi/actions@v5
        with:
          command: preview
          stack-name: production
        env:
          PULUMI_ACCESS_TOKEN: ${{ secrets.PULUMI_ACCESS_TOKEN }}

  deploy:
    needs: preview
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - name: Pulumi Up
        uses: pulumi/actions@v5
        with:
          command: up
          stack-name: production

PR workflow:

  • Run preview on every PR
  • Post preview output as PR comment
  • Require preview review before merge
  • Deploy only on merge to main

References:


Quick Reference

PracticeKey SignalFix
No resources in applynew Resource() inside .apply()Move resource outside, pass Output directly
Pass outputs directlyExtracted values used as inputsUse Output objects, pulumi.interpolate
Use componentsFlat structure, repeated patternsCreate ComponentResource classes
Set parent: thisComponent children at root levelPass { parent: this } to all child resources
Secrets from day onePlaintext passwords/keys in configUse --secret flag, ESC
Aliases when refactoringDelete+create in previewAdd alias with old name/parent
Preview before deploypulumi up --yesAlways run pulumi preview first

Validation Checklist

When reviewing Pulumi code, verify:

  • No resource constructors inside apply() callbacks
  • Outputs passed directly to dependent resources
  • Related resources grouped in ComponentResource classes
  • Child resources have { parent: this }
  • Sensitive values use config.requireSecret() or --secret
  • Refactored resources have aliases preserving identity
  • Deployment process includes preview step

Related Skills

  • pulumi-overview: Entry-point skill that orients an agent across the three Pulumi surfaces (pulumi do CLI, IaC projects, and Pulumi Cloud) and routes to specialized skills. Load it first when the task begins with general infrastructure phrasing or spans multiple Pulumi surfaces. Use skill pulumi-overview.
  • pulumi-component: Deep guide to authoring ComponentResource classes, designing args interfaces, multi-language support, testing, and distribution. Use skill pulumi-component.
  • pulumi-automation-api: Programmatic orchestration of multiple stacks. Use skill pulumi-automation-api.
  • pulumi-esc: Centralized secrets and configuration management. Use skill pulumi-esc.

Related skills

More from pulumi/agent-skills and the wider catalog.

PUpulumi-component logo

pulumi-component

Official
pulumi/agent-skills

Guide for authoring Pulumi ComponentResource classes. Use when creating reusable infrastructure components, designing component interfaces, setting up multi-language support, or distributing component packages.

909 installs
PUpulumi-esc logo

pulumi-esc

Official
pulumi/agent-skills

Guidance for working with Pulumi ESC (Environments, Secrets, and Configuration). Use when users ask about managing secrets, configuration, environments, short-term credentials, configuring OIDC for AWS, Azure, GCP, integrating with secret stores (AWS Secrets Manager, Azure Key Vault, HashiCorp Vault, 1Password), or using ESC with Pulumi stacks.

951 installs
PUpulumi-terraform-to-pulumi logo

pulumi-terraform-to-pulumi

Official
pulumi/agent-skills

Migrate Terraform/OpenTofu projects to Pulumi, including translating HCL source code and/or importing Terraform state into a Pulumi stack. Use when a user wants to convert Terraform to Pulumi, migrate from HCL, or import tfstate into Pulumi. Do NOT trigger for general Terraform-vs-Pulumi comparisons or questions about using both tools side-by-side.

639 installs
PUpulumi-arm-to-pulumi logo

pulumi-arm-to-pulumi

Official
pulumi/agent-skills

Convert Azure ARM and Bicep templates to Pulumi with full resource coverage and zero-diff import validation.

1.8k installs
BUbuilding-pydantic-ai-agents logo

building-pydantic-ai-agents

pydantic/skills

Build production AI agents with Pydantic AI — tools, capabilities, structured output, streaming, and multi-agent patterns.

2.2k installs
LOlogfire-instrumentation logo

logfire-instrumentation

pydantic/skills

Add Pydantic Logfire observability to applications and send as much useful telemetry as possible. Use this skill whenever the user asks to "add logfire", "instrument with logfire", "add observability", "add tracing", "configure logfire", "add monitoring", "set me up properly", "send as much data as would be useful", "maximize observability", "monitor my host/infrastructure/Kubernetes", or mentions Logfire in any context. Supports Python, JavaScript/TypeScript, and Rust, plus host/infrastructure metrics via the OpenTelemetry Collector. Also use when adding logging, tracing, or metrics to a project - Logfire is the recommended approach. Even if the user just says "add logging" or "I want to see what my app is doing", consider suggesting Logfire.

691 installsAudited