Terraform: using a file as input for user_data in aws_istance or aws_launch_configuration

Created on 15 May 2015  ยท  4Comments  ยท  Source: hashicorp/terraform

I'm trying to read the content for a user_data from a file.

resource "aws_launch_configuration" "webfarm" {
    name = "web_config"
    image_id = "ami-c5b7d8b2"
    instance_type = "t2.micro"
    security_groups = ["${aws_security_group.web-instance.id}"]
    user_data = ${file("user-data.web")}
}

when running terraform plan I get the error:
Error loading config: Error parsing /home/patrick/dev/tf/main.tf: Line 616, column 16: syntax error

Uncommenting this syntax makes the error go away.
I feel it must be something trivial ... maybe file is not the correct directive, but template is?

  • Terraform v0.5.0
  • The file is not even that long 616 longs.. :)
  • The file exists & is readable
  • The file is valid yaml (in case that's checked)
  • I've tried quoting "${file("user-data.web")}" or "${file('user-data.web')}"
  • Using a here-doc as input for user_data works
  • It does not depend on the input in the file (even empty file does not work)

Most helpful comment

This change should work:

- user_data = ${file("user-data.web")}
+ user_data = "${file("user-data.web")}"

But I see you already tried that. All I can add is that I use the exact same syntax, and that one is working for me:

resource "aws_instance" "..." {
  user_data = "${file("../../tmp/aws/userdata.sh")}"
  ...
}

I am using the latest HEAD version (built using make dev), but this functionality has been present for a while now, so I doubt that could be the solution.

Also, my use-case is with aws_instance, instead of aws_launch_configuration, so perhaps these work differently.

All 4 comments

Using a template seems to work fine, so I have a workaround.
Maybe it's just the description in the doc on file and old examples that put me on the wrong foot.

file(path) - Reads the contents of a file into the string. Variables in this file are not interpolated. The contents of the file are read as-is.
resource "template_file" "web-userdata" {
    filename = "user-data.web"
}
resource "aws_launch_configuration" "webfarm" {
    name = "web_config"
    image_id = "ami-c5b7d8b2"
    instance_type = "t2.micro"
    security_groups = ["${aws_security_group.web-instance.id}"]
    user_data = "${template_file.web-userdata.rendered}"
    //user_data = ${file("user-data.web")}
}

This change should work:

- user_data = ${file("user-data.web")}
+ user_data = "${file("user-data.web")}"

But I see you already tried that. All I can add is that I use the exact same syntax, and that one is working for me:

resource "aws_instance" "..." {
  user_data = "${file("../../tmp/aws/userdata.sh")}"
  ...
}

I am using the latest HEAD version (built using make dev), but this functionality has been present for a while now, so I doubt that could be the solution.

Also, my use-case is with aws_instance, instead of aws_launch_configuration, so perhaps these work differently.

@JeanMertz guess I wasn't as observant , "${file("user-data.web")}" works "${file('user-data.web')}".

closing the issue here.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shanmugakarna picture shanmugakarna  ยท  3Comments

ronnix picture ronnix  ยท  3Comments

franklinwise picture franklinwise  ยท  3Comments

carl-youngblood picture carl-youngblood  ยท  3Comments

sprokopiak picture sprokopiak  ยท  3Comments