terraform

1. Terraform ์ด๋ž€?

  • ์—ฌ๋Ÿฌ ํด๋ผ์šฐ๋“œ์— ๋ฆฌ์†Œ์Šค๋ฅผ ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๋„๋ก API๋ฅผ ํ˜ธํ™˜ํ•ด ๋†“์€ ์˜คํ”ˆ ์†Œ์Šค tool

  • ์„œ๋น„์Šค ์‹คํ–‰์— ํ•„์š”ํ•œ ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๋„๊ตฌ๋ผ๋Š” ์ธก๋ฉด์— ์žˆ์–ด์„œ Chef, Ansible๊ณผ ๊ฐ™์€ ํ”„๋กœ๋น„์ €๋‹ ๋„๊ตฌ๋กœ์จ ๋ถ„๋ฅ˜๋จ

  • ์„ ์–ธํ˜• ์–ธ์–ด HCL (Hashicorp Configuration Language)๋กœ ์ธํ”„๋ผ ๊ตฌ์„ฑ์„ ์ž‘์„ฑ

  • ํ…Œ๋ผํผ์ด ์„ ์–ธํ˜• ์–ธ์–ด๋ฅผ ๋ถ„์„ ๋ฐ ํด๋ผ์šฐ๋“œ ํ”„๋กœ๋ฐ”์ด๋”์˜ API๋ฅผ ํ˜ธ์ถœ

2. Terraform ํŠน์„ฑ

(1) Infrastructure as Code

  • ์ธํ”„๋ผ๋ฅผ ์ฝ”๋“œ๋กœ ์ •์˜ํ•˜์—ฌ ์ƒ์‚ฐ์„ฑ๊ณผ ํˆฌ๋ช…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Œ

  • ์ •์˜ํ•œ ์ฝ”๋“œ๋ฅผ ์‰ฝ๊ฒŒ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์–ด ํšจ์œจ์ ์œผ๋กœ ํ˜‘์—… ๊ฐ€๋Šฅ

(2) Execution Plan

  • ๋ณ€๊ฒฝ ๊ณ„ํš๊ณผ ๋ณ€๊ฒฝ ์ ์šฉ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ณ€๊ฒฝ ๋‚ด์šฉ์„ ์ ์šฉํ•  ๋•Œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Œ

(3) Resource Graph

  • ์‚ฌ์†Œํ•œ ๋ณ€๊ฒฝ์ด ์ธํ”„๋ผ ์ „์ฒด์— ์–ด๋–ค ์˜ํ–ฅ์„ ๋ฏธ์น ์ง€ ๋ฏธ๋ฆฌ ํ™•์ธ ๊ฐ€๋Šฅ

  • ์ข…์†์„ฑ ๊ทธ๋ž˜ํ”„๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์ด ๊ทธ๋ž˜ํ”„๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ณ„ํš์„ ์„ธ์šฐ๊ณ , ์ด ๊ณ„ํš์„ ์ ์šฉํ–ˆ์„ ๋•Œ ๋ณ€๊ฒฝ๋˜๋Š” ์ธํ”„๋ผ ์ƒํƒœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

(4) Change Automation

  • ์—ฌ๋Ÿฌ ์žฅ์†Œ์— ๊ฐ™์€ ๊ตฌ์„ฑ์˜ ์ธํ”„๋ผ๋ฅผ ๊ตฌ์ถ•ํ•˜๊ณ  ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋„๋ก ์ž๋™ํ™”ํ•  ์ˆ˜ ์žˆ์Œ

  • ์ธํ”„๋ผ๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ๋“œ๋Š” ์‹œ๊ฐ„์„ ์ ˆ์•ฝ ๊ฐ€๋Šฅ

3. Terraform Registry

  • ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ ์ œ์–ด๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ๋‹ค์šด๋ฐ›๊ธฐ ์œ„ํ•œ ์ €์žฅ์†Œ

  • 500 ๊ฐœ ์ด์ƒ์˜ ์„œ๋น„์Šค ํ”„๋กœ๋ฐ”์ด๋” ์ง€์›

  • ์„œ๋น„์Šค ํ”„๋กœ๋ฐ”์ด๋”์˜ ์‹ ๋ขฐ์„ฑ์— ๋”ฐ๋ผ 3๊ฐœ์˜ Tier๋กœ ๊ตฌ๋ถ„ (Official, Verified, Community)

