๐Ÿ“‚ IaC/Terraform

TF 101 Study 4์ฃผ์ฐจ ์ •๋ฆฌ - state, module

dhyuck 2023. 7. 29. 18:09
๋ฐ˜์‘ํ˜•
  • ๊ฐ€์‹œ๋‹ค๋‹˜์ด ์ง„ํ–‰ํ•˜์‹œ๋Š” CloudNeta์˜ Terraform 101 Study์— ์ฐธ๊ฐ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
  • ์Šคํ„ฐ๋”” ๊ต์žฌ โ†’ ํ…Œ๋ผํผ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” IaC

CH 5. State


  • ํ…Œ๋ผํผ์€ Statefulํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ โ†’ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜
  • ํ”„๋กœ๋น„์ €๋‹ ๊ฒฐ๊ณผ๋ฅผ State์— ์ €์žฅํ•˜๊ณ  ํ”„๋กœ๋น„์ €๋‹ํ•œ ๋ชจ๋“  ๋‚ด์šฉ์„ ์ €์žฅ๋œ ์ƒํƒœ๋กœ ์ถ”์ 
  • terraform.tfstate ํŒŒ์ผ์— JSON ์œผ๋กœ ์ €์žฅ โ†’ ํ˜ผ์ž ๋กœ์ปฌ์—์„œ ๊ด€๋ฆฌํ•˜๊ฑฐ๋‚˜ ํ˜‘์—…ํ•˜๊ธฐ ์œ„ํ•ด ์›๊ฒฉ ์ €์žฅ์†Œ์—์„œ ๊ด€๋ฆฌ
  • State์—๋Š” ์ž‘์—…์ž๊ฐ€ ์ •์˜ํ•œ ์ฝ”๋“œ์™€ ์‹ค์ œ ํ”„๋กœ๋น„์ €๋‹ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅ โ†’ ์ดํ›„ํ›„์˜ ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œ์— ๋Œ€ํ•œ ๋™์ž‘์„ ํŒ๋‹จ

5-1. State์˜ ๋ชฉ์ ๊ณผ ์˜๋ฏธ


  • terraform apply โ†’ State๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ด์ „์— ์ƒ์„ฑ๋œ ๋ฆฌ์†Œ์Šค์™€ ๋น„๊ตํ•ด ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œ ๋™์ž‘์ด ์ˆ˜ํ–‰

  • State์˜ ์—ญํ• 

    • State์—๋Š” ํ…Œ๋ผํผ ๊ตฌ์„ฑ๊ณผ ์‹ค์ œ๋ฅผ ๋™๊ธฐํ™”ํ•˜๊ณ  ๊ฐ ๋ฆฌ์†Œ์Šค์— ๊ณ ์œ ํ•œ ์•„์ด๋””(๋ฆฌ์†Œ์Šค ์ฃผ์†Œ)๋กœ ๋งตํ•‘
    • ๋ฆฌ์†Œ์Šค ์ข…์†์„ฑ๊ณผ ๊ฐ™์€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ถ”์ 
    • ํ…Œ๋ผํผ ๊ตฌ์„ฑ์œผ๋กœ ํ”„๋กœ๋น„์ €๋‹ ๊ฒฐ๊ณผ๋ฅผ ์บ์‹ฑํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰
  • Plan์„ ์‹คํ–‰ํ•˜๋ฉด ์•”๋ฌต์ ์œผ๋กœ refresh ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ์‹ค์ œ ๋ฆฌ์†Œ์Šค์™€ State๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.

  • Plan์—์„œ์˜ -refresh=false
    โ†’ ๋Œ€๋Ÿ‰์œผ๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ ์‹ค์ œ ๋ฆฌ์†Œ์Šค์™€ State๋ฅผ ๋น„๊ตํ•˜๋Š”๋ฐ ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Œ
    โ†’ State๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‹คํ–‰ ๊ณ„ํš์„ ์ƒ์„ฑํ•˜๋Š” ์˜ต์…˜

