3

In my lambda.tf, I have a data resource

data "template_file" "handler" {
    template = "${file("${path.module}/templates/handler.js")}"

    vars = {
        ENDPOINT = "${var.domain}"
        PASSWORD = "${var.password}"
    }
}

However - I'm encountering a syntax error:

Error: failed to render : <template_file>:280,49-50: Extra characters after interpolation expression; Expected a closing brace to end the interpolation expression, but found extra characters.

  on ../docs/lambda.tf line 1, in data "template_file" "handler":
   1: data "template_file" "handler" {

Is interpolation inside an interpolation allowed for Terraform? If so - any suggestions on pointing towards where the error is would be greatly appreciated.

Terraform v0.12.9. Provider "aws" version "~> 2.7"

1
  • You should be able to just do file("${path.module}/templates/handler.js"). Commented Oct 8, 2019 at 14:32

2 Answers 2

5

Not exactly clear what your template file looks like or what you are trying to do, so here are a couple different answers.

  1. You can escape interpolation with double dollar signs: $${foo} will be rendered as a literal ${foo}.

  2. Terraform does not allow dynamic construction of variable names, because it needs to be able to analyze the configuration statically (that is, without evaluating any expressions) in order to determine which order the expressions must be resolved in.

    Terraform supports a map data structure that can be used to achieve this effect.

variable "var1" {
 default = "value1"
}
variable "var2" {
 default = "value2"
}

locals {
 var3 = "${var.var1}_${var.var2}"
 values = {
   "value1_value2" = "local1"
   "value2_value3" = "local2"
   "value3_value4" = "local3"
 }
}

output "val_output" {
 value = "${local.values[local.var3]}"
}

If neither is what you are looking for, you need to share your template file or a modified version that duplicates the error.

Sign up to request clarification or add additional context in comments.

Comments

4

The template_file data source continues to exist for users of Terraform 0.11 and earlier, but since you are using a Terraform 0.12 release I'd recommend using the templatefile function instead. Because it's built directly into Terraform, it is able to produce better error messages.

To use it, you can replace your references to data.template_file.handler.rendered with a direct call to templatefile. If you are using that rendered result in multiple locations, you can assign the templatefile result to a local value and reference that in multiple places instead.

templatefile("${path.module}/templates/handler.js", {
  ENDPOINT = var.domain
  PASSWORD = var.password
})

The error message you saw suggests that there's a syntax error in your template itself, but because template_file is implemented in a separate provider it's reporting that syntax error in an unhelpful way, pointing to a particular source location but not including the relevant source code snippet.

If you use templatefile instead, Terraform can hopefully report this syntax error itself and give better information about it.

Either way, it seems like the syntax error is on line 280 column 49 of your handler.js file and is caused by Terraform's template engine expecting to find the } to close a ${ ... } interpolation sequence but finding something else instead. If you correct that syntax error, template rendering should succeed by either approach.

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.