【问题标题】:using variables in aws launch template userdata (terraform)using variables in aws launch template userdata (terraform)
【发布时间】:2022-12-27 18:03:43
【问题描述】:

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 ?

【问题讨论】:

  • What is your current attempt at file_name.sh? Why it does not work? What errors do you get?

标签: amazon-web-services terraform terraform-provider-aws launch-template


【解决方案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}"

【讨论】:

  • Thanks , I tired it earlier, but user_data argument is only valid for EC2 and not for launch_template
  • @ChoolaiRaj I beg to differ. user_data is also available in launch template take a look below registry.terraform.io/providers/hashicorp/aws/latest/docs/…
  • @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}))
【解决方案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}))

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 1970-01-01
    • 2021-05-19
    • 2022-06-13
    • 2022-12-02
    • 2022-11-09
    • 2014-09-02
    • 2022-12-28
    • 2018-05-31
    相关资源
    最近更新 更多