IT/Terraform

[Terraform] 테라폼 Best practice 네이밍 편

반응형

Intro

오늘은 terraform best pratice 문서에서 참조할만한 내용 공유 드리도록 하겠습니다. 원문은 여길(https://www.terraform-best-practices.com/naming) 참고 바라며 개인적으로 테라폼을 사용하는 사람들은 꼭 지켰으면 하는 3가지만 정리해보겠습니다.

1. 리소스 타입과 리소스명을 중복하지말기

resource "aws_route_table" "public_route_table" {}` → bad
resource "aws_route_table" "public" {} → good

1-1) 추가로 리소스명에 환경 명 넣지 말기 (향후 변경이 될 가능성이 있는 것들은 최대한 리소스명에 넣지 않도록 한다)

resource "aws_route_table" "prod_public" {}` → good
resource "aws_route_table" "public" {} → best

2. Count 말고 for_each 사용하기

  • best

    resource "aws_route_table" "public" {
    count = 2
    
    vpc_id = "vpc-12345678"
    # ... remaining arguments omitted
    }
    

resource "aws_route_table" "private" {
for_each = toset(["one", "two"])

vpc_id = "vpc-12345678"

... remaining arguments omitted

}
bad
resource "aws_route_table" "public" {
vpc_id = "vpc-12345678"
count = 2

... remaining arguments omitted

}



### 3. tag 위치 조정하기
- best

resource "aws_nat_gateway" "this" {
count = 2

allocation_id = "..."
subnet_id = "..."

tags = {
Name = "..."
}

depends_on = [aws_internet_gateway.this]

lifecycle {
create_before_destroy = true
}
}



- bad
tag가 파라미터 상단에 위치하지 않도록 한다

resource "aws_nat_gateway" "this" {
count = 2

tags = "..."

depends_on = [aws_internet_gateway.this]

lifecycle {
create_before_destroy = true
}

allocation_id = "..."
subnet_id = "..."
}


반응형