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

famous-needle-81667

06/01/2022, 2:57 PM
Hello everyone, trying to port some changes from Terraform to Pulumi and run into an issue: This is EC2 instance spec in terraform. I have a templated bash script into which I inject loadbalancer DNS name.
Copy code
user_data                   = base64encode(
    templatefile("../templates/bash_script.sh.tftpl", {
        internal_lb_dns_name = aws_lb.aws-internal-load-balancer.dns_name
      }
    )
  )
How to achieve similar results with pulumi? If it was not a templated file it would be easy, namely:
Copy code
UserData:            pulumi.StringPtr(base64.StdEncoding.EncodeToString(bashScriptContent))
However, I need to inject some variables into that script that will be known once some resources got created. Any help would be appreciated, this is a part of my Master's Degree thesis and it would be a shame that this cannot be solved in Pulumi 😄 //EDIT, I'm writing in Golang
f

full-artist-27215

06/01/2022, 3:45 PM
I'm not a Go person, so I can't speak to the particulars, but I have done this with Python several times. You'll basically end up using a
pulumi.Output.all(...)
call, passing all your variables in, and call
.apply(...)
with a function that takes those variables and your templating solution of choice and spits out the final rendered output.
❤️ 1
f

famous-needle-81667

06/01/2022, 9:42 PM
Thank you @full-artist-27215
Copy code
userDataTpl, err := template.New("bash_script.tpl.sh").
			ParseFiles("./templates/bash_script.tpl.sh")
		if err != nil {
			return err
		}

		userData := internalLoadBalancer.DnsName.ApplyT(
			func(lbDNSName string) (*string, error) {
				buf := new(bytes.Buffer)
				err := userDataFrontendTpl.Execute(buf, map[string]string{"internal_lb_dns_name": lbDNSName})
				userData := base64.StdEncoding.EncodeToString(buf.Bytes())
				return &userData, err
			},
		).(pulumi.StringPtrOutput)
worked like a charm with native go templating engine
f

full-artist-27215

06/01/2022, 9:44 PM
awesome 😎
31 Views