๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

๐Ÿ’ป Lab - Provisioners, Variables, Outputs

1๋ถ„ ๋ฏธ๋งŒncloudncpterraformworkshop

๐Ÿ’ป Lab - Provisioners, Variables, Outputs

ํŽธ์ง‘๊ธฐ์—์„œ ์—ด๊ธฐ

  • VSCode๋ฅผ ์‹คํ–‰ํ•˜๊ณ  File(ํŒŒ์ผ) ๋ฉ”๋‰ด์—์„œ Open Folder... ๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.
  • ์•ž์„œ ์‹ค์Šต์„ ์ง„ํ–‰ํ•œ lab02์„ ์—ด์–ด์ค๋‹ˆ๋‹ค.

๐Ÿ› ๏ธ Use a Provisioner

๐Ÿ’ป Provisioner๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

main.tf ํŒŒ์ผ์„ ์—ด์–ด remote-exec ํ•ญ๋ชฉ์ด ์žˆ๋Š” ๊ณณ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

inline ํ•ญ๋ชฉ์— ๋‹ค์Œ์„ ๋‘์ค„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

"sudo apt -y install cowsay",
"cowsay Mooooooooooo!",

ํŒ

terraform fmt๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๋ฉ‹์ง€๊ฒŒ ์ •๋ ฌ ํ•  ์ˆ˜ ์žˆ๋Š” ์ข‹์€ ์‹œ๊ฐ„ ์ž…๋‹ˆ๋‹ค.

์ด์ œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ ์šฉํ•˜์‹ญ์‹œ์˜ค.

terraform apply -auto-approve

๋กœ๊ทธ ์ถœ๋ ฅ์„ ๋’ค๋กœ ์Šคํฌ๋กคํ•ฉ๋‹ˆ๋‹ค. "Moooooooo!"๋ผ๊ณ  ๋งํ•˜๋Š” ASCII ์•„ํŠธ ์•”์†Œ๊ฐ€ ๋ณด์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Result example

๐Ÿ–จ๏ธ Add an Output

๐Ÿ’ป Output์„ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

output.tf ํŒŒ์ผ์„ ์—ด์–ด ์•„๋ž˜ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

output "ssh_info" {
  value = nonsensitive("sshpass -p '${data.ncloud_root_password.hashicat.root_password}' ssh root@${ncloud_public_ip.hashicat.public_ip} -oStrictHostKeyChecking=no")
}

ํ•ด๋‹น output์˜ ์ด๋ฆ„์€ ssh_info ์ž…๋‹ˆ๋‹ค.

์–ด๋–ค ์œ ํ˜•์˜ ์ถœ๋ ฅ์ด ์œ ํšจํ•œ์ง€ ๋ณด๋ ค๋ฉด ๋ฌธ์„œ ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

output.tf์— ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ์ €์žฅํ•˜๊ณ  terraform refresh ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ถœ๋ ฅ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

terraform refresh

terraform output ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ๋ชจ๋“  ์ถœ๋ ฅ์„ ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

terraform output

๐Ÿถ Fun With Variables

๐Ÿ’ป ๋ณ€์ˆ˜๋ฅผ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

Terraform ๋ณ€์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ terraform.tfvars ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ช…๋ น์ค„์—์„œ ๊ธฐ๋ณธ๊ฐ’๊ณผ ๋‹ค๋ฅธ height, width ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉ ํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋‹ค์‹œ ๋ฐฐํฌํ•ด ๋ณด์‹ญ์‹œ์˜ค.

๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๊ด€์ฐฐํ•˜๊ธฐ ์œ„ํ•ด ์ ์šฉํ•  ๋•Œ๋งˆ๋‹ค ์›น ์•ฑ์„ ๋‹ค์‹œ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.

terraform apply -auto-approve -var height=600 -var width=800

๋‹ค์Œ์œผ๋กœ Terraform์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด ๋ณด์‹ญ์‹œ์˜ค. ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ placeholder ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

export TF_VAR_placeholder=placedog.net

ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ ์šฉ ํ›„ terraform apply -auto-approve๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋‹ค์‹œ ์ ์šฉํ•ด ๋ด…๋‹ˆ๋‹ค.

terraform apply -auto-approve

์ด์ œ ๋ช…๋ น์ค„์—์„œ ๋™์ผํ•œ ๋ณ€์ˆ˜๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•˜์—ฌ ๋‹ค์‹œ ์‹œ๋„ํ•˜์‹ญ์‹œ์˜ค.

terraform apply -auto-approve -var placeholder=placebear.com

์–ด๋–ค ๋ณ€์ˆ˜๊ฐ€ ์šฐ์„ ์‹œ ๋˜์—ˆ์Šต๋‹ˆ๊นŒ? ์ž˜ ์ดํ•ด ๋˜์…จ๋‚˜์š”?

๋‹ค์Œ ๊ณต์‹๋ฌธ์„œopen in new window๋ฅผ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ˜ฑ Quiz Time 4. Terraform Variables

Q. *.tfvars ํŒŒ์ผ๊ณผ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ๋™์ผํ•œ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋Š ๊ฒƒ์ด ์šฐ์„ ํ•ฉ๋‹ˆ๊นŒ?

๋‹ต

์ด ์žฅ์—์„œ ์šฐ๋ฆฌ๋Š” :

  • Terraform Provisioners์— ๋Œ€ํ•ด ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค.
  • file ๊ณผ remote-exec ํ”„๋กœ๋น„์ €๋‹ ๋„๊ตฌ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.
  • ์ƒˆ๋กœ์šด ํ”„๋กœ๋น„์ €๋‹ ๋‹จ๊ณ„๋กœ ์›น์„œ๋ฒ„ ์žฌ๊ตฌ์„ฑ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.