본문 바로가기

개발하자

테라폼 - 쿠버네티스 - if 변수가 존재하는 경우 사양 환경 생성

반응형

테라폼 - 쿠버네티스 - if 변수가 존재하는 경우 사양 환경 생성

나는 변수를 기반으로 자원을 생성하려고 노력한다.

variable "apps" {
  default = null
  type = map(object({
      name          = string
      type          = string
      secrets       = optional(map(string))
  }))
}

terraform.tfvars

apps = {

    "myfirst" = {
        name = "myfirst"
        type = "deploy"
        secrets = {
            "FIRST_VAR" = "TestVariable",
            "SECOND_VAR" = "SecontTestVariable",
            "THIRD" = "NothingHere"
        }
    },
    "second" ={
        name = "second"
        type = "deploy"
        secrets = {
            "SECRET_VAR" = "SecretVar"
        }
    },
    "simlepod" ={
        name = "simplepod"
        type = "deploy"
    },
    "another" ={
        name = "another"
        type = "pod"

그리고 나의

terraform {
  required_providers {
    kubernetes = {
      source = "hashicorp/kubernetes"
      version = "2.9.0"
    }
  }
  experiments = [module_variable_optional_attrs]
}

provider "kubernetes" {
    config_path    = "~/.kube/config"
  # Configuration options
}

resource "kubernetes_secret" "secret" {
  
  for_each = { for k in compact([for k, v in var.apps: v.secrets != null ? k : ""]): k => var.apps[k] }


  metadata {
    name = "${each.value.name}-secret"
  }

  data = each.value["secrets"]

}

resource "kubernetes_pod" "test" {

  for_each = { for k in compact([for k, v in var.apps: v.type =="deploy" ? k : ""]): k => var.apps[k] }

  metadata {
    name = "app-${each.value.name}"
  }

  spec {
    container {
      image = "nginx:1.21.6"
      name  = "test-${each.value.name}"

      
      env_from {
        secret_ref {
          name = kubernetes_secret.secret[each.value.name].metadata[0].name
        }
      }

      resources {
        limits = {
          cpu          = "0.5"
          memory       = "512Mi"
        }

        requests = {
          cpu          = "250m"
          memory       = "50Mi"
        }
      }
    }
  }

  timeouts {
    create = "60s"

  }
}

모든 개체에 변수가 있는 것은 아니기 때문에 오류가 발생합니다.

 terraform plan
╷
│ Warning: Experimental feature "module_variable_optional_attrs" is active
│ 
│   on main.tf line 8, in terraform:
│    8:   experiments = [module_variable_optional_attrs]
│ 
│ Experimental features are subject to breaking changes in future minor or patch
│ releases, based on feedback.
│ 
│ If you have feedback on the design of this feature, please open a GitHub issue to
│ discuss it.
╵
╷
│ Error: Unsupported block type
│ 
│   on main.tf line 68, in resource "kubernetes_pod" "test":
│   68:   dynamic "secret" {
│ 
│ Blocks of type "secret" are not expected here.

객체에 변수가 있을 때만 식을 사용하여 만드는 방법은 무엇인가요?




나는 해결책을 찾았다

...
      dynamic "env_from" {
        for_each = each.value.secrets[*]
        content {
          secret_ref {
            name = kubernetes_secret.secret[each.value.name].metadata[0].name
          }
        }
      }
...

효과가 있는 것 같아요. 보다 자세한 내용은 의 테라폼 문서에서 확인할 수 있습니다.


반응형