Wisdom’s Cloud

[AWS] 5. Terraform을 사용하여 VPC 리소스 생성 본문

AWS/Intermediate

[AWS] 5. Terraform을 사용하여 VPC 리소스 생성

지혜로운지혜쓰 2022. 2. 24. 11:05

Terraform

  • Terraform은 수백 개의 클라우드 서비스를 관리하기 위한 일관된 CLI 워크플로우를 제공하는 코드 소프트웨어 도구로서의 오픈 소스 인프라로, 클라우드 API를 선언적 구성 파일로 코드화합니다.
  • 선언적 구성 파일을 사용하여 인프라를 코드로 작성하므로, HashiCorp 구성 언어(HCL)를 사용하여 블록, 인수 및 표현식을 통해 리소스를 간결하게 설명할 수 있습니다.
  • 또한, 인프라를 프로비저닝하거나 변경하기 전에 "terraform plan"을 실행하여 구성에 대한 실행 계획이 예상과 일치하는지 확인할 수 있으며, 원하는 구성 상태에 도달하기 위해 "terraform apply"을 실행하여 수 백개의 클라우드 제공업체에 변경 사항을 적용할 수 있습니다.

 

Terraform AWS modules

https://github.com/terraform-aws-modules

 

Terraform AWS modules

Collection of Terraform AWS modules supported by the community - Terraform AWS modules

github.com

 

 

실습

구성 아키텍처

 

Terraform 설치 및 설정

https://www.terraform.io/downloads

 

Downloads | Terraform by HashiCorp

Terraform is an open-source infrastructure as code software tool that enables you to safely and predictably create, change, and improve infrastructure.

www.terraform.io

위 사이트에 접속하여 설치 파일을 다운로드 받은 후, 압축을 풀고 C:\terraform 폴더를 생성하여 terraform.exe 파일을 이동시킵니다.
제어판의 시스템 환경 변수 편집에서 환경 변수를 클릭하고, 시스템 변수의 Path에 C:\terraform을 추가합니다.
Visual Studio Code에서 코드를 작성하고 실행할 것이기 때문에 터미널에서 Terraform CLI가 잘 동작하는지 확인합니다.

 

VPC 리소스 구성

# aws-provider.tf
먼저 AWS 리소스를 정의하기 전에 AWS 프로바이더 정의가 필요합니다.
aws-provider.tf 파일에 AWS 프로바이더를 다음과 같이 정의하여, 사용할 계정의 ACCESS_KEY와 SECRET_KEY를 통해 서울 리전에 리소스를 정의할 수 있도록 작성합니다.
--------------------------------------------------------------------------------------------------

provider "aws" {
    access_key = "XXXXXXXXXXXXXXXXXXXX"
    secret_key = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
    region = "ap-northeast-2"}

# vpc.tf
다음으로 AWS VPC 리소스를 vpc.tf 파일에 정의합니다.
Terraform의 AWS Documentation을 참고하여 원하는 리소스들을 정의하면 됩니다.

--------------------------------------------------------------------------------------------------

# VPC 생성
resource "aws_vpc" "jihye-vpc" {
    cidr_block = "10.0.0.0/16"
    enable_dns_hostnames = true
    enable_dns_support = true
    tags = {
        Name = "jihye-vpc"
    }
}

# Public Subnet 생성
resource "aws_subnet" "jihye-public-1" {
    vpc_id = "${aws_vpc.jihye-vpc.id}"
    cidr_block = "10.0.1.0/24"
    availability_zone = "ap-northeast-2a"
    tags = {
        Name = "jihye-public-1"
    }
}
resource "aws_subnet" "jihye-public-2" {
    vpc_id = "${aws_vpc.jihye-vpc.id}"
    cidr_block = "10.0.2.0/24"
    availability_zone = "ap-northeast-2c"
    tags = {
        Name = "jihye-public-2"
    }
}

# Internet Gateway 생성
resource "aws_internet_gateway" "jihye-igw" {
    vpc_id = "${aws_vpc.jihye-vpc.id}"
    tags = {
        Name = "jihye-igw"
    }
}

# Public Subnet Routing Table 생성
resource "aws_route_table" "jihye-public-rt" {
    vpc_id = "${aws_vpc.jihye-vpc.id}"
    tags = {
        Name = "jihye-public-rt"
    }
}

