https://pulumi.com logo
#general
Title
# general
c

clever-kite-79772

08/02/2023, 12:17 PM
Hi all, Can someone help me with Pulumi and golang? How can I marshal
pulumi.StringOutput
and then use it in
ctx.Export
?
m

melodic-tomato-39005

08/02/2023, 2:18 PM
Hi, you should be able to use the
StringOutput
as is. An example with an S3 bucket,
Region
is a `StringOutput`:
Copy code
ctx.Export("bucketRegion", bucket.Region)
c

clever-kite-79772

08/02/2023, 2:25 PM
If I need to export map of objects, I get the error:
Copy code
./main.go:65:27: cannot use pairResources (variable of type map[pulumi.String]KeyPairExport) as pulumi.Input value in argument to ctx.Export: map[pulumi.String]KeyPairExport does not implement pulumi.Input (missing method ElementType)
or
Copy code
error: an unhandled error occurred: program failed:
waiting for RPCs: json: error calling MarshalJSON for type pulumi.StringOutput: Outputs can not be marshaled to JSON
m

melodic-tomato-39005

08/02/2023, 2:28 PM
Well, that’s different from StringOutput 🙂 What’s KeyPairExport? You might use
pulumi.Output()
on your map, or you might need to
range
over the map and export the elements.
c

clever-kite-79772

08/02/2023, 3:11 PM
This is the example what I try to do:
Copy code
type KeyPairExport struct {
	KeyName pulumi.StringOutput `pulumi:"key_name"`
	Id      pulumi.StringOutput `pulumi:"id"`
}

		pairResources := make(map[pulumi.String]KeyPairExport)
		for _, kp := range keyPairs {
			nkp, _ := ec2.NewKeyPair(ctx, kp.Name, &ec2.KeyPairArgs{
				KeyName:   pulumi.String(environment + "-key-pair-" + kp.Name),
				PublicKey: pulumi.String("ssh-rsa key"),
				Tags:      tags,
			})
			pairResources[pulumi.String(kp.Name)] = KeyPairExport{
				KeyName: nkp.KeyName,
				Id:      nkp.KeyPairId,
			}

		}

		ctx.Export("key-pairs", pairResources)
Is it possible to make it workable. That I can use dict of keys in other projects.
m

melodic-tomato-39005

08/02/2023, 3:40 PM
I believe
ctx.Export("key-pairs", pulumi.ToOutput(pairResources))
should work.
c

clever-kite-79772

08/02/2023, 4:19 PM
Thank you. I'll check.
Nope, the same 😞
Copy code
error: an unhandled error occurred: program failed:
waiting for RPCs: cannot marshal an input of type pulumi.StringOutput with element type string as a value of type pulumi.StringOutput
If comment
.Export
- it works.