★★cloudNet@ 팀의 가시다 님이 진행하는 Terraform 101 Study 4기 내용과
한빛미디어 테라폼으로 시작하는 IaC 책을 참고하여 정리하였습니다.
악분님이 제공한 count 실습 ★★★★★

 

  • 실습 환경 구성
    - 코드 가져오기
    git clone https://github.com/sungwook-practice/t101-study.git
    cd t101-study/count_vs_foreach
    cat Readme.md
    tree -L 1

    - 실습 환경 배포 : AWS VPC 생성
    cd template
    ls 
    cat terraform.tfvars
    terraform init && terraform plan && terraform apply -auto-approve
    terraform state list


    - AWS 웹 콘솔에서 생성된 VPC 확인

terraform VPC 확인

[ 시나리오 1] - aws_subnet
- AWS VPC Subnet 테라폼 코드 작성
- 요구사항 : subnet cidr를 변수로 입력

 

 

main.tf

: var.vpc_cidr -> variables.tf에서 선언

: var.subnet_cidr -> variabes.tf에서 선언

variables.tf

: 실제 vpc_cidr 값은 terraform.tfvars 에 cidr를 가져옴

: 실제 subnet_cidr 값은 terraform.tfvars에서 가져옴

terraform.tfvars

 

배포 후 생성된 서브넷 2개 확인
웹 콘솔에서 생성된 서브넷 확인

 

[ 시나리오 2] - index element length

- 요구사항 : subnet 여러 개 만들고 subnet cidr 변수 값을 여러 개 입력 받도록 설정

 

variables.tf 수정
terraform.tfvars
main.tf

: count = length(var.subnet_cidr) -> variables.tf의 type=list(string) --> terraform.tfvars의 subnet_cidr를 참조하니 2번의 사이클이 돈다

: 192.168.1.0/24의 서브넷 1개, 192.168.2.0/24의 서브넷 1개 생성

생성된 서브넷 확인
terraform state list

 

aws 웹콘솔에서 생성된 서브넷 확인

 

 

 

[ 시나리오3 ] - aws_subnet-arg_az (AZ for the subnet)

- 요구사항 : 서브넷이 배치되는 AZ 설정 필요. AZ는 변수로 입력

 

코드 수정

 

배포 후 원하는 AZ에 생성된 서브넷 확인
aws 웹콘솔에서 서브넷 확인

 

[시나리오 4] - aws_subnet-arg_tags (A map of tags assigned to the resource) 

- 요구사항 : subnet tag 설정

main.tf 수정

 

웹콘솔에서 서브넷 Name 확인

 

[시나리오 5]

- 요구사항 : subnet tag를 설정하는 변수 생성

코드 수정
배포 후 확인
aws 웹콘솔에서 Name 확인

[시나리오 6] (리팩토링) 

- 요구사항 : subnet 설정 변수를 한 변수로 설정하도록 리팩토링

코드 수정
생성된 서브넷 결과값

 

 

[시나리오7] 장애 상황 재현

- 요구사항 : 오류 확인 후 코드 수정

- 테라폼 변수에서 안쓰는 subnet을 삭제한 후, 테라폼을 실행하니 EC2 instance가 교체
EC2내에 작업한 내용이 다 사라짐

 

 

main.tf 코드 수정
terraform.tfvars 수정
배포 후 생성된 ec2 종료 중

 

** 장애 발생 원인 : 반복문을 index로 접근했기 때문

 

 


** 코드 동작을 직접 확인해볼 때

>> terraform console 로 확인

--------------------
> length(var.subnet_cidr)
2

> aws_vpc.main.id
"vpc-0c01f6cdbf3c9ad7a"

> exit
--------------------

 

** 공식문서 참조

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet#tags

 

Terraform Registry

 

registry.terraform.io

https://developer.hashicorp.com/terraform/language/functions/map

 

map - Functions - Configuration Language | Terraform | HashiCorp Developer

The map function constructs a map from some given elements.

developer.hashicorp.com

https://developer.hashicorp.com/terraform/language/meta-arguments/count#when-to-use-for_each-instead-of-count

 

The count Meta-Argument - Configuration Language | Terraform | HashiCorp Developer

Count helps you efficiently manage nearly identical infrastructure resources without writing a separate block for each one.

developer.hashicorp.com

https://developer.hashicorp.com/terraform/language/meta-arguments/count#when-to-use-for_each-instead-of-count

 

The count Meta-Argument - Configuration Language | Terraform | HashiCorp Developer

Count helps you efficiently manage nearly identical infrastructure resources without writing a separate block for each one.

developer.hashicorp.com