5-2. State ๋™๊ธฐํ™”


  • ํ…Œ๋ผํผ์€ configuration ํŒŒ์ผ๊ณผ ๊ธฐ์กด State๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •

  • configuration ํŒŒ์ผ์€ ํ…Œ๋ผํผ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•œ ํŒŒ์ผ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

  • ํ…Œ๋ผํผ์€ configuration ํŒŒ์ผ ๋˜๋Š” State ํŒŒ์ผ์— ์ •๋ณด๊ฐ€ ์žˆ๋Š” ๋ฆฌ์†Œ์Šค๋งŒ์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

  • configuration - ๋ฆฌ์†Œ์Šค ์กด์žฌ / state - ๋ฆฌ์†Œ์Šค ์—†์Œ โ†’ ๋ฆฌ์†Œ์Šค create

  • configuration - ๋ฆฌ์†Œ์Šค ์กด์žฌ / state - ๋ฆฌ์†Œ์Šค ์กด์žฌ โ†’ ๋ฆฌ์†Œ์Šค update ๋˜๋Š” no changes

  • configuration - ๋ฆฌ์†Œ์Šค ์—†์Œ / state - ๋ฆฌ์†Œ์Šค ์กด์žฌ โ†’ ๋ฆฌ์†Œ์Šค destroy

5-3. ์›Œํฌ์ŠคํŽ˜์ด์Šค


  • State๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋…ผ๋ฆฌ์ ์ธ ๊ฐ€์ƒ๊ณต๊ฐ„ โ†’ ์›Œํฌ์ŠคํŽ˜์ด์Šค

  • ์›Œํฌ์ŠคํŽ˜์ด์Šค๋ฅผ ๊ตฌ๋ถ„ํ•˜๋ฉด ๋™์ผํ•œ ๊ตฌ์„ฑ์—์„œ ๊ธฐ์กด ์ธํ”„๋ผ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด์„œ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ…Œ๋ผํผ ํ”„๋กœ๋น„์ €๋‹์„ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๊ธฐ๋ณธ ์›Œํฌ์ŠคํŽ˜์ด์Šค๋Š” default๋กœ ์ •์˜๋˜๊ณ , ์‚ฌ์šฉ ์ค‘์ธ ์›Œํฌ์ŠคํŽ˜์ด์Šค๋Š” terraform workspace list ๋ช…๋ น์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

  • terraform workspace new <์›Œํฌ์ŠคํŽ˜์ด์Šค ์ด๋ฆ„> : ์ƒˆ๋กœ์šด ์›Œํฌ์ŠคํŽ˜์ด์Šค ์ƒ์„ฑ

  • configuration ํŒŒ์ผ์—์„œ๋Š” terraform.workspace๋กœ ์›Œํฌ์ŠคํŽ˜์ด์Šค์˜ ์ด๋ฆ„์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ํ…Œ๋ผํผ OSS์˜ ์›Œํฌ์ŠคํŽ˜์ด์Šค๋Š” ์™„๋ฒฝํ•œ ๊ฒฉ๋ฆฌ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ์— ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ธฐ๋ฐ˜์˜ ๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

  • ์›Œํฌ์ŠคํŽ˜์ด์Šค๋ฅผ ์‹œ์Šคํ…œ์ ์œผ๋กœ ๋ณด์™„ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ํ…Œ๋ผํผ ํด๋ผ์šฐ๋“œ ๋˜๋Š” ํ…Œ๋ผํผ ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

CH 6. ๋ชจ๋“ˆ


  • ํ…Œ๋ผํผ์œผ๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๋‹ค๋ณด๋ฉด ์‹œ๊ฐ„ ์ง€๋‚ ์ˆ˜๋ก ๊ด€๋ฆฌํ•˜๋Š” ๋ฆฌ์†Œ์Šค๊ฐ€ ๋งŽ์•„์ง€๊ณ  ๋ณต์žกํ•ด์ง„๋‹ค.
    โ†’ ๋ฆฌ์†Œ์Šค ๊ฐ„์˜ ์—ฐ๊ด€ ๊ด€๊ณ„๊ฐ€ ๋ณต์žกํ•ด์ง€๊ณ  ๋ณ€๊ฒฝ ์ž‘์—…์˜ ์˜ํ–ฅ๋„๋ฅผ ๋ถ„์„ํ•˜๊ธฐ ์–ด๋ ค์›Œ์ง„๋‹ค.

  • ๋ชจ๋“ˆ โ†’ ํ…Œ๋ผํผ ๊ตฌ์„ฑ์˜ ์ง‘ํ•ฉ

  • ๋ชจ๋“ˆ์€ ํ…Œ๋ผํผ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋Œ€์ƒ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๊ณ  ๋ณต์žกํ•ด์ง„ ๋ฌธ์ œ๋ฅผ ๋ณด์™„ํ•˜๊ณ  ๊ด€๋ฆฌ ์ž‘์—…์„ ์ˆ˜์›”ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

  • ๋ชจ๋“ˆ์€ ๋ฃจํŠธ ๋ชจ๋“ˆ๊ณผ ์ž์‹ ๋ชจ๋“ˆ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค.

    • ๋ฃจํŠธ ๋ชจ๋“ˆ(Root Module) โ†’ ํ…Œ๋ผํผ์„ ์‹คํ–‰ํ•˜๊ณ  ํ”„๋กœ๋น„์ €๋‹ํ•˜๋Š” ์ตœ์ƒ์œ„ ๋ชจ๋“ˆ
    • ์ž์‹ ๋ชจ๋“ˆ(Child Module) โ†’ ๋ฃจํŠธ๋ชจ๋“ˆ์˜ ๊ตฌ์„ฑ์—์„œ ํ˜ธ์ถœํ•˜๋Š” ์™ธ๋ถ€ ๊ตฌ์„ฑ ์ง‘ํ•ฉ
  • ๋ชจ๋“ˆ ์‚ฌ์šฉ์˜ ์žฅ์  โ†’ ๊ด€๋ฆฌ ํšจ์œจํ™”, ์บก์Šํ™”, ์žฌ์‚ฌ์šฉ์„ฑ, ์ผ๊ด€์„ฑ, ํ‘œ์ค€ํ™”

6-1. ๋ชจ๋“ˆ ์ž‘์„ฑ ๊ธฐ๋ณธ ์›์น™


  • ๋ชจ๋“ˆ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๊ถŒ์žฅ ํ˜•์‹ โ†’ terraform-<ํ”„๋กœ๋ฐ”์ด๋” ์ด๋ฆ„>-<๋ชจ๋“ˆ ์ด๋ฆ„>
  • ํ…Œ๋ผํผ ์ฝ”๋“œ๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ชจ๋“ˆํ™”๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ๊ตฌ์„ฑํ•  ๊ฒƒ์„ ๊ถŒ์žฅ
  • ๊ฐ๊ฐ์˜ ๋ชจ๋“ˆ์€ ๋…๋ฆฝ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ
  • ๊ณต๊ฐœ๋œ ํ…Œ๋ผํผ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ์˜ ๋ชจ๋“ˆ์„ ์ฐธ๊ณ ํ•  ๊ฒƒ์„ ๊ถŒ์žฅ
  • ์ž‘์„ฑํ•œ ๋ชจ๋“ˆ์€ ๊ณต๊ฐœ ๋˜๋Š” ๋น„๊ณต๊ฐœ๋กœ ๊ณต์œ  ํ•  ๊ฒƒ์„ ๊ถŒ์žฅ โ†’ ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๊ณ  ํ”ผ๋“œ๋ฐฑ์„ ํ†ตํ•ด ๋” ๋ฐœ์ „๋œ ๋ชจ๋“ˆ์„ ๊ตฌ์„ฑ

6-2. ๋ชจ๋“ˆํ™”ํ•ด๋ณด๊ธฐ


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

  • ๋ชจ๋“ˆ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ž…๋ ฅ ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑ
    โ†’ ๋ชจ๋“ˆ์—์„œ ํ•„์š”ํ•œ ๊ฐ’์€ variable๋กœ ์ž…๋ ฅ ๋ฐ›๊ณ , ๋ชจ๋“ˆ์—์„œ ์ƒ์„ฑํ•œ ๊ฐ’ ์ค‘ ์™ธ๋ถ€ ์ฐธ์กฐ๊ฐ€ ํ•„์š”ํ•œ ๊ฐ’์€ output์œผ๋กœ ๊ตฌ์„ฑ

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

  • ๋ชจ๋“ˆ๋กœ ๋ฌถ์—ฌ์ง„ ๋ฆฌ์†Œ์Šค๋Š” module์ด๋ผ๋Š” ์ •์˜๋ฅผ ํ†ตํ•ด ๋‹จ์ˆœํ•˜๊ฒŒ ์žฌํ™œ์šฉํ•˜๊ณ  ๋ฐ˜๋ณต ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๋ชจ๋“ˆ์˜ ๊ฒฐ๊ณผ ์ฐธ์กฐ โ†’ module.<๋ชจ๋“ˆ ์ด๋ฆ„>.<output ์ด๋ฆ„>

