brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Nov 15. 2022

19탄-17. 테라폼-AWS-반복문 For표현식

다음은 주말 CloudNet 테라폼 스터디 내용 참고하여  정리한 부분입니다.

https://gasidaseo.notion.site/gasidaseo/CloudNet-Blog-c9dfa44a27ff431dafdd2edacc8a1863


<1> 반복문  - For 표현식

<2> 반복문  - 문자열 지시자



<1> 반복문  - For 표현식


1

기존것 삭제

cd test

terraform destroy -auto-approve

rm -rf *.tf



2

표현식

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



3

cat <<EOT > main.tf

variable "names" {

  description = "A list of names"

  type        = list(string)

  default     = ["gasida", "akbun", "fullmoon"]

}

EOT


4

계정이름을 모두 대문자로 바꾸어 보자~


cat <<EOT > main.tf

variable "names" {

  description = "A list of names"

  type        = list(string)

  default     = ["gasida", "akbun", "fullmoon"]

}


output "upper_names" {

  value = [for name in var.names : upper(name)]

}

EOT


5

terraform init && terraform plan && terraform apply -auto-approve


Outputs:

upper_names = [

  "GASIDA",

  "AKBUN",

  "FULLMOON",




6

names  글자가 6개보다 작은것을 출력해보자. 

조건을 지정해 아웃풋을 볼수 있다.

타입이 리스트


cat <<EOT > main.tf

variable "names" {

  description = "A list of names"

  type        = list(string)

  default     = ["gasida", "akbun", "fullmoon"]

}


output "upper_names" {

  value = [for name in var.names : upper(name)]

}


output "short_upper_names" {

  value = [for name in var.names : upper(name) if length(name) < 6]

}

EOT



 terraform apply -auto-approve


Outputs:

short_upper_names = [

  "AKBUN",

]

upper_names = [

  "GASIDA",

  "AKBUN",

  "FULLMOON",

]





7

맵으로  키와 값을  반복해 보자.


형식

[for <KEY>, <VALUE> in <MAP> : <OUTPUT>]



8

코드


cat <<EOT > main.tf

variable "names" {

  description = "A list of names"

  type        = list(string)

  default     = ["gasida", "akbun", "fullmoon"]

}


output "upper_names" {

  value = [for name in var.names : upper(name)]

}


output "short_upper_names" {

  value = [for name in var.names : upper(name) if length(name) < 5]

}


variable "hero_thousand_faces" {

  description = "map"

  type        = map(string)

  default     = {

    gasida    = "hero"

    akbun     = "love interest"

    fullmoon  = "mentor"

  }

}


output "bios" {

  value = [for name, role in var.hero_thousand_faces : "\${name} is the \${role}"]

}

EOT



terraform apply -auto-approve


Outputs:

bios = [

  "akbun is the love interest",

  "fullmoon is the mentor",

  "gasida is the hero",

]

short_upper_names = []

upper_names = [

  "GASIDA",

  "AKBUN",

  "FULLMOON",

]





9

terraform destroy -auto-approve

rm -rf *.tf




<2> 반복문  - 문자열 지시자


1

문자열 내에서 반복문,  if 같은 제어문을 사용할수 있습니다.


"Hello, ${var.name}"


2

FOR 형식

%{ for <ITEM> in <COLLECTION> }<BODY>%{ endfor }


ITEM은 COLLECTION 의 각 항목에 할당할 로컬 변수의 이름

COLLECTION 은 반복할 리스트 또는 맵이고 

BODY는 ITEM을 참조할 수 있는 각각의 반복을 렌더링하는 대상입니다


3


cat <<EOT > main.tf

variable "names" {

  description = "Names to render"

  type        = list(string)

  default     = ["gasida", "akbun", "fullmoon"]

}


output "for_directive" {

  value = "%{ for name in var.names }\${name}, %{ endfor }"

}

EOT


4

terraform apply -auto-approve


for_directive = "gasida, akbun, fullmoon, "




5

인덱스 추가


%{ for <INDEX>, <ITEM> in <COLLECTION> }<BODY>%{ endfor }


ITEM은 COLLECTION 의 각 항목에 할당할 로컬 변수의 이름

COLLECTION 은 반복할 리스트 또는 맵이고 

BODY는 ITEM을 참조할 수 있는 각각의 반복을 렌더링하는 대상입니다



6

코드 수정


cat <<EOT > main.tf

variable "names" {

  description = "Names to render"

  type        = list(string)

  default     = ["gasida", "akbun", "fullmoon"]

}


output "for_directive" {

  value = "%{ for name in var.names }\${name}, %{ endfor }"

}


output "for_directive_index" {

  value = "%{ for i, name in var.names }(\${i}) \${name}, %{ endfor }"

}

EOT



terraform apply -auto-approve


Outputs:

for_directive = "gasida, akbun, fullmoon, "

for_directive_index = "(0) gasida, (1) akbun, (2) fullmoon, "





7

terraform destroy -auto-approve

rm -rf *.tf




다음

https://brunch.co.kr/@topasvga/2811







https://brunch.co.kr/@topasvga/2421


감사합니다.











keyword
매거진의 이전글 19탄-16. 테라폼-AWS-반복문 & if문
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari