๐Ÿ“‚ IaC/Terraform

[Terraform] Configuration file๊ณผ HCL

dhyuck 2022. 8. 8. 22:01
๋ฐ˜์‘ํ˜•

Terraform & Configuration file

  • ํ…Œ๋ผํผ์€ IaC(Infra as a Code) Tool๋กœ ๊ตฌ์„ฑ ํŒŒ์ผ(configuration file)์„ ์„ ์–ธ์ (declarative)์œผ๋กœ ์ž‘์„ฑํ•จ์œผ๋กœ์„œ ์ธํ”„๋ผ๋ฅผ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.
  • ๊ตฌ์„ฑ ํŒŒ์ผ์€ ํ…Œ๋ผํผ์—๊ฒŒ ์„ค์น˜ํ•  ํ”Œ๋Ÿฌ๊ทธ์ธ, ์ƒ์„ฑํ•  ์ธํ”„๋ผ, ๊ฐ€์ ธ์˜ฌ ๋ฐ์ดํ„ฐ ๋“ฑ์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.
  • ๊ตฌ์„ฑ ํŒŒ์ผ์€ ํ…Œ๋ผํผ ๋„ค์ดํ‹ฐ๋ธŒ ์–ธ์–ด์ธ HCL(Hashicorp Configuration Language)๊ณผ JSON์œผ๋กœ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ .tf, .tf.json์˜ ํ™•์žฅ์ž๋ฅผ ๊ฐ–์œผ๋ฉฐ ํ‰๋ฌธ(plain text)์œผ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
  • ํ…Œ๋ผํผ์€ CDKTF(Cloud Development Kit for Terraform)์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. Typescript, Python, Java, Go, C#์œผ๋กœ ์ธํ”„๋ผ๋ฅผ ์ •์˜ํ•˜๋ฉด CDKTF๋Š” JSON์œผ๋กœ ์ž‘์„ฑ๋œ ํ…Œ๋ผํผ ๊ตฌ์„ฑ ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

HCL(Hashicorp Configuration Language)

  • HCL์€ Arguments์™€ Blocks์ด๋ผ๋Š” 2๊ฐ€์ง€ ์ค‘์‹ฌ ๊ตฌ๋ฌธ(key syntax)์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Arguments

    • ์ผ์ข…์˜ Attribute๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

      image_id = "abc123"
  • Blocks

    • ๋ธ”๋Ÿญ์€ ๋‹ค๋ฅธ ์ปจํ…์ธ ์˜ ์ปจํ…Œ์ด๋„ˆ๋กœ Blcok Type๊ณผ Block Type์— ๋”ฐ๋ฅธ Block Label์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

    • ๋ธ”๋Ÿญ์˜ ๋ณธ๋ฌธ(Block body)๋Š” {, }๋กœ ๊ตฌ๋ถ„๋˜๋ฉฐ Arguments๊ณผ Blocks์ด ์ค‘์ฒฉ๋˜์–ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

      <BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
      # Block body
      <IDENTIFIER> = <EXPRESSION> # Argument
      <NESTED BLOCK> {
          <IDENTIFIER> = <EXPRESSION> # Argument
      }
      }
    • Block Type์€ resource, data, provider, terraform, variable ๋“ฑ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Style Conventions

  • ํ…Œ๋ผํผ์€ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•œ ํŒŒ์ผ์ด๋‚˜ ๋ชจ๋“ˆ๊ด€์˜ ์ผ๊ด€์„ฑ์„ ์œ„ํ•ด ๊ถŒ์žฅ๋˜๋Š” ์Šคํƒ€์ผ ์ปจ๋ฒค์…˜(style conventions)์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. terraform fmt๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์Šคํƒ€์ผ ์ปจ๋ฒค์…˜์„ ์†์‰ฝ๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ 

Overview - Configuration Language | Terraform by HashiCorp

๋ฐ˜์‘ํ˜•