brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Nov 15. 2022

19탄-18. 테라폼-AWS-조건문

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

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


<1> 조건문 3가지

<2> count 매개 변수

<3> for_each 와 for 표현식

<4> if 문자열 지시자




<1> 조건문 3가지


count 매개 변수parameter : 조건부 리소스에서 사용

for_each 와 for 표현식expressions : 리소스 내의 조건부 리소스 및 인라인 블록에 사용

If 문자열 지시자if string directive : 문자열 내의 조건문에 사용



<2>  count 매개 변수


1

분기처리

ASG 환경에서 조건부로 일부 사용자에게는 모듈을 생성해 주고, 일부는 생성해 주지 않는다.

Boolean입력 변수를 사용한다.


2

variable "enable_autoscaling" {

  description = "If set to true, enable auto scaling"

  type        = bool

}


<CONDITION> ? <TRUE_VAL> : <FALSE_VAL>


참고

https://github.com/brikis98/terraform-up-and-running-code/blob/master/code/terraform/05-tips-and-tricks/loops-and-if-statements/modules/services/webserver-cluster/variables.tf



3

테라폼은 <CONDITION> ? <TRUE_VAL> : <FALSE_VAL> 형식



resource "aws_autoscaling_schedule" "scale_out_during_business_hours" {

  count = var.enable_autoscaling ? 1 : 0


  scheduled_action_name  = "${var.cluster_name}-scale-out-during-business-hours"

  min_size               = 2

  max_size               = 10

  desired_capacity       = 10

  recurrence             = "0 9 * * *"

  autoscaling_group_name = aws_autoscaling_group.example.name

}


resource "aws_autoscaling_schedule" "scale_in_at_night" {

  count = var.enable_autoscaling ? 1 : 0


  scheduled_action_name  = "${var.cluster_name}-scale-in-at-night"

  min_size               = 2

  max_size               = 10

  desired_capacity       = 2

  recurrence             = "0 17 * * *"

  autoscaling_group_name = aws_autoscaling_group.example.name

}


4

var.enable_autoscaling 가 true 인 경우 각각의 aws_autoscaling_schedule 리소스에 대한 count 매개 변수가 1로 설정되므로 리소스가 각각 하나씩 생성됩니다.

var.enable_autoscaling 가 false 인 경우 각각의 aws_autoscaling_schedule 리소스에 대한 count 매개 변수가 0로 설정되므로 리소스가 생성되지 않습니다.


5

오토스케일링

https://github.com/brikis98/terraform-up-and-running-code/blob/master/code/terraform/05-tips-and-tricks/loops-and-if-statements/live/stage/services/webserver-cluster/main.tf




<3> for_each 와 for 표현식




<4> if 문자열 지시자


1

문자열 내에서 조건문을 사용하는 것이다.


2

%{ if <CONDITION> }<TRUEVAL>%{ endif }



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 }"

}


output "for_directive_index_if" {

  value = <<EOF

%{ for i, name in var.names }

  \${name}%{ if i < length(var.names) - 1 }, %{ endif }

%{ endfor }

EOF

}

EOT


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



Outputs:

for_directive = "gasida, akbun, fullmoon, "

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

for_directive_index_if = <<EOT

  gasida,

  akbun,

  fullmoon

EOT






다음

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




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



감사합니다.

매거진의 이전글 19탄-17. 테라폼-AWS-반복문 For표현식

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari