Using templatefile to pass a list into script.yml

Hello,
I am a new to terraform and struggling with using templatefile to pass a list to my script.yml.

I can pass the list devnames to a bash script without issues but I cannot figure it out with script.yml. With the belw configuration, when I run terraform plan, the list just shows as $devnames and does not unpack the content of the list devnames.

Can you please assist?

resource "aws_imagebuilder_component" "devmappings" {
  name     = "devmappings"
  platform = "Linux"
  data = templatefile("${path.module}/script.yml", {
    device_names = join(" ", local.devnames)
    })
  version  = "1.0.0"
}

I need to execute script.yml against the content of the list devnames

name: 'myName'

description: 'myDescrip'

schemaVersion: 1.0

phases:

  - name: build

    steps:

      - name: Install-prereqs

        action: ExecuteBash

        inputs:

          commands: ["echo $devnames; for i in `seq 0 26`; do; nvme_blkd='/dev/nvme$${i}n1'; if [ -e  $nvme_blkd ]; then; mapdevs='$${devnames[i]}'; if [[ -z '$mapdevs' ]]; then; mapdevs='$${devnames[i]}'; fi;      if [[ '$mapdevs' != /dev/* ]]; then; mapdevs='/dev/$${mapdevs}'; fi; if [ -e $mapdevs ]; then; echo 'path exists: $${mapdevs}'; else; ln -s $nvme_blkd $mapdevs; echo 'symlink created: $${nvme_blkd} to $${mapdevs}'; fi; fi; done"]

Hi @saidben0,

There’s some information on how to reliably generate YAML in a template in the documentation section Generating JSON or YAML from a template, which is part of the documentation for the templatefile function.

The general idea here will be to use Terraform’s expression language to construct a suitable data structure that matches the shape of the YAML you want to produce, and then use the yamlencode function to produce a YAML syntax version of that value, thus making Terraform responsible for generating valid YAML where you only need to generate a suitable value.

Thanks @apparentlymart, I was just missing the curly braces "${devnames}" to retrieve the list in script.yml