Hey Good Evening, I am just working my way through...
# general
f
Hey Good Evening, I am just working my way through a simple AWS VPC setup with Pulumi / aws-go, I was had the following issue though, I can create a “Create New VPC” function with a return type of the ec2.Vpc. I can use this return type and from this return, pass in the ID() method to my other “Create New Subnets” function. This is great and works as expected !  However, I would like my code to be a little more flexible and check if there is an existing vpc passed it as a config item
Copy code
Vpc:id: "vpcid1234"
If there is a VPC:id set then don’t run the first “Create New VPC” function, instead just use this string value as my VPC id for the “Create New Subnets” function.  The issue I have run into is I am now dealing with a different type. So for the “Create new subnets function to take in the VPC from the ID() method then I can have func like this:
Copy code
func CreateSubnets(ctx *pulumi.Context, vpcID pulumi.IDOutput) { << vpcID is type pulumi.IDOoutput
   //creates subnet(s)
Now for a string ID taken from the config object I need it to look like this
Copy code
func CreateSubnets(ctx *pulumi.Context, vpcID string {
   //creates subnet(s)
My question is am I tackling this the wrong way and if so what would be another approach? It feels like I am. I do of course appreciate this is a Go Type issue rather than a strictly Pulumi one :)
l
Right approach to creation. The ID of a Vpc is an Input, which is a union of string and IDOutput. You can use either. However, you shouldn't need to check if the resource already exists: that's Pulumi's job.
If you used Pulumi to create it originally, then Pulumi still knows about it and won't create it again. This is part of the declarative approach to resource provisioning.
If the resource was created outside of Pulumi, then instead of conditionally creating the resource, the appropriate thing to do is to unconditionally import it once, then allow Pulumi to manage the resource from then on.
f
Ah okay I see - yes this was my approach for cases where we have a VPC created outside of pulumi. So conditional creation isn't a workable solution. Well thank you for the clearing that up @little-cartoon-10569
b
yep, agree with tenwit here. just to clarify though, there is a way of doing this. just make the
CreateSubnet
function take
IDOutput
and then check if the vpc exists in the calling code
l
It's workable, but you'd need to wrap almost your entire Pulumi program in conditions. It doesn't fit the declarative model. Pulumi isn't imperative: you don't ask Pulumi to do something like create a VPC. You declare something: you ask it to ensure that the the VPC exists.
b
some thing like:
Copy code
var vpc *vpc
if config.CreateNewVPC {
  // create a new VPC
  vpc = aws.NewVPC
} else {
  vpc = aws.LookupVPC
}

subnets = CreateSubnets(ctx, vpc.ID()
f
thank you both
useful info
b
also, you probably want to make your function ComponentResources
f
oh okay i better find out what that means! googles
b
f
ta