$npx -y skills add pulumi/agent-skills --skill pulumi-componentGuide for authoring Pulumi ComponentResource classes. Use when creating reusable infrastructure components, designing component interfaces, setting up multi-language support, or distributing component packages.
| 1 | # Authoring Pulumi Components |
| 2 | |
| 3 | A ComponentResource groups related infrastructure resources into a reusable, logical unit. Components make infrastructure easier to understand, reuse, and maintain. Components appear as a single node with children nested underneath in `pulumi preview`/`pulumi up` output and in the Pulumi Cloud console. |
| 4 | |
| 5 | This skill covers the full component authoring lifecycle. For general Pulumi coding patterns (Output handling, secrets, aliases, preview workflows), use the `pulumi-best-practices` skill instead. |
| 6 | |
| 7 | ## When to Use This Skill |
| 8 | |
| 9 | Invoke this skill when: |
| 10 | |
| 11 | - Creating a new ComponentResource class |
| 12 | - Designing the args interface for a component |
| 13 | - Making a component consumable from multiple Pulumi languages |
| 14 | - Publishing or distributing a component package |
| 15 | - Refactoring inline resources into a reusable component |
| 16 | - Debugging component behavior (missing outputs, stuck creating, children at wrong level) |
| 17 | |
| 18 | ## Component Anatomy |
| 19 | |
| 20 | Every component has four required elements: |
| 21 | |
| 22 | 1. **Extend ComponentResource** and call `super()` with a type URN |
| 23 | 2. **Accept standard parameters**: name, args, and `ComponentResourceOptions` |
| 24 | 3. **Set `parent: this`** on all child resources |
| 25 | 4. **Call `registerOutputs()`** at the end of the constructor |
| 26 | |
| 27 | ### TypeScript |
| 28 | |
| 29 | ```typescript |
| 30 | import * as pulumi from "@pulumi/pulumi"; |
| 31 | import * as aws from "@pulumi/aws"; |
| 32 | |
| 33 | interface StaticSiteArgs { |
| 34 | indexDocument?: pulumi.Input<string>; |
| 35 | errorDocument?: pulumi.Input<string>; |
| 36 | } |
| 37 | |
| 38 | class StaticSite extends pulumi.ComponentResource { |
| 39 | public readonly bucketName: pulumi.Output<string>; |
| 40 | public readonly websiteUrl: pulumi.Output<string>; |
| 41 | |
| 42 | constructor(name: string, args: StaticSiteArgs, opts?: pulumi.ComponentResourceOptions) { |
| 43 | // 1. Call super with type URN: <package>:<module>:<type> |
| 44 | super("myorg:index:StaticSite", name, {}, opts); |
| 45 | |
| 46 | // 2. Create child resources with parent: this |
| 47 | const bucket = new aws.s3.Bucket(`${name}-bucket`, {}, { parent: this }); |
| 48 | |
| 49 | const website = new aws.s3.BucketWebsiteConfigurationV2(`${name}-website`, { |
| 50 | bucket: bucket.id, |
| 51 | indexDocument: { suffix: args.indexDocument ?? "index.html" }, |
| 52 | errorDocument: { key: args.errorDocument ?? "error.html" }, |
| 53 | }, { parent: this }); |
| 54 | |
| 55 | // 3. Expose outputs as class properties |
| 56 | this.bucketName = bucket.id; |
| 57 | this.websiteUrl = website.websiteEndpoint; |
| 58 | |
| 59 | // 4. Register outputs -- always the last line |
| 60 | this.registerOutputs({ |
| 61 | bucketName: this.bucketName, |
| 62 | websiteUrl: this.websiteUrl, |
| 63 | }); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Usage |
| 68 | const site = new StaticSite("marketing", { |
| 69 | indexDocument: "index.html", |
| 70 | }); |
| 71 | export const url = site.websiteUrl; |
| 72 | ``` |
| 73 | |
| 74 | ### Python |
| 75 | |
| 76 | ```python |
| 77 | import pulumi |
| 78 | import pulumi_aws as aws |
| 79 | |
| 80 | class StaticSiteArgs: |
| 81 | def __init__(self, |
| 82 | index_document: pulumi.Input[str] = "index.html", |
| 83 | error_document: pulumi.Input[str] = "error.html"): |
| 84 | self.index_document = index_document |
| 85 | self.error_document = error_document |
| 86 | |
| 87 | class StaticSite(pulumi.ComponentResource): |
| 88 | bucket_name: pulumi.Output[str] |
| 89 | website_url: pulumi.Output[str] |
| 90 | |
| 91 | def __init__(self, name: str, args: StaticSiteArgs, |
| 92 | opts: pulumi.ResourceOptions = None): |
| 93 | super().__init__("myorg:index:StaticSite", name, None, opts) |
| 94 | |
| 95 | bucket = aws.s3.Bucket(f"{name}-bucket", |
| 96 | opts=pulumi.ResourceOptions(parent=self)) |
| 97 | |
| 98 | website = aws.s3.BucketWebsiteConfigurationV2(f"{name}-website", |
| 99 | bucket=bucket.id, |
| 100 | index_document=aws.s3.BucketWebsiteConfigurationV2IndexDocumentArgs( |
| 101 | suffix=args.index_document, |
| 102 | ), |
| 103 | error_document=aws.s3.BucketWebsiteConfigurationV2ErrorDocumentArgs( |
| 104 | key=args.error_document, |
| 105 | ), |
| 106 | opts=pulumi.ResourceOptions(parent=self)) |
| 107 | |
| 108 | self.bucket_name = bucket.id |
| 109 | self.website_url = website.website_endpoint |
| 110 | |
| 111 | self.register_outputs({ |
| 112 | "bucket_name": self.bucket_name, |
| 113 | "website_url": self.website_url, |
| 114 | }) |
| 115 | |
| 116 | site = StaticSite("marketing", StaticSiteArgs()) |
| 117 | pulumi.export("url", site.website_url) |
| 118 | ``` |
| 119 | |
| 120 | ### Type URN Format |
| 121 | |
| 122 | The first argument to `super()` is the type URN: `<package>:<module>:<type>`. |
| 123 | |
| 124 | | Segment | Convention | Example | |
| 125 | |---------|-----------|---------| |
| 126 | | package | Organization or package name | `myorg`, `acme`, `pkg` | |
| 127 | | module | Usually `index` | `index` | |
| 128 | | type | PascalCase class name | `StaticSite`, `VpcNetwork` | |
| 129 | |
| 130 | Full examples: `myorg:index:StaticSite`, `acme:index:KubernetesCluster` |
| 131 | |
| 132 | ### registerOutputs Is Required |
| 133 | |
| 134 | **Why**: Without `registerOutputs()`, the c |