title: How to reduce boilerplate provisioning in modules?
body: <p>So I have two terraform modules that are exactly the same ... the exception is that there is one extra remote-execution step</p>
  
  <p><strong>Module A</strong></p>
  
  <pre><code>  provisioner "remote-exec" {
      inline = [
        "sudo chmod 777 -R ${var.setup_files_destination}",
        "sudo ${var.setup_files_destination}/scripts/setup.sh ${var.setup_files_destination}",
      ]
  
      connection {
        user        = "ec2-user"
        private_key = "${file(var.private_key_location)}"
      }
    }
  </code></pre>
  
  <p><strong>Module B - Difference is one extra script to execute</strong></p>
  
  <pre><code>  provisioner "remote-exec" {
      inline = [
        "sudo chmod 777 -R ${var.setup_files_destination}",
        "sudo ${var.setup_files_destination}/scripts/setup.sh ${var.setup_files_destination}",
      ]
  
      connection {
        user        = "ec2-user"
        private_key = "${file(var.private_key_location)}"
      }
    }
  
    provisioner "remote-exec" {
      inline = [
        "sudo chmod 777 -R ${var.setup_files_destination}",
        "${var.setup_files_destination}/scripts/setup_special.sh ${var.setup_files_destination}",
      ]
  
      connection {
        user        = "ec2-user"
        private_key = "${file(var.private_key_location)}"
      }
    }
  </code></pre>
  
  <p>Other than this script difference, the modules have the exact same steps. The amount of duplicate terraform code is around 50 lines.</p>
  
  <p><strong>What I would like to do in Terraform</strong></p>
  
  <p>So on the Terraform side, I would like to specify the scripts to execute as a list, so something like</p>
  
  <p><strong>Variable</strong></p>
  
  <pre><code>variable "commands_to_execute" {
    type = "list"
  }
  </code></pre>
  
  <p><strong>Common Step</strong></p>
  
  <pre><code>provisioner "remote-exec" {
    inline = "[${var.commands_to_execute}]"
  
    connection {
      user        = "ec2-user"
      private_key = "${file(var.private_key_location)}"
    }
  }
  </code></pre>
  
  <p><strong>Perceived use of common step</strong></p>
  
  <pre><code>module "instances" {
    ... rest of declaration...
    commands_to_execute = [
      "sudo chmod 777 -R ${var.setup_files_destination}",
      "sudo ${var.setup_files_destination}/scripts/setup.sh ${var.setup_files_destination}",
    ]
  }
  </code></pre>
  
  <p>When I run this, I get </p>
  
  <blockquote>
    <p>Error: module 'instances': unknown variable referenced: 'setup_files_destination'; define it with a 'variable' block</p>
  </blockquote>
  
  <p>So <code>setup_files_destination</code> is a variable defined inside the module, but not in the <code>.tf</code> file using the module. It has a default value and is effectively the location to the directory of the shell script on the remote instance. </p>
  
  <p>I would like to keep the <code>setup_files_destination</code> in the module. So what I am <strong>looking for is a way to defer the interpolation of the variable</strong> until when the module is actually used. <strong>Is this possible in terraform?</strong></p>