Accessing Terraform variables within user_data provider template file

房东的猫 提交于 2020-06-10 03:11:10

问题


I am launching a aws_launch_configuration instance using terraform.

I'm using a shell script for the user_data variable, like so:

resource "aws_launch_configuration" "launch_config" {
    ...
     user_data                     = "${file("router-init.sh")}"
    ....  
}

Within this router-init.sh, one of the things I would like to do, is to have access to the ip addresses for other instances I am launching via terraform.

I know that I can use a splat to access all the ip addresses of that instance, for instance:

output ip_address {
    value = ${aws_instance.myAWSInstance.*.private_ip}"
}

Is there a way to pass / access these ip addresses within the router-init.sh script?


回答1:


You can do this using a template_file data source:

data "template_file" "init" {
  template = "${file("router-init.sh.tpl")}"

  vars = {
    some_address = "${aws_instance.some.private_ip}"
  }
}

Then reference it inside the template like:

#!/bin/bash

echo "SOME_ADDRESS = ${some_address}" > /tmp/

Then use that for the user_data:

 user_data = ${data.template_file.init.rendered}


来源:https://stackoverflow.com/questions/50835636/accessing-terraform-variables-within-user-data-provider-template-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!