Batch replace of files content in Terraform

Viewed 2432

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:

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}")
}

The only thing is left is that I need to loop over all files recursively and replace markers (for example !S3!) with values from variables of terraform’s module. Similar to this, but across all files in directories/subdirectories:

replace(file("${path.module}/launchconfigs/file"), “#S3”, aws_s3_bucket.kops.bucket)

So the question in one sentence: how to loop over files and replace parts of them with variables from terraform?

1 Answers

An option could be using templates, the code will look like:

provider "aws" {
  region = "us-west-1"
}

resource "aws_s3_bucket" "sample_bucket2222" {
  bucket = "my-tf-test-bucket2222"
  acl    = "private"
}

resource "aws_s3_bucket_object" "k8s-state" {
  for_each = fileset("${path.module}/data", "**/*")
  bucket   = aws_s3_bucket.sample_bucket2222.bucket
  key      = each.value
  content  = data.template_file.data[each.value].rendered
  etag     = filemd5("${path.module}/data/${each.value}")
}

data "template_file" "data" {
  for_each = fileset("${path.module}/data", "**/*")
  template = "${file("${path.module}/data/${each.value}")}"
  vars = {
    bucket_id  = aws_s3_bucket.sample_bucket2222.id
    bucket_arn = aws_s3_bucket.sample_bucket2222.arn
  }
}

Instead of source you can see I'm using content to consume the template_file, that is the only difference in that resource with yours


On your files the variables could be consumed like:

Hello ${bucket_id}

I have all my test code here:
https://github.com/heldersepu/hs-scripts/tree/master/TerraForm/regional

Related