6-3. ๋ชจ๋“ˆ ์‚ฌ์šฉ ๋ฐฉ์‹


  • ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“  ๋ฆฌ์†Œ์Šค๋Š” ๊ด€๋ จ ํ”„๋กœ๋ฐ”์ด๋”์˜ ์ •์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

  • ํ”„๋กœ๋ฐ”์ด๋” ์ •์˜์˜ ์œ„์น˜

    • ์ž์‹ ๋ชจ๋“ˆ์—์„œ ํ”„๋กœ๋ฐ”์ด๋” ์ •์˜

      โ†’ ์ž์‹ ๋ชจ๋“ˆ์˜ ํ”„๋กœ๋ฐ”์ด๋” ๋ฒ„์ „์ด ๋ฏผ๊ฐํ•˜๊ฑฐ๋‚˜ ์ž์‹ ๋ชจ๋“ˆ์ด ๋…๋ฆฝ์ ์ธ ๊ตฌ์กฐ์ผ ๋•Œ ์‚ฌ์šฉ
      โ†’ ๋ฃจํŠธ ๋ชจ๋“ˆ๊ณผ ์ž์‹ ๋ชจ๋“ˆ์˜ ํ”„๋กœ๋ฐ”์ด๋” ๋ฒ„์ „์ด ์ผ์น˜๋˜์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฐฉ์‹

    • ๋ฃจํŠธ ๋ชจ๋“ˆ์—์„œ ํ”„๋กœ๋ฐ”์ด๋” ์ •์˜
      โ†’ ์ž์‹ ๋ชจ๋“ˆ์ด ๋ฃจํŠธ ๋ชจ๋“ˆ์˜ ํ”„๋กœ๋ฐ”์ด๋” ๊ตฌ์„ฑ์— ์ข…์†๋˜๋Š” ๋ฐฉ์‹
      โ†’ ํ”„๋กœ๋ฐ”์ด๋”๋ฅผ ๋ชจ๋“ˆ ๋‚ด ๋ฆฌ์†Œ์Šค์™€ ๋ฐ์ดํ„ฐ ์†Œ์Šค์— ์ผ๊ด„ ์ ์šฉ

        # ๋ฃจํŠธ ๋ชจ๋“ˆ์—์„œ aws.seoul์„ ์ •์˜ํ•˜๊ณ  ec2_seoul ๋ชจ๋“ˆ์„ ํ˜ธ์ถœ
        provider "aws" {
          alias  = "seoul"
          region = "ap-northeast-2"
        }
      
        module "ec2_seoul" {
          provider = {
            aws = aws.seoul
          }
          source = "../modules/terraform-aws-ec2"
          instance_type = "m5.large"
        }
  • ๋ชจ๋“ˆ์—๋„ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

      locals {
        env = {
          dev = {
            type = "t3.micro"
            name = "dev_ec2"
          }
          prod = {
            type = "m5.large"
            name = "prod_ec2"
          }
        }
      }
    
      module "ec2_seoul" {
        for_each      = local.env
        source        = "../modules/terraform-aws-ec2"
        instance_type = each.value.type
        instance_name = each.value.name
      }

6-4. ๋ชจ๋“ˆ ์†Œ์Šค ๊ด€๋ฆฌ


  • module ๋ธ”๋ก์—์„œ source๋กœ ๋ชจ๋“ˆ ์ฝ”๋“œ์˜ ์œ„์น˜๋ฅผ ์ •์˜

      # ๋กœ์ปฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” ๋ชจ๋“ˆ ์‚ฌ์šฉ
      module "local_directory_vpc" {
        source = "../modules/"
      }
    
      # ํ…Œ๋ผํผ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ์— ์žˆ๋Š” ๋ชจ๋“ˆ ์‚ฌ์šฉ
      module "terraform_registry_vpc" {
        source = "terraform-aws-modules/vpc/aws"
      }
    
      # github์— ์žˆ๋Š” ๋ชจ๋“ˆ ์‚ฌ์šฉ
      module "github_vpc" {
        source = "github.com/hyuckang/terraform-vpc"
      }
  • ๋ชจ๋“ˆ ์†Œ์Šค์˜ ์œ ํ˜•

    • ๋กœ์ปฌ ๋””๋ ‰ํ„ฐ๋ฆฌ, ํ…Œ๋ผํผ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ, ๊นƒํ—ˆ๋ธŒ, S3 ๋“ฑ
๋ฐ˜์‘ํ˜•