Terraform Module ์ •๋ฆฌ & ํ•ด์„

2022. 4. 25. 08:35ใ†Amazon Web Service/Terraform

Terraform Tutorial - Modules ์ •๋ฆฌ ๋ฐ ํ•ด์„ 

 

1. ๋ชจ๋“ˆ ์š”์•ฝ

๋ชจ๋“ˆ์€ ์™œ ํ•„์š”ํ• ๊นŒ?

  • Configuration์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ - ๋ชจ๋“ˆ์€ ์—ฐ๊ด€๋œ ์„ค์ •๋“ค์„ ๋ฌถ์–ด๋‘ ์œผ๋กœ์จ ํ™˜๊ฒฝ ๊ตฌ์„ฑ(configuration)์„ ์ฐพ๊ณ , ์ดํ•ดํ•˜๊ณ , ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ณผ์ •์„ ํ›จ์”ฌ ์‰ฝ๊ฒŒ ๋ฐ”๊พผ๋‹ค. ๊ตฌ์กฐ๊ฐ€ ๋ณต์žกํ•œ ์ธํ”„๋ผ๋Š” ์ˆ˜๋ฐฑ, ์ˆ˜์ฒœ ์ค„์˜ configuration์„ ์ ์šฉํ•ด์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ๋ชจ๋“ˆ์„ ํ†ตํ•ด ํ™˜๊ฒฝ ๊ตฌ์„ฑ๋“ค์„ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Configuration ์บก์Šํ™” - ๋ชจ๋“ˆ์˜ ๋˜ ๋‹ค๋ฅธ ์žฅ์ ์€ ํ™˜๊ฒฝ ๊ตฌ์„ฑ์„ ๊ฐœ๋ณ„์ ์ธ ๋…ผ๋ฆฌ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ์บก์Šํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์บก์Šํ™”๋Š” ์˜๋„ํ•˜์ง€ ์•Š์•˜๋˜ ์‹ค์ˆ˜๋กœ ๋‹ค๋ฅธ ์ธํ”„๋ผ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๋“ฑ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฆฌ์†Œ์Šค์— ๋™์ผํ•œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋‹จ์ˆœํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
  • Configuration ์žฌ์‚ฌ์šฉ - ๋ชจ๋“  ํ™˜๊ฒฝ๊ตฌ์„ฑ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ž‘์„ฑํ•˜๋Š” ๊ฑด ์‹œ๊ฐ„ ๋‚ญ๋น„์ผ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋”ฑ! ์ข‹์€ ํ–‰๋™์ด๋‹ค. ๋ณธ์ธ, ์•„๋‹ˆ๋ฉด ํŒ€์›๋“ค, ๊ทธ๋ฆฌ๊ณ  ํ…Œ๋ผํผ ๊ด€๊ณ„์ž๋“ค์ด ๋งŒ๋“ค์–ด ๋‘” ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ฐ„์„ ์ ˆ์•ฝํ•˜๊ณ , ์—๋Ÿฌ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๋ชจ๋“ˆ์„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๊ณผ ๊ณต์œ ํ•˜๋ฉฐ ์Šค์Šค๋กœ์˜ ๋ฉ‹์ง„ ์ž‘์—…์— credit์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค.โญ๏ธ
  • ์ผ๊ด€์„ฑ ์ œ๊ณต & best practice(๋ญ๋ผ๊ณ  ๋ฒˆ์—ญํ•ด์•ผํ• ์ง€..๐Ÿค”) - ๋ชจ๋“ˆ์€ ๋‹น์‹ ์˜ ํ™˜๊ฒฝ๊ตฌ์„ฑ์— ์ผ๊ด€์„ฑ์„ ์ œ๊ณตํ•œ๋‹ค. ์ผ๊ด€์„ฑ์€ ๋ณต์žกํ•œ ํ™˜๊ฒฝ๊ตฌ์„ฑ์„ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๋ชจ๋“  ๊ตฌ์„ฑ์— ๋ชจ๋ฒ”์ ์ธ ์ ์šฉ์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํด๋ผ์šฐ๋“œ ์ œ๊ณต ์—…์ฒด๋“ค์€ Amazon S3๋‚˜ Google Cloud Storage buckets ๊ฐ™์€ storage ์„œ๋น„์Šค๋“ค์„ ๊ตฌ์„ฑํ•˜๋Š”๋ฐ ๊ต‰์žฅํžˆ ๋งŽ์€ ์˜ต์…˜์„ ์ œ๊ณตํ•œ๋‹ค. ์ž˜๋ชป๋œ ๋ณด์•ˆ ์Šคํ† ๋ฆฌ์ง€์™€ ๊ด€๋ จํ•˜์—ฌ ๋งŽ์€ ๋ณด์•ˆ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๊ณ , ๋ณต์žกํ•œ ํ™˜๊ฒฝ ๊ตฌ์„ฑ ๋•Œ๋ฌธ์— ์ด๋Ÿฌํ•œ ์„œ๋น„์Šค๋ฅผ ์‹ค์ˆ˜๋กœ ์ž˜๋ชป ๊ตฌ์„ฑํ•˜๊ธฐ ์‰ฝ๋‹ค. 

 

๊ทธ๋Ÿผ Terraform ๋ชจ๋“ˆ์€ ๋ญ˜๊นŒ?

 

ํ…Œ๋ผํผ์—์„œ์˜ ๋ชจ๋“ˆ์€ ํ•˜๋‚˜์˜ ํ”„๋กœ์ ํŠธ ๋‹ค๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” Terraform confgiuration ํŒŒ์ผ์„ ๋ชจ๋‘ ๋œปํ•œ๋‹ค. ํ•˜๋‚˜ ์ด์ƒ์˜ .tf ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด ๊ฐ„๋‹จํ•˜๋“  ๋ณต์žกํ•˜๋“  ์ด๊ฒƒ๋“ค์€ ๋ชจ๋‘ configuration์ด๋‹ค. ๋งŒ์•ฝ ํ…Œ๋ผํผ ๋ช…๋ น์–ด๋“ค์„ ๋ฐ”๋กœ ์ „์ฒด ํ”„๋กœ์ ํŠธ ํด๋”์—์„œ ์ ์šฉํ•œ๋‹ค๋ฉด, ๊ทธ ํด๋”๋Š” root module๋กœ ๊ฐ„์ฃผ๋œ๋‹ค. ์ด๋Ÿฐ ๋งฅ๋ฝ์—์„œ ๋ชจ๋“  ํ…Œ๋ผํผ ํ™˜๊ฒฝ ๊ตฌ์„ฑ ํŒŒ์ผ๋“ค์€ '๋ชจ๋“ˆ'์˜ ๊ตฌ์„ฑ์›์ด ๋˜๋Š” ์…ˆ์ด๋‹ค. 

 

~/terraform_test
.
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ main.tf
โ”œโ”€โ”€ variables.tf
โ”œโ”€โ”€ outputs.tf

 

์ด ์˜ˆ์‹œ์—์„œ๋Š” terraform_test์ด๋ผ๋Š” ํด๋”๊ฐ€ root_modlue์ด ๋˜๋Š”๊ฒƒ์ด๋‹ค.

 

๋ชจ๋“ˆ ํ˜ธ์ถœ

 

Terraform ๋ช…๋ น์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” ๊ตฌ์„ฑ ํŒŒ์ผ๋งŒ ์ง์ ‘ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ™˜๊ฒฝ ๊ตฌ์„ฑ์—์„œ๋Š” ๋ชจ๋“ˆ ๋ธ”๋ก์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ๋””๋ ‰ํ† ๋ฆฌ์˜ ๋ชจ๋“ˆ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค!(๋ญ”๊ฐ€ ํŒŒ์ด์ฌ ๊ฐ™๊ธฐ๋„~) Terraform์€ ๋ชจ๋“ˆ ๋ธ”๋ก์„ ๋งŒ๋‚˜๋ฉด ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ๊ตฌ์„ฑ ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๊ณ  ์ฒ˜๋ฆฌํ•œ๋‹ค.
๋‹ค๋ฅธ ๊ตฌ์„ฑ์— ์˜ํ•ด ํ˜ธ์ถœ๋˜๋Š” ๋ชจ๋“ˆ์„ ํ•ด๋‹น ๊ตฌ์„ฑ์˜ "ํ•˜์œ„ ๋ชจ๋“ˆ"์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

 

๋กœ์ปฌ & ์›๊ฒฉ ๋ชจ๋“ˆ

 

๋ชจ๋“ˆ์€ ๋กœ์ปฌ์ด๋‚˜ ์›๊ฒฉ์—์„œ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. Terraform์€  Terraform Registry, ๋ฒ„์ „ ์ปจํŠธ๋กค ์‹œ์Šคํ…œ, HTTP URLs, Terraform Cloud๋‚˜ Terraform Enterprise ๊ฐœ์ธ ๋ชจ๋“ˆ registires๋“ฑ ๋‹ค์–‘ํ•œ ์›๊ฒฉ source๋ฅผ ์ง€์›ํ•œ๋‹ค.

 

 

๋ชจ๋“ˆ best practices

 

ํ…Œ๋ผํผ ๋ชจ๋“ˆ์€ ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ํŒจํ‚ค์ง€ ๋˜๋Š” ๋ชจ๋“ˆ์˜ ๊ฐœ๋…๊ณผ ์œ ์‚ฌํ•˜๋ฉฐ, ๋น„์Šทํ•œ ์žฅ์ ์„ ์ œ๊ณตํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋žจ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์‹ค์ œ Terraform ๊ตฌ์„ฑ๋“ค์€ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ์ด์ ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ๊ฑฐ์˜ ํ•ญ์ƒ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.(๊ทธ๋ƒฅ ํ•ญ์ƒ ๋ชจ๋“ˆ ์“ฐ๋ผ๋Š” ๊ฑฐ ๊ฐ™์€)


๋ชจ๋“  Terraform ์ด์šฉ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชจ๋ฒ” ์‚ฌ๋ก€(best practices)๋ฅผ ๋”ฐ๋ผ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

  1. provider์˜ ์ด๋ฆ„์„ terraform-<PROVIDER>-<NAME>๋กœ ์ง€์ •ํ•œ๋‹ค. Terraform Cloud ๋˜๋Š” Terraform Enterprise ๋ชจ๋“ˆ registires์— ๋ชจ๋“ˆ์„ ๋“ฑ๋กํ•˜๋ ค๋ฉด ์ด ๊ทœ์น™์„ ๋”ฐ๋ผ์•ผ ํ•œ๋‹ค.
  2. ๋ชจ๋“ˆ์„ ์—ผ๋‘์— ๋‘๊ณ !! ํ™˜๊ฒฝ ๊ตฌ์„ฑ ์ž‘์„ฑ์„ ์‹œ์ž‘ํ•˜์ž. ๊ฐœ์ธ์ด ๊ด€๋ฆฌํ•˜๋Š” ๋ณต์žกํ•œ Terraform ๊ตฌ์„ฑ์—์„œ๋„ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ชจ๋“ˆ์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ์‹œ๊ฐ„์ด ์ ๊ฒŒ ๊ฑธ๋ฆด ๊ฒƒ์ด๋‹ค. 
  3. ๋กœ์ปฌ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์บก์Šํ™”ํ•˜์ž. ์›๊ฒฉ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ฑฐ๋‚˜, ๊ฒŒ์‹œํ•˜์ง€ ์•Š๋”๋ผ๋„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ชจ๋“ˆ ์ธก๋ฉด์—์„œ ํ™˜๊ฒฝ ๊ตฌ์„ฑ์„ ๋งŒ๋“ค๋ฉด ์ธํ”„๋ผ๊ฐ€ ๋ณต์žกํ•ด์ ธ๋„ ์œ ์ง€ ๋ณด์ˆ˜ํ•˜๊ณ  ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋‹ด์„ ํฌ๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
  4. ๊ณต์šฉ Terraform Registry(https://registry.terraform.io/)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ ์šฉํ•œ ๋ชจ๋“ˆ์„ ์ฐพ์ž. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ผ๋ฐ˜์ ์ธ ์ธํ”„๋ผ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ž‘์—…์— ์˜์กดํ•˜์—ฌ ๊ตฌ์„ฑ์„ ๋ณด๋‹ค ๋น ๋ฅด๊ณ  ํ™•์‹คํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
  5. ๋ชจ๋“ˆ์„ ๊ฒŒ์‹œํ•˜๊ณ  ํŒ€๊ณผ ๊ณต์œ ํ•˜์ž. ๋Œ€๋ถ€๋ถ„์˜ ์ธํ”„๋ผ๋Š” ํ•œ ํŒ€์˜ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋ฉฐ, ๋ชจ๋“ˆ์€ ํŒ€์ด ํ•จ๊ป˜ ํ˜‘๋ ฅํ•˜์—ฌ ์ธํ”„๋ผ๋ฅผ ๋งŒ๋“ค๊ณ  ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ์ค‘์š”ํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค. ์•ž์—์„œ ์–ธ๊ธ‰ํ•œ ๋ฐ”์™€ ๊ฐ™์ด ๋ชจ๋“ˆ์„ ๊ณต๊ฐœ ๋˜๋Š” ๋น„๊ณต๊ฐœ๋กœ ๊ฒŒ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

2. Registry์—์„œ ๋ชจ๋“ˆ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

์‚ฌ์ „ ์š”๊ตฌ์‚ฌํ•ญ

 

ํ•ด๋‹น ํŠœํ† ๋ฆฌ์–ผ์„ ๋”ฐ๋ผํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์ „์— ์ค€๋น„ํ•ด์•ผํ•˜๋Š” ๊ฒƒ๋“ค

  • AWS ๊ณ„์ •
  • Terrafrom CLI

 

Terraform Registry ์‚ฌ์šฉํ•˜๊ธฐ

 

Terraform Registry page for the VPC module ๋งํฌ๋กœ ์ ‘์†!

 

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.21.0"
  # insert the 8 required variables here
}

 

