I know the answer is going to be "use `Apply`", bu...
# golang
I know the answer is going to be "use `Apply`", but I'm having some trouble with the specifics. 🙂 I'm trying to get the IP address of an instance launched on EC2 as a string. So far I've tried
variations of this snippet:
Copy code
ipAddrList[0] = bastion.PrivateIp.ApplyString(func(ipAddr string) string {
	return string(ipAddr)
What am I missing?
👀 1
what error are you getting?
I haven't actually run it yet; I'm thinking it doesn't work because VS Code's linting is reporting this:
cannot use bastion.PrivateIp.ApplyString((func(ipAddr string) string literal)) (value of type pulumi.StringOutput) as string value in assignment
we do this a lot in our examples
notice the use of sprintf in there so it's definitely strings
It's not just VS Code...I just ran
pulumi preview
and it reported the same error ("cannot use bastion.PrivateIp.OutputState.ApplyString(func literal) (type pulumi.StringOutput) as type string in assignment")
@billowy-army-68599 any thoughts on what I’ve missed here?
what is
as a type?
can you show me the rest of your code? are you trying to build an array?
From the error it sounds like
is a
and you are trying to assign a
to one of it's elements which would not work. If you share a full code snippet with context I'm sure we can get something working.
yeah that was my thought too 🙂
You are spot on. I am trying to launch a group of instances and collect their IP addresses in an array that I can iterate over to write the IP addresses out to a file.
Taking a break for family for a bit, but I'll post more code for context later.
Here's a bit more code for context:
Copy code
// Launch an EC2 instance to serve as bastion host
		bastion, err := ec2.NewInstance(ctx, "bastion", &ec2.InstanceArgs{
			Ami:                      pulumi.String(amiID.Id),
			InstanceType:             pulumi.String("t2.small"),
			AssociatePublicIpAddress: pulumi.Bool(true),
			KeyName:                  pulumi.String("ssh_key_name"),
			SubnetId:                 pulumi.String(subnets.Ids[0]),
			VpcSecurityGroupIds:      pulumi.StringArray{bastionSecGrp.ID()},
			Tags: pulumi.StringMap{
				"Name": pulumi.String("ans-int-bastion"),
		if err != nil {
			log.Printf("error launching bastion instance: %s", err.Error())

		// Get IP address of bastion as a string
		ipAddrList[0] = bastion.PrivateIp.ApplyString(func(s string) string {
			return fmt.Sprintf("%s", s)
The ultimate goal---and perhaps I'm going about this the wrong way---is to get Pulumi to write out the IP addresses of the instances it launched into a file that could be used with something like Ansible. My thought process was that I'd gather the IP addresses as elements in a string array, then write the array elements to a file.
If you don't need the file as a part of your program, you can just write it in an apply: https://gist.github.com/EvanBoyle/827ef2192cc878c7f708a4e612d2f2c0
Hmm...OK, I might be able to make that work. I'm assuming that the
wouldn't be needed if I wanted each element on a separate line?
was just the way I chose to do string formatting for example purposes
👍🏻 1
I moved to a slightly different model; relevant code is here: https://gist.github.com/scottslowe/bc023d3ea10b2cb22f59f7e295657f23 It successfully writes a value to the inventory file, but it isn't the IP address (I'm guessing it's some sort of string representation of the Output). I experimented with an
function (see line 53 of the gist) but it doesn't seem to work.
returns another Output which is why you are seeing this behavior. You'll need to do any printing from within an apply.
Something like this, perhaps?
Copy code
tmp := bastion.PrivateIp.ApplyT(func(v string) string {
			res := v
			fmt.Fprintln(f, v)
			return res
Well, that didn't work (file is empty).
Maybe I should switch to using the Automation API, since then stack outputs are just "ordinary" data types.
Maybe I should switch to using the Automation API,
Using automation api would definitely make it pretty easy to take stack outputs and write them to a file.
I did put in a
statement (per your gist), but the file was still empty. I think I'm just going to rewrite it using the Automation API.