Timothy T.
Timothy T.

Reputation: 1101

Accessing Terraform variables within user_data provider template file

I am launching an 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?

Upvotes: 33

Views: 36807

Answers (3)

Sanim16
Sanim16

Reputation: 329

From Terraform 0.12 and later, you can use the templatefile function instead of the template_file resource, so for a general use case where you need to access more than one variable, you can use:

    locals {
      variable1       = "your_username"
      variable2       = "your_password"
      db_address      = aws_db_instance.some-db.address
      public_alb_dns  = aws_lb.some-alb.dns_name
    }
    
    resource "aws_instance" "web_01" {
      ....
      user_data = base64encode(templatefile("user_data.sh", {
        db_address      = local.db_address
        admin_user      = local.variable1
        admin_password  = local.variable2
        public_alb_dns  = local.private_alb_dns
      } ))
      ....
    }

You can also access other terraform resource attribute references.

Upvotes: 7

Mo Hajr
Mo Hajr

Reputation: 1332

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 = base64encode(templatefile("${path.module}/router-init.sh", local.vars))

Upvotes: 19

Brandon Miller
Brandon Miller

Reputation: 5065

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}

Upvotes: 54

Related Questions