๋ชจ๋“ˆ์„ ํ˜ธ์ถœํ•  ๋•Œ๋Š” source ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ Terraform์€ Terraform Registry์—์„œ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(source ๊ฐ’)๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“ˆ์„ ๊ฒ€์ƒ‰ํ•  ๊ฒƒ์ด๋‹ค. ๋ชจ๋“ˆ์˜ ์›๋ณธ์— ๋Œ€ํ•œ URL ๋˜๋Š” ๋กœ์ปฌ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๋ชจ๋“ˆ ์†Œ์Šค ๋ชฉ๋ก์€ Terraform ์„ค๋ช…์„œ๋ฅผ ์ฐธ์กฐํ•˜์ž.
source ์™ธ์— ํ•„์š”ํ•œ ์ธ์ˆ˜๋Š” version์ด๋‹ค. ๋ฒ„์ „์„ ํ†ตํ•ด ๋กœ๋“œํ•  ๋ชจ๋“ˆ์˜ ๋ฒ„์ „์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“ˆ์˜ ์ •ํ™•ํ•œ ๋ฒ„์ „ ๋ฒˆํ˜ธ๋ฅผ ์ง€์ •ํ•œ๋‹ค. ๋ชจ๋“ˆ ์„ค๋ช…์„œ์—์„œ ๋ฒ„์ „์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋ชจ๋“ˆ ๋ธ”๋ก์— ๋Œ€ํ•œ ๋‹ค๋ฅธ ์ธ์ˆ˜๋“ค์€ ๋ชจ๋“ˆ์— ๋Œ€ํ•œ ์ž…๋ ฅ ๋ณ€์ˆ˜๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.

 

Terraform Configuration ์ƒ์„ฑํ•˜๊ธฐ

 

์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์šฐ๋ฆฌ๋Š” ๋ชจ๋“ˆ์„ ์ด์šฉํ•ด์„œ Virtual Private Cloud(VPC)์™€ 2๊ฐœ์˜ EC2 ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ๋Š” AWS ํ™˜๊ฒฝ์„ ๋งŒ๋“ค์–ด ๋ณผ ๊ฒƒ์ด๋‹ค!

 

1. ๊นƒํ—™ ๋ ˆํฌ ํด๋ก , ~/learn-terraform-modules-use ํด๋”๋กœ ์ด๋™

git clone https://github.com/hashicorp/learn-terraform-modules-use.git
cd learn-terraform-modules-use

 

2. main.tf์—์„œ ํ™˜๊ฒฝ ๊ตฌ์„ฑ์„ ๋‚˜์—๊ฒŒ ๋งž๊ฒŒ ๋ฐ”๊พธ์–ด์ค€๋‹ค

 

region์„ ๋ฐ”๊ฟ”์ฃผ๊ณ , ami ๊ฐ’๋„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊ฟ”์ค€๋‹ค(ํŠœํ† ๋ฆฌ์–ผ์— ์žˆ๋Š” ami ๊ฐ’์€ ์šฐ๋ฆฌ ๋ฆฌ์ „์ด๋ž‘ ์•ˆ ๋งž๋‹ค). ์ธ์Šคํ„ด์Šค๋Š” micro๋กœ ์œ ์ง€(๋ˆ์ด...์—†์œผ๋‹ˆ๊นŒ..)

 

# Terraform configuration

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
    }
  }
}

provider "aws" {
  region = "ap-northeast-2"
}

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.21.0"

  name = var.vpc_name
  cidr = var.vpc_cidr

  azs             = var.vpc_azs
  private_subnets = var.vpc_private_subnets
  public_subnets  = var.vpc_public_subnets

  enable_nat_gateway = var.vpc_enable_nat_gateway

  tags = var.vpc_tags
}

module "ec2_instances" {
  source  = "terraform-aws-modules/ec2-instance/aws"
  version = "2.12.0"

  name           = "my-ec2-cluster"
  instance_count = 2

  ami                    = "ami-033a6a056910d1137"
  instance_type          = "t2.micro"
  vpc_security_group_ids = [module.vpc.default_security_group_id]
  subnet_id              = module.vpc.public_subnets[0]

