Terraform template_file use loop

I’m trying to pass two array to my script file in terraform using template_file.

My terraform code is:

variable "services" {
   type = list(string)
   default = ["Service1", "Service2", "service3"]
}

variable "passwords" {
   type = list(string)
   default = ["Password1", "Password2", "Password3"]
}

data "template_file" "configure" {
  template = "${file("${path.module}/init/configure")}"
  vars = {
    services  = join(",",var.services)
    passwords = join(",",var.passwords)

  }
}

resource "aws_instance" "grafana_nodes" {
  ...
  user_data = data.template_file.config
  ...
}  

and this is my script:

sudo echo "SERVICES:" >> /tmp/my_array.txt
sudo echo "${services}" >> /tmp/my_array.txt
sudo echo "PASSWORDS:" >> /tmp/my_array.txt
sudo echo "${passwords}" >> /tmp/my_array.txt

declare -a services_array
services_array=($(echo "${services}" | tr "," " "))

declare -a passwords_array
passwords_array=($(echo "${passwords}" | tr "," " "))

sudo printf '%s\n' "$${services_array[@]}" >> /tmp/services_array.txt
sudo printf '%s\n' "$${services_array[@]}" >> /tmp/services_array.txt

declare -i index
for index in "$${services_array[@]}"
do
   sudo echo "service: $${services_array[$index]} password: $${passwords_array[$index]}" >> /tmp/my_array2.txt
done

but the script doesn’t work, don’t create the files.
How can use the array in my template?

Hi @marco.ferrara,

I see that you already found a solution to your similar question on Stack Overflow. Do you still need help with this? Thanks!

Hi @apparentlymart ,
thanks I solved