Building component resources in Go #11694
Replies: 2 comments
-
I ended up finding https://github.com/pulumi/workshops/tree/09d39e18662769a3008d985bafa577e56219f009/building-reusable-abstractions/2-component-resources/go after doing some global GitHub search which explains a great deal. I'm still somewhat confused about the |
Beta Was this translation helpful? Give feedback.
-
I ended up asking ChatGPT, which at least solved my question on the inputs 😄.
Sure! A Component Resource in Pulumi is a way to group related resources into a reusable unit, making it easier to manage and deploy your infrastructure. Here's an example of a simple Component Resource in Go: package main
import (
"github.com/pulumi/pulumi-aws/sdk/v2/go/aws/ec2"
"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := NewVpc(ctx, "example-vpc", &VpcArgs{
CidrBlock: pulumi.String("10.0.0.0/16"),
})
if err != nil {
return err
}
return nil
})
}
type VpcArgs struct {
CidrBlock pulumi.StringInput
}
func NewVpc(ctx *pulumi.Context, name string, args *VpcArgs) (*ec2.Vpc, error) {
vpc, err := ec2.NewVpc(ctx, name, &ec2.VpcArgs{
CidrBlock: args.CidrBlock,
})
if err != nil {
return nil, err
}
return vpc, nil
} In this example, we have created a new Component Resource called NewVpc that creates an AWS VPC. The NewVpc function takes in a Pulumi context, a name for the resource, and an instance of VpcArgs which allows us to pass in the CIDR block for the VPC. The NewVpc function then creates an instance of an AWS VPC using the ec2.NewVpc function from the pulumi-aws library. The created VPC is returned from the function so it can be used elsewhere in your code. To use this Component Resource, you would call the NewVpc function in your main code and pass in the necessary arguments. This allows you to abstract away the details of creating a VPC, making your code easier to read and maintain.
The key in the ProviderMap should be named based on the package name of the resource provider that you want to inherit from the parent resource. For example, if you want to inherit the AWS provider from the parent resource, the key in the ProviderMap should be named "aws" because the AWS provider is defined in the "github.com/pulumi/pulumi-aws/sdk/go/aws" package. |
Beta Was this translation helpful? Give feedback.
-
I've started to take a look at Component Resources and as a concept they make sense to me, but the documentation of how to (g|G)o about them has left me a bit confused. I'm going based on this: https://www.pulumi.com/docs/intro/concepts/resources/components/ and using the Go programming language.
There's a couple of things I'm struggling with:
RegisterOutputs
shows an example of adding a key namedbucketDnsName
with the value ofbucket.BucketDomainName
. What if I create 2 buckets?pulumi.ProviderMap
should be? For example, why is itkubernetes
and notk8s
?MyComponentArgs
struct I should make and not explode the constructor arguments?If the AWS Quickstart VPC isn't what I should be looking at, does anyone have a real world example of a component resource in Go in a public repo they would be willing to share?
Beta Was this translation helpful? Give feedback.
All reactions