This message was deleted.
# general
s
This message was deleted.
l
Where do you need to use them? Pulumi does it automatically, most of the time.
If you just want to output a string to logs, do it inside an apply:
Copy code
outputStr.apply(str => <http://pulumi.log.info|pulumi.log.info>(`Your message is ${str}`));
Often you can avoid needing a string by passing around a
pulumi.Input<string>
. Maybe we can help arrange your code to do that?
m
I need to grab a value from an object Pulumi is creating, in particular a command:
Copy code
redJoinCommand := redcluster.ClusterRegistrationToken.Command()
I'd like to put this into a string var so I can construct a script I can pass as userdata to my ec2 instance:
Copy code
replacer := strings.NewReplacer("$redcommand", redJoinCommand,)
But I can't do this directly due to:
Copy code
Cannot use 'redJoinCommand' (type pulumi.StringPtrOutput) as type string
l
You're setting userData via the aws.ec2.Instance args parameter?
You'll see in the API that userData takes a
pulumi.Input<string>
value (or whatever the Python equivalent is), so you don't need to convert from that for userData. You do need some way to do call NewReplacer on it, and that takes a string.
m
Well first I'd like to template a bash script, do some string replacing and base64 encode it, then pass the result to the ec2 instance
l
So you can do all that in an
apply
that you pass to the ec2 instance.
m
Could you elaborate on that please, apologies I'm rather new to Pulumi.
l
So instead of this sort of thing (excuse the typescript):
Copy code
new aws.ec2.Instance("myName", {
   userData: calculatedString,
   ...
}, {});
You do this sort of thing:
Copy code
new aws.ec2.Instance("myName", {
   userData: redJoinCommand.apply(redJoinCommand => calculateString(redJoinCommand)),
   ...
}, {});
I'll see if I can figure out the Python equivalent.
w
@mammoth-honey-6147 this topic might be useful if you havenโ€™t read it yet: https://www.pulumi.com/docs/intro/concepts/inputs-outputs/
l
I see an example on lines 231 to 246 of this file: https://github.com/pulumi/examples/blob/master/aws-py-voting-app/__main__.py Where a _registry___id_ (an Output) is de-output-ized to be used with the AWS SDK (as a string), and the resulting ImageRegistry is used when creating an Image.
m
Thanks both, I'll see if I can apply that to my go app
l
Oh that's go.. shows what I know. A golang example is on lines 55 to 79 of https://github.com/pulumi/examples/blob/master/aws-go-appsync/main.go
And that one is pure string manipulation, so probably closer to what you want.
m
Oooh, thanks ๐Ÿ™‚
๐Ÿ‘ 1
Sorry @little-cartoon-10569 trying to wrap my head around this with:
Copy code
commandString := redcluster.ClusterRegistrationToken.Command().ApplyT(func(command string) (string, error) {
				return string(command), nil
		})
Where redcluster.ClusterRegistrationToken.Command() is of type
StringPtrOutput
But Pulumi panics with
panic: applier must have 1 input parameter assignable from *string
l
Is that because you've got a
string
parameter to your handler func, instead of a
*string
/
StringPtr
?
I don't know golang, but C++ would have complained about that.
m
d'oh. Of course. Thanks ๐Ÿ™‚
๐Ÿ‘ 1
So I have the following but the type of
temp
is
pulumi.StringOutput
vs what I need which is just a
string
- any ideas?
Copy code
temp := redcluster.ClusterRegistrationToken.Command().ApplyT(func(command *string) (string, error) {
				return command, nil
		})
		
		fmt.Println(reflect.TypeOf(temp))
l
You can pass temp straight into the EC2 instance's userData
The thing you return from your handler func should be the entire user data. If not, you'll have to do another apply and another handler func.
m
Ah, gotcha. That makes more sense. Thanks a lot for your help, really appreciate it.
๐Ÿ‘ 1
355 Views