title: terraform nested interpolation with count
body: <p>Using terraform I wish to refer to the content of a list of files (ultimately I want to zip them up using the archive_file provider, but in the context of this post that isn't important). These files all live within the same directory so I have two variables:</p>
  
  <pre><code>variable "source_root_dir" {
    type        = "string"
    description = "Directory containing all the files"
  }
  
  variable "source_files" {
    type        = "list"
    description = "List of files to be added to the cloud function. Locations are relative to source_root_dir"
  }
  </code></pre>
  
  <p>I want to use the template data provider to refer to the content of the files. Given the number of files in <code>source_files</code> can vary I need to use a <code>count</code> to carry out the same operation on all of them.</p>
  
  <p>Thanks to the information provided at <a href="https://stackoverflow.com/a/43195932/201657">https://stackoverflow.com/a/43195932/201657</a> I know that I can refer to the content of a single file like so:</p>
  
  <pre><code>provider "template" {
    version = "1.0.0"
  }
  
  variable "source_root_dir" {
    type        = "string"
  }
  
  variable "source_file" {
    type        = "string"
  }
  
  data "template_file" "t_file" {
    template = "${file("${var.source_root_dir}/${var.source_file}")}"
  }
  
  output "myoutput" {
    value = "${data.template_file.t_file.rendered}"
  }
  </code></pre>
  
  <p>Notice that that contains nested string interpolations. If I run:</p>
  
  <pre><code>terraform init &amp;&amp; terraform apply -var source_file="foo" -var source_root_dir="./mydir"
  </code></pre>
  
  <p><em>after creating file mydir/foo of course</em> then this is the output:
  <a href="https://i.stack.imgur.com/dDtcP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dDtcP.png" alt="enter image description here"></a></p>
  
  <p>Success!</p>
  
  <p>Now I want to combine that nested string interpolation syntax with my <code>count</code>. Hence my terraform project now looks like this:</p>
  
  <pre><code>provider "template" {
    version = "1.0.0"
  }
  
  variable "source_root_dir" {
    type        = "string"
    description = "Directory containing all the files"
  }
  
  variable "source_files" {
    type        = "list"
    description = "List of files to be added to the cloud function. Locations are relative to source_root_dir"
  }
  
  data "template_file" "t_file" {
    count    = "${length(var.source_files)}"
    template = "${file("${"${var.source_root_dir}"/"${element("${var.source_files}", count.index)}"}")}"
  }
  
  output "myoutput" {
    value = "${data.template_file.t_file.*.rendered}"
  }
  </code></pre>
  
  <p>yes it looks complicated but syntactically, its correct (at least I think it is). However, if I run init and apply:<br>
  <code>terraform init &amp;&amp; terraform apply -var source_files='["foo", "bar"]' -var source_root_dir='mydir'</code>  </p>
  
  <p>I get errors:</p>
  
  <blockquote>
    <p>Error: data.template_file.t_file: 2 error(s) occurred:<br>
    * data.template_file.t_file[0]: __builtin_StringToInt: strconv.ParseInt: parsing "mydir": invalid syntax in:<br>
    ${file("${"${var.source_root_dir}"/"${element("${var.source_files}", count.index)}"}")}<br>
    * data.template_file.t_file<a href="https://i.stack.imgur.com/dDtcP.png" rel="nofollow noreferrer">1</a>: __builtin_StringToInt: strconv.ParseInt: parsing "mydir": invalid syntax in:<br>
    ${file("${"${var.source_root_dir}"/"${element("${var.source_files}", count.index)}"}")}  </p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/BvfQV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BvfQV.png" alt="enter image description here"></a></p>
  
  <p>My best guess is that its interpreting the <code>/</code> as a division operation hence its attempting to parse the value <strong>mydir</strong> in <code>source_root_dir</code> as an int.</p>
  
  <p>I've played around with this for ages now and can't figure it out. Can someone figure out how to use nested string interpolations together with a <code>count</code> in order to refer to the content of multiple files using the template provider?</p>