티스토리 뷰

반응형

1. count

count 활용 방안

원하는 수량의 resource를 생성을 하고자 사용할 수 있는 가장 단순한 방식으로 많이 사용하며, 또는 count를 조건문을 사용하여 리소스 배포 여부를 확인 후 배포할 수도 있습니다.

count 용도

  1. count의 경우 list에 있는 갯수를 이용하여 순차적으로 매핑을 하기도 합니다
  2. count를 if문과 같이 사용을 하여 결과를 0 : 1 과 같이 ture, false로 나온다면,
    if 문의 결과가 false이면 해당 작업은 skip이 되고, true일 경우에만 동작을 하게 할 수 있습니다.
    해당 방식이 앞에 예지 main.tf에서 다루고 있는 내용입니다

 

예시) main.tf 내용

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

resource "random_pet" "count_false_0" {
        count = false && length(var.region) > 0 ? 1 : 0

}

resource "random_pet" "count_true_1" {
        count = true && length(var.region) > 0 ? 1 : 0
}

random_pet은 테라폼에서 제공하는 random reousrce로 랜덤으로 단어를 생성을 해주는 기능입니다.

참고링크 : https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet

결과 확인

$ terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  + create

Terraform will perform the following actions:

  # random_pet.count_true_1[0] will be created
  + resource "random_pet" "count_true_1" {
      + id        = (known after apply)
      + length    = 2
      + separator = "-"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

 

random_pet.count_false_0에 대한 작업은 var.region이 0보다 크므로 조건문에 부합되지 않으므로 진행이 되지 않으므로, plan 시에도 나타나지 않습니다.

apply를 통해서 확인을 해보면 아래 그림처럼, random_pet.count_true_1 만 생성이 되는것을 확인 가능합니다.

$ terraform apply --auto-approve

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  + create

Terraform will perform the following actions:

  # random_pet.count_true_1[0] will be created
  + resource "random_pet" "count_true_1" {
      + id        = (known after apply)
      + length    = 2
      + separator = "-"
    }

Plan: 1 to add, 0 to change, 0 to destroy.
random_pet.count_true_1[0]: Creating...
random_pet.count_true_1[0]: Creation complete after 0s [id=one-kiwi]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

count_true_0 → count_false_1 을 하게 되면 어떻게 될까요?

예시 ) main.tf 내용

변경 내용 : count = true && length(var.region) > 0 ? 1 : 0 → count = false && length(var.region) > 0 ? 1 : 0

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

resource "random_pet" "count_false_0" {
        count = false && length(var.region) > 0 ? 1 : 0

}

resource "random_pet" "count_true_1" {
        #count = true && length(var.region) > 0 ? 1 : 0
        count = false && length(var.region) > 0 ? 1 : 0
}

 

결과 확인

$ terraform apply --auto-approve
random_pet.count_true_1[0]: Refreshing state... [id=one-kiwi]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  - destroy

Terraform will perform the following actions:

  # random_pet.count_true_1[0] will be destroyed
  # (because index [0] is out of range for count)
  - resource "random_pet" "count_true_1" {
      - id        = "one-kiwi" -> null
      - length    = 2 -> null
      - separator = "-" -> null
    }

Plan: 0 to add, 0 to change, 1 to destroy.
random_pet.count_true_1[0]: Destroying... [id=one-kiwi]
random_pet.count_true_1[0]: Destruction complete after 0s

Apply complete! Resources: 0 added, 0 changed, 1 destroyed.

기존에 생긴 내용이 삭제가 됩니다.

 

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