title: Batch replace of files content in Terraform
body: <p>I have multiple files under some root directory, let’s call it module/data/.
  I need to upload this directory to the corresponding S3 bucket. All this works as expected with:</p>
  
  <pre><code>resource "aws_s3_bucket_object" "k8s-state" {
    for_each = fileset("${path.module}/data", "**/*")
    bucket = aws_s3_bucket.kops.bucket
    key    = each.value
    source = "${path.module}/data/${each.value}"
    etag   = filemd5("${path.module}/data/${each.value}")
  }
  </code></pre>
  
  <p>The only thing is left is that I need to loop over all files recursively and replace markers (for example <code>!S3!</code>) with values from variables of terraform’s module.
  Similar to this, but across all files in directories/subdirectories:</p>
  
  <pre><code>replace(file("${path.module}/launchconfigs/file"), “#S3”, aws_s3_bucket.kops.bucket)
  </code></pre>
  
  <p>So the question in one sentence: how to loop over files and replace parts of them with variables from terraform?</p>