4. HCL (Hashicorp Configuration Language)

  • Hashicorp ์‚ฌ์—์„œ ๊ฐœ๋ฐœํ•œ ์„ ์–ธํ˜• ์–ธ์–ด (Json-Format)

  • Terraform์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์„ ์–ธํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ

resource "aws_vpc" "main" {
  cidr_block = var.base_cidr_block
}

<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
  # Block body
  <IDENTIFIER> = <EXPRESSION> # Argument
}

# Block: HCL ์—์„œ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด์˜ ์ปจํ…Œ์ด๋„ˆ (e.g. Resource, Variable ๋“ฑ)
# Argument: ๋ธ”๋ก ๋‚ด์—์„œ ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•จ
# Expression: ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ํ˜น์€ ๋‹ค๋ฅธ ๊ฐ’์„ ์ฐธ์กฐํ•˜์—ฌ ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ„

5. Terraform ๊ตฌ์กฐ

(1) ๊ตฌ์„ฑ ๋ชจ๋“ˆ

(a) Terraform CLI

  • ํ…Œ๋ผํผ ๋ช…๋ น์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋ณธ ๋ช…๋ น์ค„ ๋„๊ตฌ

  • Terraform Core ๊ธฐ๋Šฅ ํฌํ•จ: HCL ๋ถ„์„, ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ

(b) HCL (Hashicorp Configuration Language)

  • .tf ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง„ ์„ ์–ธํ˜• ์–ธ์–ด ํŒŒ์ผ

  • ํ”„๋กœ๋น„์ €๋‹ํ•  ๋ฆฌ์†Œ์Šค๋ฅผ ์„ ์–ธ

(c) Terraform State File

  • .tfstate ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง„ ์ƒํƒœ ํŒŒ์ผ

  • terraform ๋™์ž‘์ด ์ˆ˜ํ–‰๋˜์—ˆ์„ ๋•Œ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋ฉฐ ํ…Œ๋ผํผ ๋ช…๋ น์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ์— ๋”ฐ๋ฅธ ์ƒํƒœ์ •๋ณด ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋จ

(d) Terraform Plugin (Provider)

  • ์‚ฌ์šฉํ•˜๋Š” Cloud Platform (e.g. AWS, Azure)์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ์„ค์น˜

  • "terraform init" ๋ช…๋ น ์‹คํ–‰ ์‹œ์— ๋™์ž‘์œผ๋กœ ๋‹ค์šด๋กœ๋“œ๋จ

(2) Core vs. Plugins

  • Terraform ํŠน์„ฑ ์ƒ, ๋ชจ๋“  ํ”„๋กœ๋ฐ”์ด๋”๋ฅผ ์„ค์น˜ ์‹œ์ ๋ถ€ํ„ฐ ์ง€์›ํ•˜๊ธฐ์—๋Š” ๋น„ํšจ์œจ์ ์ž„

(3) Terraform ๋™์ž‘ ํ๋ฆ„

  • ํ…Œ๋ผํผ์€ ํฌ๊ฒŒ 4๋‹จ๊ณ„์˜ ๋™์ž‘ํ๋ฆ„์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Œ

(a) terraform init

  • Working Directory์— ํ•„์š”ํ•œ Terraform ํ™˜๊ฒฝ ๊ตฌ์„ฑ

  • HCL ํŒŒ์ผ ๋‚ด ๋ฆฌ์†Œ์Šค ๋‚ด์šฉ์„ ๊ธฐ์ค€์œผ๋กœ ํ•„์š”ํ•œ Terraform Plugin ๋ชจ๋“ˆ ์„ค์น˜

(b) terraform plan

  • ์‹ค์ œ ํ”„๋กœ๋น„์ €๋‹ ์ž‘์—… ์ด์ „์— ํ”Œ๋žœ ์ƒ์„ฑ

  • HCL ์ฝ”๋“œ๊ฐ€ ์–ด๋–ค ์ธํ”„๋ผ๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋ ์ง€ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋ฏธ๋ฆฌ ๋ณด์—ฌ์ฃผ๋Š” ๋‹จ๊ณ„

(c) terraform apply

  • HCL ํŒŒ์ผ ๋‚ด์šฉ์„ ์‹ค์ œ ์ธํ”„๋ผ ํ™˜๊ฒฝ์— ์ ์šฉ (Infrastructure as a Code)

(d) terraform destroy

  • Terraform Apply ์ดํ›„์— ๋ฐฐํฌํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์— ์‚ฌ์šฉ

Last updated