What is the best practice to organize the main.tf with multiple application directories?

9/10/2021

I have 3 applications in separate directories handled with the kubernetes manifest files.

And I want to use terraform to deploy all the applications in different directories.

The structure of the applications show below.

app-1
  L kubernetes_manifest
    L deployment.yaml. ## use Dockerfile to run the app 
  L app
    L app.py
  Dockerfile. ## wrap the app
app-2
  L kubernetes_manifest
    L deployment.yaml. ## use Dockerfile to run the app 
  L app
    L app.py
  Dockerfile. ## wrap the app
app-3
  L kubernetes_manifest
    L deployment.yaml. ## use Dockerfile to run the app 
  L app
    L app.py
  Dockerfile. ## wrap the app
app_terraform
   L main.tf

main.tf

resource "google_container_cluster" "test_cluster" {
  project = "test"
  name     = "test-cluster"
  initial_node_count       = 1
}

Is there a way to deploy all the applications in main.tf?

-- Eric Lee
kubernetes
terraform
terraform-provider-gcp

1 Answer

10/9/2021

I maintain the Kustomization Provider which allows you to use native Kubernetes YAML in Terraform.

You can either use the provider directly or use a convenience module I additionally provide. I maintain both as part of my Terraform framework for AKS, EKS and GKE.

The module approach may be more convenient, because then you can simply call the module once per app and have a good balance between maintainable code and no unnecessary coupling between the apps. The module also handles the ids_prio approach, which is more robust for larger numbers of K8s resources, out of the box.

Option 1: Provider

app-1.tf:

data "kustomization_overlay" "app_1" {
  resources = [
    "${path.root}/app-1/kubernetes_manifest/deployment.yaml"
  ]
}

resource "kustomization_resource" "test" {
  for_each = data.kustomization_build.app_1.ids

  manifest = data.kustomization_build.app_1.manifests[each.value]
}

Option 2: Module

app-1.tf:

module "app_1" {
  source  = "kbst.xyz/catalog/custom-manifests/kustomization"
  version = "0.1.0"

  configuration_base_key = "default"
  configuration = {
    default = {
      resources = [
        "${path.root}/app-1/kubernetes_manifest/deployment.yaml"
      ]
    }
  }
}
-- pst
Source: StackOverflow