[Terraform] Terraform ์ž…๋ฌธ - Cloud9์—์„œ ๊ฐ„๋‹จํ•œ ์‹ค์Šตํ•˜๊ธฐ

2023. 9. 17. 22:08ใ†ETC

0. Terraform์ด๋ž€?

  • Hashicorp์—์„œ ๊ฐœ๋ฐœํ•œ ์ธํ”„๋ผ ๊ด€๋ฆฌ ๋„๊ตฌ.
  • HCL(Hashicorp Configuration Language)๋ฅผ ํ†ตํ•ด AWS๋ฟ ์•„๋‹ˆ๋ผ Azure, GCP, NCP ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ํด๋ผ์šฐ๋“œ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

1. Terraform ์žฅ์ 

IaC(Infrastructure as Code)์˜ ์žฅ์ 

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

1. ์„ ์–ธํ˜• ์–ธ์–ด

  • Terraform์€ ๊ตฌ์„ฑ๋  ์ธํ”„๋ผ์ŠคํŠธ๋Ÿญ์ฒ˜์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ์„ ์–ธ.
  • ๋ณ€๊ฒฝ ์ด๋ ฅ์„ ์•Œ ํ•„์š”๊ฐ€ ์—†๋‹ค.ex) EC2 10๋Œ€ ๊ตฌ์„ฑ ํ›„, 5๋Œ€๋ฅผ ์ถ”๊ฐ€๋กœ ๊ตฌ์„ฑํ•ด์•ผ ํ•  ๊ฒฝ์šฐ EC2 resource์˜ count ๊ฐœ์ˆ˜๋งŒ 15๋กœ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค.

2. Master/Agent ๊ตฌ์กฐ๊ฐ€ ์•„๋‹ˆ๋‹ค.

  • ๋ชจ๋“  ์„œ๋ฒ„์— agentํ˜•์‹์œผ๋กœ terraform์„ ์„ค์น˜ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • terraform์„ ์„ค์น˜ํ•˜์—ฌ ์ธํ”„๋ผ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์šฉ๋„์˜ ์„œ๋ฒ„๋งŒ ํ•˜๋‚˜ ์žˆ์œผ๋ฉด ๋œ๋‹ค.

 

2. Cloud9์—์„œ Terraform์œผ๋กœ VPC ํ”„๋กœ๋น„์ €๋‹ํ•˜๊ธฐ

  1. Terraform ์„ค์น˜
    • cloud9 ํ™˜๊ฒฝ์—๋Š” ์ด๋ฏธ Terraform์ด ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๋ณ„๋„ ์„ค์น˜๊ฐ€ ํ•„์š” ์—†๋‹ค.
  2. AWS Configure ์„ค์ •
    • Access Key, Secret Key ๋ฐ Region๊ณผ ๊ฐ™์€ aws credentials ์ €์žฅ
  3. Provider ์ง€์ •
    • Provider์˜ˆ๋ฅผ ๋“ค์–ด ํ…Œ๋ผํผ์œผ๋กœ AWS ์„œ๋น„์Šค์˜ ์ปดํ“จํŒ… ์ž์›์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” aws ํ”„๋กœ๋ฐ”์ด๋”๋ฅผ ๋จผ์ € ์…‹์—…ํ•ด์•ผ ํ•œ๋‹ค.
    • ํ…Œ๋ผํผ๊ณผ ์™ธ๋ถ€ ์„œ๋น„์Šค๋ฅผ ์—ฐ๊ฒฐํ•ด ์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋ชจ๋“ˆ.
    • Terraform์—์„œ ์ง€์›ํ•˜๋Š” provider

 

3.  ๊ธฐ๋ณธ Terraform ๋ช…๋ น์–ด

terraform init

  • terraform init ๋ช…๋ น์–ด๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ terraform์„ ๊ตฌ๋™ํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  • terraform ์ตœ์ดˆ ๊ตฌ๋™ ์‹œ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ๋ช…๋ น์–ด
# terraform init

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v3.71.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

terraform plan

  • terraform plan์€ .tfstate ํŒŒ์ผ๊ณผ ์‹ค์ œ ํด๋ผ์šฐ๋“œ์— ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ๋‚ด์—ญ์„ ๋น„๊ตํ•˜์—ฌ ํ˜„์žฌ ์ž‘์„ฑ๋œ terraform ์ฝ”๋“œ๋กœ ์ƒ์„ฑ๋˜๊ณ  ๋ณ€๊ฒฝ๋˜๋Š” ๋‚ด์—ญ์„ ๋ณด์—ฌ์ค€๋‹ค.
  • ์‹ค์ œ ํ™˜๊ฒฝ์— ์ ์šฉํ•˜๊ธฐ ์ „ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋งค์šฐ ์ค‘์š”ํ•œ ๋ช…๋ น์–ด.
  • terraform ์ฝ”๋“œ์˜ ๋ฌธ๋ฒ•์  ์˜ค๋ฅ˜๊ฐ€ ์—†๋Š”์ง€๋„ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋‹ค. (ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์ปดํŒŒ์ผ ๋‹จ๊ณ„)
# terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_vpc.vpc will be created
  + resource "aws_vpc" "vpc" {
      + arn                            = (known after apply)
      + cidr_block                     = "10.0.0.0/16"
      + default_network_acl_id         = (known after apply)
      + default_route_table_id         = (known after apply)
      + default_security_group_id      = (known after apply)
      + dhcp_options_id                = (known after apply)
      + enable_classiclink             = (known after apply)
      + enable_classiclink_dns_support = (known after apply)
      + enable_dns_hostnames           = (known after apply)
      + enable_dns_support             = true
      + id                             = (known after apply)
      + instance_tenancy               = "default"
      + ipv6_association_id            = (known after apply)
      + ipv6_cidr_block                = (known after apply)
      + main_route_table_id            = (known after apply)
      + owner_id                       = (known after apply)
      + tags                           = {
          + "Name" = "tf-vpc-hjlee"
        }
      + tags_all                       = {
          + "Name" = "tf-vpc-hjlee"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  **Enter a value: yes**

aws_vpc.vpc: Creating...
aws_vpc.vpc: Creation complete after 0s [id=vpc-0ca0a87d832909f9e]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

  • terraform apply ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด terraform.tfstate๋ผ๋Š” ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค.
  • terraform์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ธํ”„๋ผ์ŠคํŠธ๋Ÿญ์ฒ˜์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋น„๋ฐ€๋ฒˆํ˜ธ, ๊ณ„์ • ์•ก์„ธ์Šค ID ๋“ฑ์˜ ์ •๋ณด๋“ค์ด ํฌํ•จ๋œ๋‹ค.

terraform fmt

๊ฐ€๋…์„ฑ๊ณผ ์ผ๊ด€์„ฑ์„ ํ•˜๊ธฐ ์œ„ํ•ด ํ˜„์žฌ ๊ตฌ์„ฑํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ํฌ๋งทํŒ…

 

terraform validate

๊ตฌ๋ฌธ์ ์œผ๋กœ ์œ ํšจํ•˜๊ณ  ๋‚ด๋ถ€์ ์œผ๋กœ ์ผ๊ด€์„ฑ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.