  tags = {
    Terraform   = "true"
    Environment = "dev"
  }
}

 

  • terraform ๋ธ”๋ก์€ Terraform ์ž์ฒด๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. ์ด ๋ธ”๋ก์€ Hashipcorp provider registry์˜ aws provider๋ฅผ ์š”๊ตฌํ•œ๋‹ค.
  • provider "aws" ๋ธ”๋ก์€ provider(์ œ๊ณต์ž)๋ฅผ ์ •์˜ํ•œ๋‹ค. ๋ณธ์ธ์ด ์„ ํƒํ•œ ์ธ์ฆ ๋ฐฉ๋ฒ•(https://registry.terraform.io/providers/hashicorp/aws/latest/docs#authentication)์— ๋”ฐ๋ผ ๊ณต๊ธ‰์ž ๋ธ”๋ก์— ์ถ”๊ฐ€ ์ธ์ˆ˜๋ฅผ ํฌํ•จํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. (์ธ์ฆ ๋ฐฉ๋ฒ•๋„ ๋‹ค์Œ๋ฒˆ์— ํ•œ ๋ฒˆ ์ •๋ฆฌ๋ฅผ ์‹œ๋„)
  • module "vpc" ๋ธ”๋ก์€ ์ธํ”„๋ผ์—์„œ ๋„คํŠธ์›Œํ‚น ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•  Virtual Private Cloud(VPC)๋ฅผ ์ •์˜ํ•œ๋‹ค.
  • module "ec2_instaces" ๋ธ”๋ก์€ 2๊ฐœ์˜ ec2๋ฅผ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค.(instance_count = 2)

 

 

๋ชจ๋“ˆ input ๋ณ€์ˆ˜ ๊ฐ’ ์ง€์ •ํ•˜๊ธฐ

 

๋Œ€๋ถ€๋ถ„์˜ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ชจ๋“ˆ ๊ตฌ์„ฑ์— ์ž…๋ ฅ ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค. ๋ชจ๋“ˆ์„ ํ˜ธ์ถœํ•˜๋Š” ํ™˜๊ฒฝ ๊ตฌ์„ฑ์€ ๋ชจ๋“ˆ ๋ธ”๋ก์—์„œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋Š” ์ž…๋ ฅ ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๋ชจ๋“ˆ ๋ธ”๋ก์— ๋Œ€ํ•œ ๋Œ€๋ถ€๋ถ„์˜ ์ธ์ˆ˜๋Š” ์†Œ์Šค์™€ ๋ฒ„์ „์„ ์ œ์™ธํ•˜๊ณ  ๋ณ€์ˆ˜ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.

 

AWS VPC Terraform registry ํŽ˜์ด์ง€์— ํ•ด๋‹น ๋ชจ๋“ˆ์ด ์ง€์›ํ•˜๋Š” ๋ชจ๋“  ์ž…๋ ฅ ๋ณ€์ˆ˜๋ฅผ ์„ค๋ช…ํ•˜๋Š” Inputs(์ž…๋ ฅ) ํƒญ์ด ์žˆ๋‹ค.
์ผ๋ถ€ ์ž…๋ ฅ ๋ณ€์ˆ˜๋Š” ๋ชจ๋“ˆ์ด ๊ธฐ๋ณธ๊ฐ’์„ ์ œ๊ณตํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘ ์ž…๋ ฅํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. Terraform์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์‹คํ–‰๋˜๋ ค๋ฉด ์ด๋Ÿฐ ๊ฐ’์„ ์ž˜ ์ฑ„์›Œ๋„ฃ์–ด์•ผ ํ•œ๋‹ค. 


module "vpc" ๋ธ”๋ก ๋‚ด ์ž…๋ ฅ ๋ณ€์ˆ˜๋ฅผ ๊ฒ€ํ† ํ•ด๋ณด์ž. Terraform registry์—์„œ ๋ฌธ์„œํ™”๋œ ๊ฐ ์ž…๋ ฅ ๋ณ€์ˆ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์ž.

  • name - AWS๋‚ด VPC์ด๋ฆ„
  • cidr - VPC์—์„œ ์‚ฌ์šฉ๋˜๋Š” CIDR ๋ธ”๋ก ์„ค๋ช…
  • azs - VPC ์„œ๋ธŒ๋„ท์ด ์‚ฌ์šฉํ•  ๊ฐ€์šฉ ์ง€์—ญ ์„ค๋ช…
  • private_subnets - ๊ณต์šฉ IP ์ฃผ์†Œ๋‚˜ ๊ฒฝ๋กœ๊ฐ€ ์—†๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ํฌํ•จํ•˜๋Š” VPC ๋‚ด์˜ ์„œ๋ธŒ๋„ท
  • public_subnets - ๊ณต์šฉ IP ์ฃผ์†Œ๋‚˜ ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ํฌํ•จํ•˜๋Š” ์„œ๋ธŒ๋„ท
  • enable_nat_gateway - ๋งŒ์•ฝ ๊ฐ’์ด true๋ฉด, ๋ชจ๋“ˆ์ด NAT ๊ฒŒ์ดํŠธ์›จ์ด๋ฅผ private ์„œ๋ธŒ๋„ท์— provisioning
    • ํ”„๋กœ๋น„์ €๋‹(Provisioning)์ด๋ž€?
      ๋ฌด์—‡์ธ๊ฐ€ ์—ฌ๋Ÿฟ ์ค‘์— ์ตœ์ ์ธ ๊ฒƒ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ง€์‹์„ ๋ฏธ๋ฆฌ ์ค€๋น„ํ•ด ๋†“๊ณ  ์š”์ฒญ์— ๋งž๊ฒŒ ๊ณต๊ธ‰ํ•˜๋Š” ์ ˆ์ฐจ ์™€ ํ–‰์œ„๋ฅผ provisioning ๋ผ๊ณ  ํ•œ๋‹ค. ์ฆ‰, ๋ฏธ๋ฆฌ ์ •์˜๋œ ์ •์ฑ…์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋‚ด๋ ค (์ง€์›, ์„œํฌํŠธ) ์ฃผ๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. IT์ธํ”„๋ผ ์ง€์›์„ ์‚ฌ์šฉ์ž ๋˜๋Š” ๋น„์ฆˆ๋‹ˆ์Šค์˜ ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž๊ฒŒ ํ• ๋‹น ,๋ฐฐ์น˜,๋ฐฐํฌํ•ด์„œ ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋“œ๋ก ๋งŒ๋“ค์–ด ๋†“๋Š” ๊ฒƒ.
  • tags - AWS์—์„œ ๊ตฌ์„ฑ์—์„œ ํ”„๋กœ๋น„์ €๋‹ํ•œ ๊ฐ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ํƒœ๊ทธ๋ฅผ ์ง€์ •

 

Root input ๋ณ€์ˆ˜ ์ •์˜ํ•˜๊ธฐ

 

์ž…๋ ฅ ๋ณ€์ˆ˜๋ฅผ ๋ชจ๋“ˆ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ชจ๋“  Terraform ๊ตฌ์„ฑ์—์„œ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋งค์šฐ! ๋น„์Šทํ•˜๋‹ค. ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ ํŒจํ„ด์€, ๋‚˜์ค‘์— ๋ณ€๊ฒฝํ• /๋ณ€๊ฒฝ๋  ๋ชจ๋“ˆ ์ž…๋ ฅ ๋ณ€์ˆ˜๋ฅผ ์ฒดํฌํ•ด์„œ ํ™˜๊ฒฝ ๊ตฌ์„ฑ variables.tf ํŒŒ์ผ์— ์ ํ•ฉํ•œ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ผ์น˜ํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด๋Ÿฌํ•œ ๋ณ€์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ๋ชจ๋“ˆ ๋ธ”๋ก์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ชจ๋“  ๋ชจ๋“ˆ์˜ input ๋ณ€์ˆ˜๋“ค์ด ๋ณธ์ธ์˜ ํ™˜๊ฒฝ ๊ตฌ์„ฑ์— ์˜ํ•ด ์ •์˜ ๋  ํ•„์š”๋Š” ์—†๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ”„๋กœ๋น„์ €๋‹ํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—๋Š” NAT ๊ฒŒ์ดํŠธ์›จ์ด๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์ด VPC๋ฅผ ํ•ญ์ƒ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ enable_nat_gateway๋ฅผ ์„ค์ •ํ•˜๋ฉด ์—ญํšจ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

์ด๋Ÿฐ root input ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ™˜๊ฒฝ ๊ตฌ์„ฑ์—์„œ ์ด๋Ÿฐ ๋ณ€์ˆ˜๋ฅผ ๋”ฐ๋กœ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.

 

variables.tf:

# Input variable definitions

variable "vpc_name" {
  description = "Name of VPC"
  type        = string
  default     = "example-vpc"
}

variable "vpc_cidr" {
  description = "CIDR block for VPC"
  type        = string
  default     = "10.0.0.0/16"
}

variable "vpc_azs" {
  description = "Availability zones for VPC"
  type        = list(string)
  default     = ["ap-northeast-2a", "ap-northeast-2b", "ap-northeast-2c"]
  # ๊ฐ€์šฉ์˜์—ญ ๋ฆฌ์ „์— ๋งž๊ฒŒ ๋ฐ”๊พธ๊ธฐ
}

variable "vpc_private_subnets" {
  description = "Private subnets for VPC"
  type        = list(string)
  default     = ["10.0.1.0/24", "10.0.2.0/24"]
}

variable "vpc_public_subnets" {
  description = "Public subnets for VPC"
  type        = list(string)
  default     = ["10.0.101.0/24", "10.0.102.0/24"]
}

variable "vpc_enable_nat_gateway" {
  description = "Enable NAT gateway for VPC"
  type        = bool
  default     = true
}

variable "vpc_tags" {
  description = "Tags to apply to resources created by VPC module"
  type        = map(string)
  default = {
    Terraform   = "true"
    Environment = "dev"
  }
}

์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด vpc_nmae, vpc_cidr, vpc_azs ๋“ฑ ๋‹ค์–‘ํ•œ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ–ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ ,

main.tf ๋ฅผ ๋‹ค์‹œ ๋ณด๋ฉด ์—ฌ๊ธฐ์„œ ์ •์˜ํ•œ ๋ณ€์ˆ˜๋“ค์„ var.vpc_azs ํ˜•ํƒœ๋กœ ๋‹ค์‹œ ๋ธ”๋ก ๋‚ด์—์„œ ํ˜ธ์ถœํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

Root Ouput ๊ฐ’ ์ •์˜ํ•˜๊ธฐ

 

๋ชจ๋“ˆ์—๋Š” ์ถœ๋ ฅ๊ฐ’(Output ๊ฐ’)๋„ ์žˆ๋‹ค. ์ด ๊ฐ’์—๋Š” module.<MODULE ์ด๋ฆ„>.<OUTPUT ์ด๋ฆ„> ํ˜•ํƒœ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“ˆ ouput์€ Terraform registry์— output ํƒญ์— ์„œ์ˆ ๋˜์–ด ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์ž.

๋ชจ๋“ˆ ์ถœ๋ ฅ๊ฐ’์€ ํ™˜๊ฒฝ ๊ตฌ์„ฑ์˜ ๋‹ค๋ฅธ ๋ถ€๋ถ„์œผ๋กœ ์ „๋‹ฌ๋˜๊ฑฐ๋‚˜, root ๋ชจ๋“ˆ์˜ ์ถœ๋ ฅ๊ฐ’์œผ๋กœ ์ •์˜๋œ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ํ•ด๋‹น ํŠœํ† ๋ฆฌ์–ผ์„ ํ†ตํ•ด ๋ชจ๋‘ ํ™•์ธํ•  ๊ฒƒ์ด๋‹ค.

outputs.tf:

# Output variable definitions

output "vpc_public_subnets" {
  description = "IDs of the VPC's public subnets"
  value       = module.vpc.public_subnets
}

output "ec2_instance_public_ips" {
  description = "Public IP addresses of EC2 instances"
  value       = module.ec2_instances.public_ip
}

 

์ด ์˜ˆ์‹œ์—์„œ๋Š” vpc_public_subnets์˜ ๊ฐ’์€ vpc๋ผ๋Š” ๋ชจ๋“ˆ์˜ public_subnets ์ถœ๋ ฅ๊ฐ’์—์„œ ๋‚˜์˜ค๊ฒŒ ๋  ๊ฒƒ์ด๊ณ , ec2_instance_public_ips๋Š” module.ec2_instance.public_ip์œผ๋กœ ์ •์˜๋œ๋‹ค.

 

 

์ธํ”„๋ผ ํ”„๋กœ๋น„์ €๋‹

์ด์ œ terraform init์„ ํ•ด๋ณด์ž!!

 

1. terraform init

1. ์—๋Ÿฌ ๋ฐœ์ƒ

 

 

..๊ทธ๋ž˜์„œ ๋ฒ„์ „์„ ๋ฐ”๊ฟ”๋ดค๋‹ค.

 

2. ๋ฒ„์ „ ์ง€์ •

main.tf์—์„œ

 

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 3.27"
    }
  }
}

