1
votes

I have Terraform that is using a templated bash script to set my user data section for an AWS launch configuration.

data "template_file" "user_data" {
  template = "${file("${path.module}/user-data.tpl")}"

  vars {
    file_system_id = "${aws_efs_mount_target.my_efs_alpha.dns_name}"
  }
}

The file_system_id variable then needs to be used in my template:

sudo mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 $$$${file_system_id}:/ /mnt/efs

Bash will interpret a single dollar sign as a bash variable. As I understand it, Terraform will interpret a double-dollar-sign as a Terraform variable. For added fun, the dollar signs in the template need to be escaped with another dollar sign -- hence the 4 dollar signs in front of file_system_id.

Looking at the user data in my Launch Config over in AWS Console, Terraform does not appear to be making any effort to replace my $$$${file_system_id) with the variable value from my template_file definition. Rather, it just shows up in the user data section as literally $${file_system_id}.

So, the question is, how do I get my EFS DNS name (or whatever other value I want) to replace the file_system_id variable in my template? What have I missed?

1
so what's wrong to use ${file_system_id} directly in user_data.tpl?BMW
Well, that's some humble pie for me. I figured that since it was an escape character, and since I've found cases where I need to escape the '$' with double dollar signs to get Terraform variable replacement to work, that this would be a similar case. Indeed, it looks like the single dollar sign works here. Feel free to submit your comment as an answer and I'll approve it.REW

1 Answers

1
votes

As BMW mentioned, you don't need to escape the dollar signs. ${file_system_id} works just fine.

Terraform's variable-replacement in templates will run first so you don't need to worry about how Bash will parse it until after the variables are replaced.