Terraform을 사용하여 AWS에서 Virtual Private Cloud (VPC) 생성

 최근 각광 받고 있는 Terraform을 이용하여 AWS내에서 VPC 만들기!! 이 terraform을 스터디하게 된 계기는 현재 회사에서 보안, 인프라업무를 혼자 수행하여야 하는데 이런 구성에 대하여 간단하게 만들고자 작성하였습니다.

 

먼저 시작하기 전에 VPC를 생성하는데 필요한 모든 제반을 구성할 예정입니다.

 

서브넷, 인터넷 게이트웨이, NAT 게이트웨이 및 라우팅 테이블과 함께 VPC를 생성 하도록 할 것이며, 저는 4 개의 서브넷을 만들것이며 퍼블릭 서브넷2개, 프라이빗 서브넷 2개, NAT게이트웨이 1개, 인터넷 게이트웨이 및 4개의 라우팅 테이블로 1개의 VPC를 만들 도록 하겠습니다.

 

전제조건

  1. Terraform의 기본 이해.
  2. 시스템에 설치된 Terraform.
  3. AWS 계정
  4. AWS IAM 사용자의 'access_key'및 'secret_key'

다른 종속 리소스로 VPC를 생성하기 위한 Terraform 구성 파일을 작성하여 주세요. 저는 윈도우10 로컬에 terraform을 작성하여 구성하도록 하겠습니다. terraform 구성 파일을 작성할 수있는 전용 디렉토리를 작성하십시오. 다음 명령을 사용하여 디렉토리를 작성하고 현재 작업 디렉토리를 변경하십시오.

 

- mkdir terraform
- cd terraform/

 

파일에 쓰기 위해 "notepad"을 편집기로 사용하고 있습니다. 선택한 편집기를 사용하여 다음 구성을 복사하여 붙여 넣기하여 variables.tf, terraform.tfvars 및 main.tf를 작성할 수 있습니다. 종속 리소스를 사용하여 AWS에 VPC를 생성하는 'main.tf'를 생성하십시오. 이 main.tf는 variables.tf 및 terraform.tfvars에서 변수 값을 읽습니다.

 

-notepad main.tf

 

1. main.tf 내에 아래 내용을 작성하여주세요.

 

provider "aws" {

        region = "${var.region}"

        access_key = "${var.access_key}"

        secret_key = "${var.secret_key}"

}

# VPC resources: This will create 1 VPC with 4 Subnets, 1 Internet Gateway, 4 Route Tables.

resource "aws_vpc" "default" {

  cidr_block = var.cidr_block

  enable_dns_support = true

  enable_dns_hostnames = true

}

 

resource "aws_internet_gateway" "default" {

  vpc_id = aws_vpc.default.id

}

 

resource "aws_route_table" "private" {

  count = length(var.private_subnet_cidr_blocks)

  vpc_id = aws_vpc.default.id

}

 

resource "aws_route" "private" {

  count = length(var.private_subnet_cidr_blocks)

 

  route_table_id = aws_route_table.private[count.index].id

  destination_cidr_block = "0.0.0.0/0"

  nat_gateway_id = aws_nat_gateway.default[count.index].id

}

 

resource "aws_route_table" "public" {

vpc_id = aws_vpc.default.id

}

 

resource "aws_route" "public" {

  route_table_id = aws_route_table.public.id

  destination_cidr_block = "0.0.0.0/0"

  gateway_id = aws_internet_gateway.default.id

}

 

resource "aws_subnet" "private" {
  count = length(var.private_subnet_cidr_blocks)

  vpc_id = aws_vpc.default.id

  cidr_block = var.private_subnet_cidr_blocks[count.index]

  availability_zone = var.availability_zones[count.index]

}

 

resource "aws_subnet" "public" {

  count = length(var.public_subnet_cidr_blocks)

  vpc_id = aws_vpc.default.id

  cidr_block = var.public_subnet_cidr_blocks[count.index]

  availability_zone = var.availability_zones[count.index]

  map_public_ip_on_launch = true

}

 

resource "aws_route_table_association" "private" {

  count = length(var.private_subnet_cidr_blocks)

  subnet_id = aws_subnet.private[count.index].id

  route_table_id = aws_route_table.private[count.index].id

}

 