์ด๋ ‡๊ฒŒ ๋ฒ„์ „์„ ๋ฐ”๊ฟ”์คฌ๋‹ค.

 

3. upgrade ํ•˜๋ผ๋Š” ์—๋Ÿฌ

 

 

๊ทธ๋ž˜์„œ ์—…๊ทธ๋ ˆ์ด๋“œ ํ–ˆ์Šต๋‹ˆ๋‹ค ๋„ต.

 

 

4. ์„ฑ๊ณต ~

 

 

2. terraform apply

init์„ ํ†ตํ•ด provider์™€ ๋ชจ๋“ˆ์„ ์„ค์น˜ํ–ˆ๋‹ค.

์ด์ œ EC2 ์ธ์Šคํ„ด์Šค์™€ VPC๋ฅผ ์ƒ์„ฑํ•˜์ž.

 

์ž˜ ์ƒ์„ฑ๋˜์—ˆ๋‹ค! AWS์—์„œ๋„ ํ™•์ธํ•ด๋ณด์ž.

 

๋‘˜ ๋‹ค ์ž˜ ์ƒ์„ฑ๋˜์—ˆ๋‹ค!

 

 

๋ชจ๋“ˆ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๊ธฐ

์ƒˆ๋กœ์šด ๋ชจ๋“ˆ์„ ์ฒ˜์Œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” terraform init ์ด๋‚˜ terraform get ๋ช…๋ น์„ ํ†ตํ•ด ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•ด์•ผํ•œ๋‹ค. ์ด๋Ÿฐ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, Terraform์€ ๋ชจ๋“  ์ƒˆ ๋ชจ๋“ˆ์„ .terraform/modules ๋‹ค๋ ‰ํ† ๋ฆฌ์— ์ €์žฅํ•œ๋‹ค. ๋กœ์ปฌ ๋ชจ๋“ˆ์˜ ๊ฒฝ์šฐ Terraform์ด ๋ชจ๋“ˆ์˜ ๋””๋ ‰ํ† ๋ฆฌ์— ๋Œ€ํ•œ symblink์„ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋กœ์ปฌ ๋ชจ๋“ˆ์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์€ terraform get์„ ๋‹ค์‹œ ์‹คํ–‰ํ•  ํ•„์š” ์—†์ด ์ฆ‰์‹œ ์ ์šฉ๋œ๋‹ค.

 

