본문 바로가기

혼자하는 프로젝트/AWS_도커 구현

2.Terraform을 활용한 VPC 생성

728x90
반응형
provider "aws" {

region = "ap-northeast-2"

}

- provider.tf 파일을 생성하고 위의 스크립트를 작성하자.

 

resource "aws_vpc" "main" {
        cidr_block = "10.0.0.0/16"

        tags = {
        Name = "terraform-101"
        }
}

resource "aws_subnet" "public_subnet" {
        vpc_id = aws_vpc.main.id
        cidr_block = "10.0.0.0/24"

        availability_zone = "ap-northeast-2a"

        tags = {
        Name = "terraform-101-public-subnet"
        }

}

resource "aws_subnet" "private_subnet" {
        vpc_id = aws_vpc.main.id
        cidr_block = "10.0.10.0/24"

        tags = {
        Name = "terraform-101-private-subnet"
        }
}

resource "aws_internet_gateway" "igw" {
        vpc_id = aws_vpc.main.id

        tags = {
        Name = "terraform-101-igw"
        }
}

resource "aws_eip" "nat" {
        vpc = true

        lifecycle {
                create_before_destroy = true
        }
}

resource "aws_nat_gateway" "nat_gateway" {
        allocation_id = aws_eip.nat.id

        subnet_id = aws_subnet.public_subnet.id

        tags = {
                Name = "terraform-NATGW"
        }
}

resource "aws_route_table" "public" {
        vpc_id = aws_vpc.main.id

        route {
                cidr_block = "0.0.0.0/0"
                gateway_id = aws_internet_gateway.igw.id
        }

        tags = {
                Name = "terraform-101-rt-public"

        }
}

resource "aws_route_table_association" "route_table_association_public" {
        subnet_id = aws_subnet.public_subnet.id
        route_table_id = aws_route_table.public.id
}

resource "aws_route_table" "private" {
        vpc_id = aws_vpc.main.id

        tags = {
                Name = "terraform-101-rt-public"
        }

}

resource "aws_route_table_association" "route_table_association_private" {
        subnet_id = aws_subnet.private_subnet.id
        route_table_id = aws_route_table.private.id
}

resource "aws_route" "private_nat" {
        route_table_id = aws_route_table.private.id
        destination_cidr_block = "0.0.0.0/0"
        nat_gateway_id = aws_nat_gateway.nat_gateway.id
}

- vpc.tf 파일을 생성하고 위의 스크립트를 작성한다.

728x90
반응형