How do I register outputs in a Pulumi ComponentResource?
In Pulumi, a ComponentResource
is a way to create reusable and encapsulated pieces of infrastructure. When creating a ComponentResource
, you can register outputs that can be used by other resources or components. This is useful for exposing values like IDs, ARNs, or URLs that are generated within the component.
Here, we’ll create a ComponentResource
that sets up an S3 bucket and registers its name and ARN as outputs.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Define a custom ComponentResource for an S3 bucket
class S3BucketComponent extends pulumi.ComponentResource {
public readonly bucketName: pulumi.Output<string>;
public readonly bucketArn: pulumi.Output<string>;
constructor(name: string, args: S3BucketArgs, opts?: pulumi.ComponentResourceOptions) {
super("custom:resource:S3BucketComponent", name, {}, opts);
// Create an S3 bucket
const bucket = new aws.s3.Bucket(name, {
bucket: args.bucketName,
}, { parent: this });
// Register the bucket name and ARN as outputs
this.bucketName = bucket.bucket;
this.bucketArn = bucket.arn;
// Register the outputs with Pulumi
this.registerOutputs({
bucketName: this.bucketName,
bucketArn: this.bucketArn,
});
}
}
// Define input arguments for the S3BucketComponent
interface S3BucketArgs {
bucketName: pulumi.Input<string>;
}
// Create an instance of the S3BucketComponent
const myBucket = new S3BucketComponent("myBucket", { bucketName: "my-pulumi-bucket" });
// Export the bucket name and ARN
export const bucketName = myBucket.bucketName;
export const bucketArn = myBucket.bucketArn;
In this example:
- We define a
ComponentResource
namedS3BucketComponent
. - The
S3BucketComponent
creates an S3 bucket using theaws.s3.Bucket
resource. - We register the bucket’s name and ARN as outputs using
this.registerOutputs
. - Finally, we create an instance of
S3BucketComponent
and export its outputs.
This setup allows other parts of your Pulumi program to easily access the bucket’s name and ARN.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.