ํŠœํ† ๋ฆฌ์–ผ์„ ์ž˜ ๋”ฐ๋ผํ–ˆ๋‹ค๋ฉด ๋ชจ๋“ˆ ๋‹ค๋ ‰ํ† ๋ฆฌ์— ์ด๋Ÿฐ ๊ฒฐ๊ณผ๋ฌผ์ด ์ €์žฅ๋˜์–ด ์žˆ๋‹ค.

 

 

์ธํ”„๋ผ clean up

Terraform registry์—์„œ ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•˜๊ณ  ํ…Œ์ŠคํŠธ๋„ ๋ชจ๋‘ ํ–ˆ์œผ๋‹ˆ, ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ ์ธํ”„๋ผ๋ฅผ terraform destory๋กœ ์—†์• ์ค€๋‹ค. 

 

 

 

3. ๋กœ์ปฌ ๋ชจ๋“ˆ ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ

์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์ •์  ์›น ์‚ฌ์ดํŠธ๋ฅผ ํ˜ธ์ŠคํŒ…ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” AWS S3 ๋ฒ„ํ‚ท์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ชจ๋“ˆ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„์ž์•„์ž~

๋ชจ๋“ˆ ๊ตฌ์กฐ

์•„๊นŒ ์•ž์„œ ๋งŒ๋“  ํด๋”์—์„œ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๋‚ด์šฉ๋“ค์— ๋Œ€ํ•ด ๋” ์ ์–ด๋ณด์ž.

 

terraform.tfstate ๋ฐ terraform.tfstate.backup: ์ด ํŒŒ์ผ์—๋Š” ์‚ฌ์šฉ์ž์˜ Terraform ์ƒํƒœ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๊ณ , Terraform์ด ์‚ฌ์šฉ์ž์˜ ๊ตฌ์„ฑ๊ณผ Terraform์—์„œ ํ”„๋กœ๋น„์ €๋‹๋œ ์ธํ”„๋ผ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ถ”์ ํ•˜๊ฒŒ ํ•œ๋‹ค.

.vargetform: ์ด ๋””๋ ‰ํ† ๋ฆฌ์—๋Š” ์ธํ”„๋ผ๋ฅผ ํ”„๋กœ๋น„์ €๋‹ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“ˆ ๋ฐ ํ”Œ๋Ÿฌ๊ทธ์ธ์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŒŒ์ผ์€ .tf ํŒŒ์ผ์— ์ •์˜๋œ ์ธํ”„๋ผ์˜ ๊ตฌ์„ฑ์ด ์•„๋‹ˆ๋ผ ์ธํ”„๋ผ๋ฅผ ํ”„๋กœ๋น„์ €๋‹ํ•  ๋•Œ ํŠน์ • Terraform ์ธ์Šคํ„ด์Šค๋กœ ํ•œ์ •๋œ๋‹ค.(์”..  ์ด๊ฒŒ ๋ฌด์Šจ ๋ง์ผ๊นŒ)

*.tfvars: ๋ชจ๋“ˆ ์ž…๋ ฅ ๋ณ€์ˆ˜๋Š” ๊ตฌ์„ฑ ๋‚ด ๋ชจ๋“ˆ ๋ธ”๋ก์— ๋Œ€ํ•œ ์ธ์ˆ˜๋ฅผ ํ†ตํ•ด ์„ค์ •๋˜๋ฏ€๋กœ ๋…๋ฆฝ ์‹คํ–‰ํ˜• Terraform ๊ตฌ์„ฑ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ•œ *.tfvars ํŒŒ์ผ์„ ๋ชจ๋“ˆ๊ณผ ํ•จ๊ป˜ ๋ฐฐํฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

-> ์ด ์„ธ๊ฐœ์˜ ํŒŒ์ผ๋“ค์€ ์จ๋ณด์ง€ ์•Š์•„์„œ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค..! ใ… .ใ…  

 

 

๋ชจ๋“ˆ ์ƒ์„ฑํ•˜๊ธฐ

git clone, terraform init

git clone https://github.com/hashicorp/learn-terraform-modules-create.git

terraform init ์ „์— ์•„๊นŒ์ฒ˜๋Ÿผ ๋ฆฌ์ „, ami, ๊ฐ€์šฉ์˜์—ญ ๋ชจ๋‘ ๋ฐ”๊ฟ”์ค„ ๊ฒƒ

 

์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํ™˜๊ฒฝ ๊ตฌ์„ฑ ๋‚ด์—์„œ AWS S3 ๋ฒ„ํ‚ท์„ ์‚ฌ์šฉํ•˜๋Š” ๋กœ์ปฌ submodule์„ ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น ๋ ˆํฌ๋ฅผ ํด๋ก ํ–ˆ๋‹ค๋ฉด, modules ์ด๋ผ๋Š” ํด๋”๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•˜์ง€๋งŒ ํด๋ก ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์ƒˆ๋กœ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ค€๋‹ค.

