티스토리 뷰

반응형

3. element

element 사용법

element는 list에 있는 값을 count or 숫자에 대해서 값을 반환하고자 할 때 사용을 합니다.

element 문법

element(list, count.index)

예시) main.tf 내용

variable "region" {
  description = "input list value for test element"
  type        = list(string)
  default     = ["eu-west-1", "ap-northeast-2", "ap-east-1"]
}

output "element" {
        value = element(var.region, 2)

}

결과 확인

> terraform plan

Changes to Outputs:
  + element = "ap-northeast-2"

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

element vs [x] ([2]) 차이

예시) main.tf 내용

variable "region" {
  description = "input list value for test element"
  type        = list(string)
  default     = ["eu-west-1", "ap-northeast-2", "ap-east-1"]
}

output "element" {
    value = element(var.region, 2)

}

output "test" {
        value = var.region[2]

}

element와 []를 모두 동일한 count로 설정을 하였습니다.

아래 결과를 보시면 모두 동일한 값을 확인을 할 수있습니다

결과 확인

> terraform plan

Changes to Outputs:
  + element = "ap-east-1"
  + test    = "ap-east-1"

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

다만 list를 count 값을 이용을 하여 반복을 한다고 하면 어떤 차이가 있을까요?

var.region의 list를 count하게 되면 3을 반환하게 되며, 0부터 3까지 count.index 처리가 가능합니다.

예시) main.tf 내용

variable "region" {
  description = "input list value for test element"
  type        = list(string)
  default     = ["eu-west-1", "ap-northeast-2", "ap-east-1"]
}

output "count" {
        value = length(var.region)

}

결과 내용

> terraform plan

Changes to Outputs:
  + count   = 3

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

count의 마지막인 3으로 아래와 같이 설정 후 테스트를 해보겠습니다.

예시) main.tf 내용

variable "region" {
  description = "input list value for test element"
  type        = list(string)
  default     = ["eu-west-1", "ap-northeast-2", "ap-east-1"]
}

output "element" {
        value = element(var.region, 3)

}

output "test" {
        value = var.region[3]

}

결과 확인

> terraform plan
╷
│ Error: Invalid index
│
│   on main.tf line 18, in output "test":
│   18:         value = var.region[3]
│     ├────────────────
│     │ var.region is list of string with 3 elements
│
│ The given key does not identify an element in this collection value: the given index is greater than or equal to the length of
│ the collection.

그림과 같이 [3]이란 숫자는 리스트에서 정상 동작을 하지 않습니다. 그러면 element 만 사용을 한다면 어떻게 될까요?

예시) main.tf 내용

variable "region" {
  description = "input list value for test element"
  type        = list(string)
  default     = ["eu-west-1", "ap-northeast-2", "ap-east-1"]
}

output "element" {
        value = element(var.region, 3)

}

결과 확인

> terraform plan

Changes to Outputs:
  + element = "eu-west-1"

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

다시 리스트의 첫번째 값으로 반환이 되는 것을 볼 수 있습니다.

반응형
댓글
반응형
최근에 올라온 글
Total
Today
Yesterday