Skip to content

Terraform

Terraform

Conditionally process a set of files

This takes a folder full of SQL files and runs them through the templating engine to insert variables into the file, provided we are in the prod environment. They get written into a bucket (so care needs to be taken with sensitive variables).

locals {
  templatevars = {
    manager_table = data.aws_ssm_parameter.manager_table.value
  }
}

resource "aws_s3_object" "templated_sql_objects" {
  for_each = var.ENVIRONMENT == "prod" ? fileset("prodfiles/", "*.sql") : []
  bucket   = aws_s3_bucket.prod-sql-scripts.id
  key      = each.key
  content  = templatefile("prodfiles/${each.key}", local.templatevars)
}

The template files need to contain ${manager_table} to include the templated variable.