https://pulumi.com logo
#golang
Title
# golang
e

echoing-librarian-76448

09/29/2021, 4:57 PM
I noticed that when trying to use
ctx.Export
inside of an
.ApplyT
function, the export doesn't happen. Is this the expected behavior? Here's a code example:
Copy code
aksVNet.ID().ToStringOutput().ApplyT(func(id string) error {
	applicationAKSSubnetID := fmt.Sprintf("%s/subnets/Application", id)

	var clusterInput *aks.ClusterInput
	if err := cfg.TryObject(appAKSClusterConfigKey, &clusterInput); err != nil {
		return err
	}

	cluster, err = aks.CreateAKSCluster(ctx,
		clusterInput,
		applicationAKSSubnetID,
		commonTags)
	if err != nil {
		return err
	}
	
	ctx.Export("clusterName", cluster.Name)

	return nil
})
b

bored-table-20691

09/29/2021, 4:58 PM
In general you should avoid creating resources and exports in apply
e

echoing-librarian-76448

09/29/2021, 5:00 PM
I do try to avoid it but there are times when I need a string value of a
pulumi.StringOutput
that I haven't found another way to access without
.ApplyT
you can see in the example that I'm trying to get a newly created VNet ID so I can build the subnet info. I wasn't been able to get the subnet ID directly
Is there a way to get a specific subnet's ID directly, even if it's a
pulumi.StringOutput
type? I could refactor our code to not need the plain string if it's possible.
b

bored-table-20691

09/29/2021, 7:49 PM
Which provider are you using?
l

lemon-agent-27707

09/30/2021, 1:00 AM
Exporting values from within apply is not supported and will not work.
👍 1
You can accomplish the same thing by running an apply, returning the value you want to export, and exporting the output returned by the apply. Some pseudocode:
Copy code
idOutput := val.Apply( v => ...someTransform.. return v.id);
ctx.export("idOutput", idOutput)
e

echoing-librarian-76448

10/06/2021, 2:56 PM
Sorry for the slow reply. I was able to get back to this today and ended up refactoring some functions to use
pulumi.StringInput
as a parameter instead of
String
I was able to avoid using
ApplyT
after making that switch. Thanks for the guidance on this
partypus 1
12 Views