Accessing Terraform variables within user_data provider template file

后端 未结 2 1014
说谎
说谎 2021-01-01 22:51

I am launching a aws_launch_configuration instance using terraform.

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

相关标签:
2条回答
  • 2021-01-01 23:25

    For people coming here since Terraform 0.12 and later, the templatefile function should be used instead of using template_file resource, so for the example in the question, it would be something like

    locals {
      vars = {
        some_address = aws_instance.some.private_ip
      }
    }
    
    user-data = templatefile("router-init.sh", vars)
    
    0 讨论(0)
  • 2021-01-01 23:34

    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}
    
    0 讨论(0)
提交回复
热议问题