Accessing Terraform variables within user_data provider template file
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.
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))
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}