diff --git a/.gitignore b/.gitignore index debc659..f3aeebd 100644 --- a/.gitignore +++ b/.gitignore @@ -32,8 +32,9 @@ override.tf.json # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan # example: *tfplan* -# Ignore dynamically created clusters.tf +# Ignore dynamically created *.tf files clusters.tf +backend.tf # Ignore temporary vi files *.swp diff --git a/terraform/locals.tf b/terraform/locals.tf index c4e344c..69fd1d9 100644 --- a/terraform/locals.tf +++ b/terraform/locals.tf @@ -1,6 +1,10 @@ # locals.tf - root module local variable definitions locals { + backends = { + for key in keys(var.backends): + key => var.backends[key] if var.backends[key].enabled == true + } clusters = { gke = { for key in keys(var.clusters): diff --git a/terraform/main.tf b/terraform/main.tf index 582db23..966c4be 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -1,167 +1,208 @@ # main.tf - root module -resource "local_file" "clusters" { - filename = "${path.module}/clusters.tf" - file_permission = "0664" - directory_permission = "0775" - - content = <<-EOF - ### GKE #### - %{ for key in keys(local.clusters.gke) } - provider "google" { - alias = "${key}" - - region = local.clusters.gke["${key}"].location["region"] - project = local.clusters.gke["${key}"].auth["project_id"] - credentials = local.clusters.gke["${key}"].auth["credentials"] +locals { + backend = local.backends[keys(local.backends)[0]] + backend_type = local.backend.type + backend_args = join("\n", compact([ + for key, value in local.backend.args: + value != null ? format(" %s = \"%s\"", key, value) : null + ])) +} + +resource "null_resource" "backend" { + triggers = { + config = jsonencode(local.backend) + } + provisioner "local-exec" { + interpreter = ["/bin/bash", "-c"] + command = <<-EOT + cat > ${path.module}/backend.tf < ${path.module}/clusters.tf <