DevOps/AWS Architecture

[ DevOps ] - (테라폼을 이용한 인프라 관리) 테라폼 HCL 기초 문법

Hyunseo😊 2022. 7. 21. 13:03

우선 기본적인 HCL문법의 형태에 대해 알아보겠습니다.

https://www.terraform.io/language

 

Overview - Configuration Language | Terraform by HashiCorp

You can use the Terraform language to write configuration files that tell Terraform how to manage a collection of infrastructure.

www.terraform.io

다음과 같은 형태를 뜨게 됩니다. Block Type에 따라서 Label이 몇개 올지 결정됩니다. 그리고 그 안에는 Identifier와 expression이 key-value쌍으로 들어갈 수 있게 됩니다. 여기서 테라폼 코드를 구성하는 블록의 종류는 terraform, provider, resource, data, module, local, variable, output이 있습니다. 이는 이전에 간단히 실습할 때 써봤던 문법이므로 간단히 알아보고 넘어가도록 하겠습니다.

 

https://www.terraform.io/language/files

 

Files and Directories - Configuration Language | Terraform by HashiCorp

Learn how to name, organize, and store Terraform configuration files. Also learn how Terraform evaluates modules.

www.terraform.io

다음은 파일구조입니다.

만약 위 파란색 글씨와 같은 폴더 구조가 있다고 해 봅시다. 그리고 폴더 c에서 terraform apply를 하고 a, b폴더의 모듈을 사용했다고 하면 이 때, c폴더가 root module이 되고 a, b폴더가 child module이 되는 것입니다.

 

또한 HCL에서 지향하는 Style Convention이 존재합니다. 

https://www.terraform.io/language/syntax/configuration

 

Syntax - Configuration Language | Terraform by HashiCorp

Key constructs of the native Terraform language syntax, including identifiers, arguments, blocks, and comments.

www.terraform.io

이 형식대로 하기 위해서 terraform은 명령어를 통해 포멧팅을 자동으로 시켜줍니다.

$ terraform fmt -diff

이 명령어를 치게 되면 변경사항을 한눈에 알려주고 자동으로 포멧팅도 시켜줍니다.