using variables in aws launch template userdata (terraform)

4.4k views Asked by At

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 ?

2

There are 2 answers

0
Jerry On

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}))
3
Tolis Gerodimos On

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}"