Parse error near `<<' for heredoc

Hello,

I am using a variable of type list(object) and values in the list are of type string.
My terraform version is - v1.9.3

I was getting an error as -


│ Error: Unterminated template string
│ 
│   on <value for var.open_ai_container_spn> line 1:
│   (source code not available)
│ 
│ No closing marker was found for the string.
╵
╷
│ Error: Invalid multi-line string
│ 
│   on <value for var.open_ai_container_spn> line 1:
│   (source code not available)
│ 
│ Quoted strings may not be split over multiple lines. To produce a multi-line string, either use the \n escape to represent a newline
│ character or use the "heredoc" multi-line template syntax.

And it suggested to use “heredoc” for multiline string. So I am trying it with heredoc, but its giving error as -

input.sh:line7:Parse error near <<'`

Here is my code for the list object -

SPAAPCBLLMDEVWE1="`az keyvault secret show --id https://testkv01.vault.azure.net/secrets/SP-AA-PCB-LLM-DEV-WE-1-Key  -o tsv --query value`"
SPAAWCMCBLLMDEVWE1="`az keyvault secret show --id https://testkv01.vault.azure.net/secrets/SP-AA-WCMCB-LLM-DEV-WE-1-Key  -o tsv --query value`"

lis=(
  {    
  "open_ai_container_name"= <<EOT
   ci-aa-llm-test-cb-dev-we-001,
EOT
  "spn_name"= <<EOT
  SP-AA-PCB-LLM-DEV-WE-1,
EOT
  "spn_secret"= <<EOT
  $SPAAPCBLLMDEVWE1
EOT
  }

  )

list=[${lis[@]}]

export TF_VAR_open_ai_container_spn=${list[@]}

Please suggest what I am doing wrong here or correct the syntax of heredoc.

I think the issue here is that you are ‘indenting’ the first line of the HEREDOC string:

You can fix this in two ways.

  1. Remove the indent of the first line of the Heredoc string:
  "open_ai_container_name"= <<EOT
ci-aa-llm-test-cb-dev-we-001,
EOT
  1. Use the special marker for ‘indented’ Heredoc strings:
  "open_ai_container_name"= <<-EOT
  ci-aa-llm-test-cb-dev-we-001,
EOT

See Strings and Templates - Indented Heredocs - Configuration Language | Terraform | HashiCorp Developer

Hope that helps

Happy Terraforming

Hi @ravindrascool85,

The new error you described seems to be a shell syntax error rather than a Terraform error.

When you use a shell script to generate input to Terraform you need to make sure that the shell syntax is valid first, and then that the valid shell syntax generates something that Terraform would accept as valid. You are using code written in one language to generate code written in another language, so it’s important to be mindful of which parts of the input are being handled as which of the two languages.

Currently you have a shell syntax question rather than a Terraform question. That doesn’t mean that folks here won’t be able to help, but you might also wish to ask about this in a context where people discuss whichever shell you are using here.

Thanks for your help.

I tried both the ways as you suggested, however its still not working and giving the same error msg as " Parse error near <<'`

Also I tried the same in both bash and zsh, still the same error.

Apologies for posting it here in terraform forum. That shell script is dependent for my terraform code as I am using it there, so I thought someone might have encountered the same situation.

Also I tried to find an example on the internet about it, but not able to find any correct one.

Thanks for your help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.