티스토리 뷰
반응형
4. max
max 사용법
max는 max 함수에 있는 숫자의 가장 큰수를 반환을 해줍니다.
max 문법
간단한 max를 먼저 보겠습니다.
max(숫자)
예시) main.tf 내용
output "max_test" {
value = max(5,9,4)
}
결과 확인
$ terraform apply --auto-approve
Changes to Outputs:
+ max_test = 9
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
max_test = 9
max_test = 9 인것처럼, max 함수에 있는 가장 큰 수인 9를 반환해주었습니다.
응용편
좀더 max 함수를 다양하게 사용을 하는 방법에 대해서 설명을 하겠습니다.
AWS의 subnet 수량을 최대치에 맞춰서 작업을 해야되는 상황이 발생이 된다면, 해당 함수를 이용해서
여러개의 subnet list에서 최대치를 확인 및 활용 할 수 있습니다.
예시) main.tf 내용
variable "WAS_1" {
description = "input max string"
type = list(string)
default = ["a","b","c"]
}
variable "DB_2" {
description = "input max string"
type = list(string)
default = ["d"]
}
variable "Dev_3" {
description = "input max string"
type = list(string)
default = ["e","f"]
}
output "max_test_list" {
value = max (
length(var.WAS_1),
length(var.DB_2),
length(var.Dev_3)
)
}
결과 확인
$ terraform apply --auto-approve
Changes to Outputs:
+ max_test_list = 3
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
max_test_list = 3
결과와 같이 여러개의 변수에 있는 리스트 값이 가장 큰것의 수를 확인이 가능합니다.
max를 이용하여 AWS 서비스를 생성 시 가장 큰 값을 기준으로 배포를 해야되는 경우 사용을 할 수도 있습니다.
반응형
'IaC > Terraform' 카테고리의 다른 글
[Terraform] 많이 사용 Meta Argument 3편 - element (0) | 2022.10.02 |
---|---|
[Terraform] 많이 사용 Meta Argument 2편 - format (0) | 2022.10.02 |
[Terraform] 많이 사용 Meta Argument 1편 - count (0) | 2022.10.02 |
Alicloud에서 Terraform을 활용하여 3tire 구축하기 (0) | 2021.06.17 |
댓글
반응형
최근에 올라온 글
- Total
- Today
- Yesterday