mkdir -p modules/aws-s3-static-website-bucket

 

์ •์  ์›น ์‚ฌ์ดํŠธ๋ฅผ S3๋กœ ํ˜ธ์ŠคํŒ…ํ•˜๋Š” ๊ฒƒ์€ ์ƒ๋‹นํžˆ ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€๋‹ค. ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ๋ฒ„ํ‚ท์„ ํ”„๋กœ๋น„์ €๋‹ํ•˜๋Š” ์˜ฌ๋ฐ”๋ฅธ ํ™˜๊ฒฝ ๊ตฌ์„ฑ์„ ์ฐพ๋Š” ๊ฒƒ์€ ๊ทธ๋ ‡๊ฒŒ ์–ด๋ ต์ง€ ์•Š์ง€๋งŒ,

๋ชจ๋“ˆ ๋‚ด์—์„œ ์ด ๊ตฌ์„ฑ์„ ์บก์Šํ™”ํ•˜๋ฉด ๋ชจ๋ฒ” ์‚ฌ๋ก€๋ฅผ ์ค€์ˆ˜ํ•˜๋Š” ์ •์  ์›น ์‚ฌ์ดํŠธ๋ฅผ ํ˜ธ์ŠคํŒ…ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํ‚ท์„ ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ๋•Œ์˜ ๋˜ ๋‹ค๋ฅธ ์ด์ ์€ ๋ชจ๋“ˆ ์ด๋ฆ„์ด ํ•ด๋‹น ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ๋œ ๋ฒ„ํ‚ท์˜ ์šฉ๋„๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด ์˜ˆ์—์„œ aws-s3-static-website-bucket ๋ชจ๋“ˆ์€ ์ •์  ์›น ์‚ฌ์ดํŠธ๋ฅผ ํ˜ธ์ŠคํŒ…ํ•˜๋Š” s3 ๋ฒ„ํ‚ท์„ ์ƒ์„ฑํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

๋ชจ๋“ˆ configuration ์ถ”๊ฐ€ํ•˜๊ธฐ

modules/aws-s3-static-website-bucket ๋‹ค๋ ‰ํ† ๋ฆฌ๋กœ ๋“ค์–ด๊ฐ€๋ฉด ๋˜ ๋‹ค์‹œ main.tf outputs.tf ๋“ฑ๊ณผ ๊ฐ™์€ ํŒŒ์ผ๋“ค์ด ์กด์žฌํ•จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ ์žˆ๋Š” main.tf์— S3 ๋ฒ„ํ‚ท ๋ฆฌ์†Œ์Šค๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ด๋‹ค.

 

# Terraform configuration

resource "aws_s3_bucket" "s3_bucket" {
  bucket = var.bucket_name

  tags = var.tags
}

resource "aws_s3_bucket_website_configuration" "s3_bucket" {
  bucket = aws_s3_bucket.s3_bucket.id

  index_document {
    suffix = "index.html"
  }

  error_document {
    key = "error.html"
  }
}

resource "aws_s3_bucket_acl" "s3_bucket" {
  bucket = aws_s3_bucket.s3_bucket.id

  acl = "public-read"
}

resource "aws_s3_bucket_policy" "s3_bucket" {
  bucket = aws_s3_bucket.s3_bucket.id

  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Sid       = "PublicReadGetObject"
        Effect    = "Allow"
        Principal = "*"
        Action    = "s3:GetObject"
        Resource = [
          aws_s3_bucket.s3_bucket.arn,
          "${aws_s3_bucket.s3_bucket.arn}/*",
        ]
      },
    ]
  })
}

 

์ด ํ™˜๊ฒฝ ๊ตฌ์„ฑ ์ฝ”๋“œ๋Š” public S3 bucket์ด ํ˜ธ์ŠคํŒ…ํ•˜๋Š”, index.html๊ณผ error.html๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ์›น ์‚ฌ์ดํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

์ด ์ฝ”๋“œ๋Š” ์•„๊นŒ ์ „ ํŠœํ† ๋ฆฌ์–ผ์—์„œ ํ™•์ธํ–ˆ๋˜ main.tf์™€ ๋‹ฌ๋ฆฌ providers ๋ผ๋Š” ๋ธ”๋ก์ด ์—†๋‹ค.

 

์œ—๋ถ€๋ถ„ main.tf์˜ providers:

provider "aws" {
  region = "ap-northeast-2"
}

Terraform์ด ๋ชจ๋“ˆ ๋ธ”๋ก์„ ์ฒ˜๋ฆฌํ•  ๋•Œ, ๋ชจ๋“ˆ ๋ธ”๋ก์€ ํ™˜๊ฒฝ ๊ตฌ์„ฑ ์ฝ”๋“œ์—์„œ provider๋ฅผ ์ƒ์†ํ•œ๋‹ค. ์ด ๋•Œ๋ฌธ์— ๋ชจ๋“ˆ์—๋Š” ๊ณต๊ธ‰์ž ๋ธ”๋ก์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

 

(๋‚ด๊ฐ€ ์ดํ•ดํ•œ ๋ฐ”: ์ด๋ฏธ ๋ชจ๋“ˆ ์™ธ์— ๊ธฐ๋ณธ ํ™˜๊ฒฝ ๊ตฌ์„ฑ ์ฝ”๋“œ main.tf์— provider ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต ์ƒ์† ์šฐ๋ ค๊ฐ€ ์žˆ์–ด์„œ ๋กœ์ปฌ ๋ชจ๋“ˆ main.tf์—๋Š” provider๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค)

๊ตฌ์„ฑ์˜ ๋ฃจํŠธ ๋ชจ๋“ˆ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ชจ๋“ˆ๋„ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ  ์‚ฌ์šฉํ•œ๋‹ค.

# Input variable definitions

variable "bucket_name" {
  description = "Name of the s3 bucket. Must be unique."
  type        = string
}

variable "tags" {
  description = "Tags to set on the bucket."
  type        = map(string)
  default     = {}
}

๋ชจ๋“ˆ ๋‚ด ๋ณ€์ˆ˜๋Š” ๋ฃจํŠธ ๋ชจ๋“ˆ๊ณผ ๊ฑฐ์˜ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค.