resource "aws_route_table_association" "public" {

  count = length(var.public_subnet_cidr_blocks)

  subnet_id = aws_subnet.public[count.index].id

  route_table_id = aws_route_table.public.id

}

# NAT resources: This will create 2 NAT gateways in 2 Public Subnets for 2 different Private Subnets.

resource "aws_eip" "nat" {

  count = length(var.public_subnet_cidr_blocks)

  vpc = true

}

 

resource "aws_nat_gateway" "default" {

  depends_on = ["aws_internet_gateway.default"]

 

  count = length(var.public_subnet_cidr_blocks)

 

  allocation_id = aws_eip.nat[count.index].id

  subnet_id = aws_subnet.public[count.index].id

}

 

2. 변수의 선언 및 정의를 포함하는 'variables.tf'를 작성하십시오.

- notepad variables.tf

variable "access_key" {
     description = "Access key to AWS console"
     
}
variable "secret_key" {
     description = "Secret key to AWS console"
     
}

variable "region" {
  default     = "ap-northeast-1"
  type        = string
  description = "Region of the VPC"
}


variable "cidr_block" {
  default     = "10.70.0.0/16"
  type        = string
  description = "CIDR block for the VPC"
}

variable "public_subnet_cidr_blocks" {
  default     = ["10.70.0.0/24", "10.70.2.0/24"]
  type        = list
  description = "List of public subnet CIDR blocks"
}

variable "private_subnet_cidr_blocks" {
  default     = ["10.70.1.0/24", "10.70.3.0/24"]
  type        = list
  description = "List of private subnet CIDR blocks"
}

variable "availability_zones" {
  default     = ["ap-northeast-1a", "ap-northeast-1c"]
  type        = list
  description = "List of availability zones"
}

위 파일에 정의 된 access_key 및 secret_key 변수의 정의를 포함하는 'terraform.tfvars'를 작성하십시오. 우리는이 두 변수의 선언을 'terraform.tfvars'파일에 보관했습니다.

 

3. 다음 키는 IAM 사용자의 키로 변경해야합니다.

 

- notepad terraform.tfvars 아래 내용을 첨부하여 줍니다.

 

이제 variables.tf, terraform.tfvars 및 main.tf 파일이 3개를 만들었습니다.

사용되는 첫 번째 명령은 'terraform init'입니다. 이 명령은 구성 내에서 사용되는 제공자를위한 플러그인을 다운로드하고 설치하는 역할을 합니다.

- terraform init

두 번째로 사용되는 명령은 'terraform plan'입니다. 이 명령은 인프라에서 발생할 변경 사항을 확인하는 데 사용됩니다.

- terraform plan

 

'terraform apply'명령은 main.tf 파일에 언급 된 AWS의 리소스를 생성합니다. 리소스를 만들려면 입력을 제공하라는 메시지가 표시됩니다.

- terraform apply

 

위 명령을 실행하면 20 개의 새로운 리소스가 추가되었고 출력에서 ​​0이 제거되었음을 알 수 있습니다.

AWS VPC 콘솔로 이동하여 VPC가 서브넷, 라우팅 테이블 NAT 게이트웨이 및 인터넷 게이트웨이와 함께 생성되는지 확인할 수 있습니다.

[VPC 생성완료]

 

 

Terraform을 사용하여 생성 된 VPC 및 기타 종속 리소스 삭제

main.tf 파일에 언급 된 구성을 사용하여 작성한 자원이 더 이상 필요하지 않은 경우 "terraform destroy"명령을 사용하여 해당 자원을 모두 삭제할 수 있습니다.

- terraform destroy

 

위 명령을 실행하면 생성 된 20 개의 리소스가 출력에서 ​​파괴 된 것을 볼 수 있습니다. VPC 대시 보드에서 AWS를 방문하여 동일한 내용을 확인할 수 있습니다.

 

이상으로 terraform 을 이용한 vpc만들기를 진행하였습니다. 감사합니다.

댓글

이 블로그의 인기 게시물

AWS 용어

AML 체계란 무엇인가?

Cisco 명령어 정리