1

I'm not sure where I'm going wrong with this error. I have a Terraform resource that creates a template_file used in a launch configuration. The resource is below:

Template File

data "template_file" "user_data" {
  count    = "${(var.enable ? 1 : 0) * var.number_of_zones}" // 3 templates being created
  template = "${file("userdata.sh")}"

  vars {
    ebs_volume = "${count.index == 0 ? ${var.EBS_VOLUME1} : ${var.EBS_VOLUME2}}"
  }
}

The template_file is being used to launch a script that mounts an EBS to an instance upon startup via autoscaling events. Below is the script:

userdata.sh

#!/bin/bash
# Attach EBS volume
aws ec2 attach-volume --volume-id "${EBS_VOLUME}" --instance_id `curl http://169.254.169.254/latest/meta-data/instance-id` --device /dev/sdf

EBS_VOLUME=${ebs_volume}

Upon executing this code, I am getting the following error and cannot understand why:

Error

Error: Error loading autoscaling-group.tf: Error reading config for template_file[user_data]: parse error at 1:22: expected expression but found invalid sequence "$"

Any advice on how I can resolve this would be helpful.

4
  • Which TF version are you using? Commented Jul 1, 2021 at 22:38
  • Version is 0.11.10. Commented Jul 1, 2021 at 22:50
  • How did it go? The issue still persists? Commented Jul 15, 2021 at 0:03
  • Hey, it worked! Thanks! Commented Jul 15, 2021 at 0:04

1 Answer 1

1

TF 0.11 is very old, and you should consider upgrading. But anyway, there are few mistakes in your code (wrong interpolation, userdata). The following should work:

variable "number_of_zones" {
  default = 3
}

variable "EBS_VOLUME2" {
  default = "2222"
}

variable "EBS_VOLUME1" {
  default = "1111"
}


variable "enable" {
  default = "true"
}

data "template_file" "user_data" {
  count    = "${(var.enable ? 1 : 0) * var.number_of_zones}"
  template = "${file("userdata.sh")}"

  vars {
    ebs_volume = "${count.index == 0 ? var.EBS_VOLUME1 : var.EBS_VOLUME2}"
  }
}

output "test" {  
  value    = "${data.template_file.user_data.*.rendered}"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.