Templatefile function in terraform 12 is failed

Hi All,

I need your help, spent too much time and could not figured this out.

I use terraform 12

I am using templatefile function and want to pass some value to user_data.sh script

locals.tf:

locals {
web_user_data = templatefile("${path.modules}/scripts/user_data.sh", {
    Opt_Variable = var.chef_variable
    })
}
variable "chef_variable" {
    type = map(string)
    default = { "Name" : "somenamehere" , "Env" : "someenvnamehere" }
}

in main.tf calling as shown below

resource "aws_instance"  "myec2" {
    ...
    ...
    ...
    user_data = local.web_user_data
}

Error:
Error in function call

modules/myec2/locals.tf line 14, in locals:
web_user_data = templatefile("${path.modules}/scripts/user_data.sh, {

path.module is module/myec2

Call to function “templatefile” failed.
modules/myec2/scripts/user_data.sh:229,41-42: Invalid character; This character is not used in the language.

Really appreciate for any help.

@apparentlymart might be able to help?

Does this work?

variable "chef_variable" {
    type = map(string)
    default = {
        Name = "somenamehere"
        Env = "someenvnamehere"
    }
}

The error indicates there is an invalid character at the location modules/myec2/scripts/user_data.sh:229,41-42, what is the character at that location?

The most common mistake is caused by copy&pasting in an unexpected character like curly quotes. You could also have a missing closing bracket, causing extra characters to be included in the interpolation string.

Nope,
event just with one value it is not working:

variable "chef_variable" {
    type = map(string)
    default = {
        Name = "abc123"
    }
}

In user_data.sh:229, 41-42
I see this as a line 229 in user_data.sh, and character 41-42, there us nothing.

There is only 40 character at that line in user_data.sh shown below. actualVariable name changed with somevariable name. But including whitespaces there are 40 characters

instances=$(echo ${somevariableName[0]})

Even passed directly like this in the file:

locals {
web_user_data = templatefile("${path.modules}/scripts/user_data.sh", {
    Name = "abc123"
    })
}

Hi all,

The issue was with user_data.sh script itself. Thank you all for the help!