# Public Subnet과 Public Subnet Routing Table 연결
resource "aws_route_table_association" "jihye-public-rta-1" {
    subnet_id = "${aws_subnet.jihye-public-1.id}"
    route_table_id = "${aws_route_table.jihye-public-rt.id}"
}
resource "aws_route_table_association" "jihye-public-rta-2" {
    subnet_id = "${aws_subnet.jihye-public-2.id}"
    route_table_id = "${aws_route_table.jihye-public-rt.id}"
}

# Public Subnet Routing Table에 경로 생성
resource "aws_route" "jihye-public-igw" {
    route_table_id = "${aws_route_table.jihye-public-rt.id}"
    destination_cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.jihye-igw.id}"
}

# Private Subnet 생성
resource "aws_subnet" "jihye-private-1" {
    vpc_id = "${aws_vpc.jihye-vpc.id}"
    cidr_block = "10.0.3.0/24"
    availability_zone = "ap-northeast-2a"
    tags = {
        Name = "jihye-private-1"
    }
}
resource "aws_subnet" "jihye-private-2" {
    vpc_id = "${aws_vpc.jihye-vpc.id}"
    cidr_block = "10.0.4.0/24"
    availability_zone = "ap-northeast-2c"
    tags = {
        Name = "jihye-private-2"
    }
}

# EIP 생성
resource "aws_eip" "jihye-eip-1" {
    vpc = true
    lifecycle {
        create_before_destroy = true
    }
    tags = {
        Name = "jihye-eip-1"
    }
}
resource "aws_eip" "jihye-eip-2" {
    vpc = true
    lifecycle {
        create_before_destroy = true
    }
    tags = {
        Name = "jihye-eip-2"
    }
}

# NAT Gateway 생성
resource "aws_nat_gateway" "jihye-ngw-1" {
    allocation_id = "${aws_eip.jihye-eip-1.id}"
    subnet_id = "${aws_subnet.jihye-public-1.id}"
    tags = {
        Name = "jihye-ngw-1"
    }
}
resource "aws_nat_gateway" "jihye-ngw-2" {
    allocation_id = "${aws_eip.jihye-eip-2.id}"
    subnet_id = "${aws_subnet.jihye-public-2.id}"
      tags = {
        Name = "jihye-ngw-2"
    }
}

# Private Subnet Routing Table 생성
resource "aws_route_table" "jihye-private-rt-1" {
    vpc_id = "${aws_vpc.jihye-vpc.id}"
    tags = {
        Name = "jihye-private-rt-1"
    }
}
resource "aws_route_table" "jihye-private-rt-2" {
    vpc_id = "${aws_vpc.jihye-vpc.id}"
    tags = {
        Name = "jihye-private-rt-2"
    }
}

# Public Subnet과 Public Subnet Routing Table 연결
resource "aws_route_table_association" "jihye-private-rta-1" {
    subnet_id = "${aws_subnet.jihye-private-1.id}"
    route_table_id = "${aws_route_table.jihye-private-rt-1.id}"
}
resource "aws_route_table_association" "jihye-private-rta-2" {
    subnet_id = "${aws_subnet.jihye-private-2.id}"
    route_table_id = "${aws_route_table.jihye-private-rt-2.id}"
}

# Public Subnet Routing Table에 경로 생성
resource "aws_route" "jihye-private-nat-1" {
    route_table_id = "${aws_route_table.jihye-private-rt-1.id}"
    destination_cidr_block = "0.0.0.0/0"
    nat_gateway_id = "${aws_nat_gateway.jihye-ngw-1.id}"
}
resource "aws_route" "jihye-private-nat-2" {
    route_table_id = "${aws_route_table.jihye-private-rt-2.id}"
    destination_cidr_block = "0.0.0.0/0"
    nat_gateway_id = "${aws_nat_gateway.jihye-ngw-2.id}"
}

 

확인

C:\tf_file\seoul-network 폴더 밑에 aws-provider.tf 파일과 vpc.tf 파일을 작성한 후, terraform plan을 통해 작성한 리소스들을 AWS에 생성할 수 있는지 확인합니다.
문제가 없다면 terraform apply를 통해 작성한 리소스들을 AWS에 적용합니다.
작성한 리소스들이 모두 생성된 것을 확인할 수 있습니다. 리소스 삭제는 terraform destroy를 통해 삭제하면 됩니다.