반응형
Jen'_'
Jen's Space
Email
GitHub
전체 방문자
오늘
어제
  • 분류 전체보기 (98)
    • Ansible (4)
      • 우아한앤서블 (4)
    • Cloud (19)
      • AWS (17)
      • GCP (2)
    • DevOps (40)
      • Docker (6)
      • Kubernetes (26)
      • CICD (6)
      • Terraform (2)
    • Language (13)
      • Python (12)
      • Node.js (1)
    • Linux (10)
    • etc. (7)
    • 개인기록 (1)
    • AEWS (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • 피드백 환영합니다 :)

인기 글

태그

  • 자격증
  • Kubernetes
  • karpenter
  • gitops
  • ubuntu
  • ansible
  • Docker
  • EKS
  • INGRESS
  • terraform
  • vagrant
  • 우아하게앤서블
  • CICD
  • autoscaling
  • 쿠버네티스
  • jenkins
  • AWS
  • ALB
  • k8s
  • argocd

최근 댓글

Jen'_'

Jen's Space

[Terraform] for Expressions
DevOps/Terraform

[Terraform] for Expressions

2023. 1. 3. 15:30
반응형

개요 

Terraform For문은 다른 언어랑 다소 다른 점이 있습니다.

테라폼 공식문서 설명도 잘 이해가 안 되고 예제도 자세하지 않아서 정리하는 글입니다.

아래 공식문서를 해석하고 예제를 돌립니다.

https://developer.hashicorp.com/terraform/language/expressions/for

 

For Expressions - Configuration Language | Terraform | HashiCorp Developer

For expressions transform complex input values into complex output values. Learn how to filter inputs and how to group results.

developer.hashicorp.com

 

 

A for expression's input (given after the in keyword) can be a list, a set, a tuple, a map, or an object.

in 뒤에 는 list, set, tuple, map, object 타입이 올 수 있다.

 

variable "list" {
  type     = list
  default  = ["a", "b", "c"]
}

output "list_output" {
  value = [for s in var.list : upper(s)]
}

 

outputs

list_output = ["A", "B", "C"]

 

위에서는 임시 기호 s만 있는 for 식을 보여주었다.

이번에는 각 항목의 key, value를 사용하기 위해 한 쌍의 임시 기호를 선언해 보겠다.

 

variable "map" {
  type     = map
  default  = {
    student1 = "jenana"
    student2 = "chachacha"
    student3 = "duck"
  }
}

output "map_output" {
  value = [for k, v in var.map : length(k) + length(v)]
}

 

outputs

map_output = [14, 17, 12]

 

The type of brackets around the for expression decide what type of result it produces.

The above example uses [ and ], which produces a tuple. If you use { and } instead, the result is an object and you must provide two result expressions that are separated by the => symbol:

 

for 표현식은 괄호에 따라 결과 타입이 달라진다.

위에 두 개의 예시는 [] 안에 for문을 정의했기 때문에 결과 타입이 tuple이었다.

만약 {} 안에 for문을 정의하면 결과 타입은 object이고, 반드시 => 기호로 구분되는 두 개의 결과 식을 제공해야 한다.

 

output "object_output" {
  value = {for s in var.list : s => upper(s)}
}

 

outputs

object_output = {
  a = "A"
  b = "B"
  c = "C"
}

 

if문을 사용하여 필터링할 수 있다.

 

variable "users" {
  type = map(object({
    is_admin   = bool
    age        = number
    like_color = string
  }))

  default = {
    "chacha" = {
      is_admin   = true
      age        = 25
      like_color = "puple"      
    }
    "duck" = {
      is_admin   = false
      age        = 30
      like_color = "red"      
    }
  }
}

locals {
  admin_users = { for name, user in var.users : name => user if user.is_admin }
  regular_users = { for name, user in var.users : name => user if !user.is_admin }
}

output "admin_users" {
  value = local.admin_users
}

output "regular_users" {
  value = local.regular_users
}

 

outputs

admin_users = {
  chacha = {
    age        = 25
    is_admin   = true
    like_color = "puple"
  }
}

regular_users = {
  duck = {
    age        = 30
    is_admin   = false
    like_color = "red"
  }
}

 

반응형
저작자표시 (새창열림)

'DevOps > Terraform' 카테고리의 다른 글

[Terraform] count vs for_each Meta-Argument 차이점 - 변수 list(object) 타입 사용  (0) 2023.06.23
    'DevOps/Terraform' 카테고리의 다른 글
    • [Terraform] count vs for_each Meta-Argument 차이점 - 변수 list(object) 타입 사용
    Jen'_'
    Jen'_'
    DevOps 엔지니어 Jen의 기술 블로그 / 이메일: jenakim47@gmail.com

    티스토리툴바