0

I am currently creating a launch template using terraform aws_launch_template resource. I need to pass in user data and i do so using user_data =filebase64("file_path/file_name.sh"). I need to have variables inside my userdata file (file_name.sh). What is the best way to accomplish this ?

Choolai Raj
  • 111
  • 2
  • 8
  • 1
    What is your current attempt at `file_name.sh`? Why it does not work? What errors do you get? – Marcin May 16 '22 at 02:01

2 Answers2

2

You're using templatefile to pass variable, then you'll need the function base64encode to provide user_data required by launch_teamplate. Try this:

user_data =  base64encode(templatefile("${path.module}/your-file.sh", {the_var1 = var.the_var1, the_var2= value2}))
Jerry
  • 97
  • 5
1

I would suggest using the function templatefile, since it lets you pass variables to the template file

user_data = templatefile("file_path/file_name.sh", { var1 = value1, var2= value2})

And to use them to your script

file_name.sh

#!/bin/bash

echo "template vars are: ${var1} , ${var2}"
Tolis Gerodimos
  • 3,782
  • 2
  • 7
  • 14
  • Thanks , I tired it earlier, but user_data argument is only valid for EC2 and not for launch_template – Choolai Raj May 16 '22 at 13:27
  • @ChoolaiRaj I beg to differ. user_data is also available in launch template take a look below https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template#user_data – Tolis Gerodimos May 16 '22 at 14:41
  • @ChoolaiRaj, if you're using aws_launch_template then you'll need base64 encoded user data. You can have it using the base64encode function, like: base64encode(templatefile("file_path/file_name.sh", { var1 = value1, var2= value2})) – Jerry Dec 27 '22 at 10:01