๋ฃจํŠธ ํ™˜๊ฒฝ ๊ตฌ์„ฑ์—์„œ Terraform ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋ฉด, ๋ณ€์ˆ˜ ๊ฐ’์„ commandline์— ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜, .tfvars ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ์„ค์ •ํ•˜๋Š” ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š”, ๋ชจ๋“ˆ์— ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•œ๋‹ค. ๋ฃจํŠธ ๋ชจ๋“ˆ์˜ main.tf์—์„œ ์ด ๋ชจ๋“ˆ์„ ํ˜ธ์ถœํ•  ๋•Œ ์ด๋Ÿฌํ•œ ๋ณ€์ˆ˜ ์ค‘ ์ผ๋ถ€๋ฅผ ์„ค์ •ํ•˜๊ฒŒ ๋œ๋‹ค.


๊ธฐ๋ณธ๊ฐ’์ด ์ง€์ •๋˜์ง€ ์•Š์€ ๋ชจ๋“ˆ์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š” ํ•„์ˆ˜์ด๋ฏ€๋กœ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ๋•Œ๋งˆ๋‹ค ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

๋ชจ๋“ˆ์„ ์ƒ์„ฑํ•  ๋•Œ ๋ชจ๋“ˆ ์ตœ์ข… ์‚ฌ์šฉ์ž์—๊ฒŒ ์ž…๋ ฅ ๋ณ€์ˆ˜๋กœ ํ‘œ์‹œํ•  ๋ฆฌ์†Œ์Šค ์ธ์ˆ˜๋ฅผ ๊ณ ๋ คํ•ด์•ผํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์›น ์‚ฌ์ดํŠธ๋ฅผ ํ˜ธ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ๋ฒ„ํ‚ท์˜ ACL์„ public-read์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ƒ‰์ธ ๋ฐ ์˜ค๋ฅ˜ ๋ฌธ์„œ๋ฅผ ๋ณ€์ˆ˜๋กœ ์ด ๋ชจ๋“ˆ์˜ ์ตœ์ข… ์‚ฌ์šฉ์ž์—๊ฒŒ ํ‘œ์‹œํ•˜์ง€๋งŒ, ACL์„ ์„ค์ •ํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.

ouput์€ ์‚ฌ์šฉ์ž๊ฐ€ ๋ชจ๋“ˆ์— ์˜ํ•ด ๊ตฌ์„ฑ๋œ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์ด๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅ์œผ๋กœ ์ถ”๊ฐ€ํ•  ๊ฐ’์„ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค.

modules/aws-s3-static-static-module-module ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์˜ outputs.tf ํŒŒ์ผ์— ์ถœ๋ ฅ์„ ๋ชจ๋“ˆ์— ์ถ”๊ฐ€ํ•˜์ž.

# Output variable definitions

output "arn" {
  description = "ARN of the bucket"
  value       = aws_s3_bucket.s3_bucket.arn
}

output "name" {
  description = "Name (id) of the bucket"
  value       = aws_s3_bucket.s3_bucket.id
}

output "domain" {
  description = "Domain name of the bucket"
  value       = aws_s3_bucket_website_configuration.s3_bucket.website_domain
}

 

๋ณ€์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ชจ๋“ˆ ouputs์€ root ๋ชจ๋“ˆ๊ณผ ๊ฐ™์€ ์ผ์„ ํ•˜์ง€๋งŒ, ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•˜๊ฒŒ ๋œ๋‹ค. ๋ชจ๋“ˆ ouputs์€ read-only์ด๋ฉฐ, access syntax ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค: module.<Moudle ์ด๋ฆ„>.<Ouput ์ด๋ฆ„>

 

์ด์ œ ๋ชจ๋“ˆ ์„ค์ •์ด ๋๋‚ฌ์œผ๋‹ˆ ๋‹ค์‹œ root์˜ main.tf์œผ๋กœ ๋Œ์•„๊ฐ€์„œ ๋ชจ๋“ˆ์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž.

 

์ด๋ ‡๊ฒŒ source๋ฅผ ๋ณด๋ฉด ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ๋กœ์ปฌ module๋ฅผ ์ง€์ •ํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ๋‹ค์‹œ ํ•œ ๋ฒˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ bucket_name ๋„ uniqueํ•˜๊ฒŒ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค. 

 

์ง€๊ธˆ ์—ฌ๊ธฐ์„œ ์„ค์ •ํ•œ buckent_name๊ณผ tags ์ธ์ž๋“ค์„ ๋ชจ๋“ˆ๋กœ ๋„˜๊ฒจ์ฃผ๊ฒŒ ๋˜๊ณ , ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  modules/aws-s3-static-website-bucket/variables.tf์— ์ผ์น˜ํ•˜๋Š” ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๊ฐ’์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.

 

 

๋กœ์ปฌ ๋ชจ๋“ˆ ์„ค์น˜ํ•˜๊ธฐ

terraform get ๋ช…๋ น์–ด๋กœ ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•˜์ž.

 

๋ฒ„ํ‚ท์— ํŒŒ์ผ ์—…๋กœ๋“œํ•˜๊ธฐ

์ด์ œ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  static website์„ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•ด๋ณด์ž. ์ด๊ฒƒ๋„ ์šฐ๋ฆฌ๋Š” ๊นƒํ—™ ๋ ˆํฌ๋ฅผ ํด๋ก ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—... ๋”ฑํžˆ index.html์„ ๋งŒ๋“ค ํ•„์š” ์—†์ด ๋ชจ๋“ ๊ฒŒ www ๋ผ๋Š” ํด๋”์— ์ค€๋น„ ๋˜์–ด ์žˆ๋‹ค(์•„์‹ธ)

 

๋‹ค์Œ ๋ช…๋ น์–ด๋กœ ๋ฒ„ํ‚ท์— ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•ด๋ณด์ž

 

aws s3 cp modules/aws-s3-static-website-bucket/www/ s3://$(terraform output -raw website_bucket_name)/ --recursive

 

 

์ด์ œ ์ง์ ‘ ์›น์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•ด๋ณด์ž.

์˜ˆ์‹œ: https://nayeon-tf-module-test.s3.ap-northeast-2.amazonaws.com/index.html

https://<๋ฒ„ํ‚ท์ด๋ฆ„>.s3.<๋ฆฌ์ „์ด๋ฆ„>/index.html

 

 

์•„์ฃผ ์ž˜ ๋œ๋‹ค.

 

์ด์ œ ๋ชจ๋“ ๊ฑธ ๋‹ค ํ–ˆ์œผ๋‹ˆ! ๋‹ค์‹œ ๋ชจ๋“ ๊ฑธ destory ํ•ด์ฃผ๊ณ  ๋๋‚ธ๋‹ค.

 

๋๋~