jolly-activity-81321
09/04/2023, 7:44 AMpulumi.StringOutput
(EC2 instance public IP) to native string in Go.
I was trying with pulumi.Sprintf()
or with pulumi.ApplyT()
with no success:
stringValue := pulumi.String("Hello, World!").ToStringOutput()
fmt.Printf("%s", stringValue.ApplyT(func(s string) string {
return s
}))
melodic-tomato-39005
09/04/2023, 8:48 AMApply
is the correct approach, but the Printf
needs to be inside the callback function. See the example and explanation here.jolly-activity-81321
09/04/2023, 9:19 AMAll
, same thing, basicallystringValue := pulumi.String("Hello, World!").ToStringOutput()
fmt.Println(pulumi.All(stringValue).ApplyT(func(args []interface{}) (string, error) {
s := args[0]
return fmt.Sprintf("%v", s), nil
}).(pulumi.StringOutput))
//Outputs {0xc00047a460}
{0xc00047a460}
where I expected "Hello, World!"
melodic-tomato-39005
09/04/2023, 9:28 AMApply
returns again an Output
.All
is unnecessarily cumbersome if you have only one value, ApplyT
is easier.jolly-activity-81321
09/04/2023, 9:29 AMAll
🙂melodic-tomato-39005
09/04/2023, 9:30 AMHi Martins,?is the correct approachApply
jolly-activity-81321
09/04/2023, 9:35 AMstringValue := pulumi.String("Hello, World!").ToStringOutput()
original := stringValue.ApplyT(func(s string) (string, error) {
return fmt.Sprintf("%v", s), nil
})
fmt.Printf("the parsed string is: %s", original)
melodic-tomato-39005
09/04/2023, 9:46 AMApply
, it’s how Pulumi’s whole model of tracking dependencies works.Export
for when you want to use a value outside of your Pulumi program, or in another onejolly-activity-81321
09/04/2023, 1:36 PMs := pulumi.Sprintf("%s", stringValue)
would work and give me a meaningful value... it does not...Export
is for exporting the value for use in a different stack, isn't it?melodic-tomato-39005
09/04/2023, 1:46 PMpulumi.Sprintf
is a helper wrapping pulumi.Apply
. I think your examples above used fmt.Sprintf
, which is different. But yes, pulumi.Sprintf
with an argument of type pulumi.StringOutput
should work.jolly-activity-81321
09/04/2023, 1:47 PMstringValue := pulumi.String("Hello, World!").ToStringOutput()
s := pulumi.Sprintf("%s", stringValue)
fmt.Println(s)
{0xc0001a82a0}
melodic-tomato-39005
09/04/2023, 1:50 PMs
is again a StringOutput
, not a string
. Basically, the raw value wrapped by an Output cannot escape Apply
and friends. Your logic working with the raw values needs to live in the callbacks to `Apply`/`All`/etc.If you need to access an output’s plain value—for example, to compute a derived, new value, or because you want to log it—you have these options:
• Apply: a callback that receives the plain value, and computes a new output
• Lifting: directly read properties off an output value
• Interpolation: concatenate string outputs with other strings directly
jolly-activity-81321
09/04/2023, 2:33 PMprintf.go
from Pulumi's PoW?func main() {
stringValue := pulumi.String("Hello, World!").ToStringOutput()
s := pulumi.Sprintf("%s", stringValue)
fmt.Println(s)
}
melodic-tomato-39005
09/04/2023, 2:37 PMs
is a StringOutput, not a string, so you can’t print it. The print needs to happen inside an Apply
, i.e., in the `Apply`’s callback. Like in the example in the docs:
myPet.ID().ApplyT(func(id string) error {
fmt.Printf("Hello, %s!", id)
return nil
})
jolly-activity-81321
09/04/2023, 2:42 PMmelodic-tomato-39005
09/04/2023, 2:44 PMjolly-activity-81321
09/04/2023, 3:06 PMctx.Export("publicIp", instance.PublicIp)
to export the value. I am provisioning EC2 instances in this case.
2. And then you can access it after running `Up`:
upRes, err := stack.Up(ctx, stdoutStreamer)
if err != nil {
log.Fatal(err)
}
master.publicIp = fmt.Sprintf("%s", upRes.Outputs["publicIp"].Value)
melodic-tomato-39005
09/04/2023, 7:28 PMApply
/`All` callback functions.