[실습] ③ Terraform을 활용한 VPC 생성하기

2024. 11. 27. 21:31Terraform/[인프런] 아키텍처와 함께하는 Terraform

1. Provider

Provider 설정
# vpc.tf
# AWS Provider 설정
provider "aws" {
  region = "ap-northeast-2"
}
terraform init
- terraform init 명령어를 통해 Terraform 작업 디렉토리를 초기화 한다.
- Initializing the backend...(state 파일 저장하고 관리하는 Backend 초기화)
- Initializing provider plugins...(Terraform이 사용할 Provider 플러그인 확인하고 다운로드)
- Lock 파일 생성(.terraform.lock.hcl)
  · Terraform이 설치한 Provider 버전 및 세부 정보 기록
  · 예기치 않은 업데이트로 인한 문제 방지
terraform plan
- terraform plan 명령어를 통해 실제 인프라 상태구성 파일 상태비교하여 일치하는지 확인한다.
- 실제 인프라 상태(terraform.tfstate 및 AWS에 존재하는 서비스)
- 구성 파일 상태(Terraform의 구성 파일(.tf)에 정의된 원하는 리소스 상태)

 

 

2. VPC 생성

VPC 생성
# vpc.tf
# VPC 생성
resource "aws_vpc" "vpc" {
    cidr_block = "10.0.0.0/16"

    tags = {
        Name = "my-vpc"
    }
}
terraform apply
- + create는 Terraform이 새로운 리소스를 생성할 것임을 나타낸다.
- (known after apply)는 리소스 생성이 완료된 후에만 값을 알 수 있다는 것을 의미한다.
- terraform apply 명령 실행 후 terraform.tfstate 파일 생성된다.
  · Terraform이 생성한 리소스의 현재 상태 정보를 저장한다.
  · state 파일은 Terraform이 자체 관리하므로 직접 수정하면 데이터 손상 발생할 수 있다. 상태를 수정해야 할 경우, terraform state 명령어를 사용한다.
* 결과
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_dns_hostnames                 = (known after apply)
      + enable_dns_support                   = true
      + enable_network_address_usage_metrics = (known after apply)
      + id                                   = (known after apply)
      + instance_tenancy                     = "default"
      + ipv6_association_id                  = (known after apply)
      + ipv6_cidr_block                      = (known after apply)
      + ipv6_cidr_block_network_border_group = (known after apply)
      + main_route_table_id                  = (known after apply)
      + owner_id                             = (known after apply)
      + tags                                 = {
          + "Name" = "my-vpc"
        }
      + tags_all                             = {
          + "Name" = "my-vpc"
        }
    }

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 2s [id=vpc-0d7a8723c202958c4]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
VPC 생성 확인

my-vpc 생성 확인

 

 

 

 

출처 ; 아키텍처와 함께하는 Terraform (테라폼)