How to pass variable to user_data under launch_configuration in Terraform 0.9.2 ?

7,241 views
Skip to first unread message

egul...@gmail.com

unread,
Apr 18, 2017, 2:03:09 PM4/18/17
to Terraform
Hello everyone,

I've been using TF 0.8.8 and recently started using 0.9.2 and got a roadblock with passing variables to user_data under Launch Configuration.

Here's my code that I have inside 'autoscaling_group' module:




resource "aws_launch_configuration" "launch_config" {
  name_prefix                          = "${var.lc_name}"
  image_id                               = "${var.lc_ami_id}"
  instance_type                        = "${var.lc_instance_type}"
  security_groups                     = ["${var.lc_security_groups}"]
  associate_public_ip_address  = "${var.associate_public_ip_address}"
  key_name                             = "${var.key_name}"
  iam_instance_profile              = "${var.instance_iam_role}"
  user_data                              = "CLUSTER_NAME=${var.cluster_name}\nEBS_VOLUME=${var.ebs_id}\n"
}
...
 

Variables are required in variables.tf.


In main.tf in my project's folder I have:


module "ServerGroup1" {
....
#
# User Data
#
cluster_name = "test"
ebs_id = ""${module.Volume1.id}"

}


This works perfectly fine in 0.8.8 but not in 0.9.2. Did anyone had similar issue?


Could that be a bug or passing simple variables to user_data under ASG is not supported anymore?



Any advice appreciated.

Thank you,
Ernest

egul...@gmail.com

unread,
Apr 18, 2017, 2:08:06 PM4/18/17
to Terraform
I'm sorry I forgot to write the actual problem..

Ec2 instance is created but user_data is simply blank in 0.9.2 where in 0.8.8 is passed as expected.

Terraform doesn't complain and there are no errors, so I'm a bit lost on that one.

Philip Nelson

unread,
Apr 19, 2017, 6:30:43 AM4/19/17
to terrafo...@googlegroups.com
Assuming the double quote in your message was an email typo, I can say I'm passing userdata via autoscaling groups in 0.9.2 every day without problems. the only difference is that we are using data "template_file" instead of your multiline string. I'd also think for a small set of user data, a heredoc would at least be more usable. Note the addition of quotes

...
userdata = << EOF
CLUSTER_NAME="${var.cluster_name}"
EBS_VOLUME="${var.ebs_id}"
EOF



--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/0e036cf2-1ca3-4c57-8be7-bc761972d86b%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Chris Jefferies

unread,
Nov 8, 2017, 6:42:40 PM11/8/17
to Terraform
This is from quite a while back, but I'm having a problem in v0.10.7 where quotes end up in the user_data around the variable.

I go from this:

user_data = <<-EOF
   
#cloud-config
    hostname
: "${var.number_of_instances > 1 ? "${var.host_name}${format("%02d", count.index + 1)}.${var.instance_name}" : "${var.instance_name}" }"
    fqdn
: "${var.number_of_instances > 1 ? "${var.host_name}${format("%02d", count.index + 1)}.${var.instance_name}" : "${var.instance_name}" }"
    manage_etc_hosts
: true


    runcmd
:
   
- touch /var/tmp/HelloInThere.txt
   
"${var.host_name == "if" ? "${local.user_cmd_if}" : "${local.user_cmd_empty}" }"
    EOF

to this:

    #cloud-config
    hostname
: "yum.system.dmn"
    fqdn
: "yum.system.dmn"
    manage_etc_hosts
: true


    runcmd
:
   
- touch /var/tmp/HelloInThere.txt
   
""

The hostname and the fqdn still wotk, even with the quotes but if I add the last runcmd, it chokes.

Any tips and help appreciated,
Chris.


To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

Igor Cicimov

unread,
Nov 8, 2017, 11:26:04 PM11/8/17
to Terraform
The variables interpolation guide has an example:

```
subnet = "${var.env == "production" ? var.prod_subnet : var.dev_subnet}"
```

you can try the same syntax.

Chris Jefferies

unread,
Nov 9, 2017, 3:55:18 PM11/9/17
to Terraform
Another good suggestion, Igor.  Again I am thwarted.

I went from this:
"${var.host_name == "if" ? "${local.user_cmd_if}" : "${local.user_cmd_empty}" }"

To this:
"${var.host_name == "if" ? local.user_cmd_if : local.user_cmd_empty }"

The terraform validation passed OK, and the values were correctly resolved, but still... the quotes.

I should show that I am defining the local variables like this:
locals {
  user_cmd_empty
= ""
  user_cmd_if    
= "- touch /var/tmp/user_cmd_if.txt"
  user_cmd_pm    
= "- touch /var/tmp/user_cmd_pm.txt"
  user_cmd_ym    
= "- touch /var/tmp/user_cmd_ym.txt"
}

If I removed the
local.user_cmd_empty

variable from my user_data and replaced it with "" it has the same result.

I've also tried to escape the characters like this, thinking that it is the outside quotes which are likely the problem:
\"${var.host_name == "if" ? local.user_cmd_if : local.user_cmd_empty }\"

But it does not pass the terraform validate test.

Thanks for your help,
Chris.

Igor Cicimov

unread,
Nov 9, 2017, 7:55:13 PM11/9/17
to Terraform
Hi Chris,

As suggested in the other thread you posted, try this:


"${var.host_name == "if" ? ${local.user_cmd_if} : ${local.user_cmd_empty} }"

Notice the quotes around the internal vars are omitted.
Reply all
Reply to author
Forward
0 new messages