스크린샷 2023-11-05 오전 11.04.40.png

VPC 완성

VPC 완성


VPC 구성하기

구성 요소

# vpc 만들기
resource "aws_vpc" "my_vpc" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "test-vpc"
  }
}
# igw 만들기
resource "aws_internet_gateway" "my_igw" {
  vpc_id = aws_vpc.my_vpc.id

  tags = {
    Name = "test-igw"
  }
}
# 각각 public, private subnet 2개씩 만든다.
resource "aws_subnet" "public_my_subnet_1" {
  vpc_id            = aws_vpc.my_vpc.id # 어느 vpc 안에 구성되는지
  cidr_block        = "10.0.1.0/24" # 네트워크 범위
  availability_zone = "${local.region}a" # 어느 가용영역에 구성되는지

  tags = {
    Name = "public-test-subnet-1" # 이름
  }
}

...(이하 구성 동일, 값은 다름)...
# route table public, private 각각 만들어준다.
resource "aws_route_table" "my_route_table_1" {
  vpc_id = aws_vpc.my_vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.my_igw.id # 어느 igw와 연결하는지
  }

  tags = {
    Name = "public-route-table-1"
  }
}

...(이하 구성 동일, 값은 다름)...
# subnet과 route table들들을 이어준다.
resource "aws_route_table_association" "public_route_table_association_1" {
  subnet_id      = aws_subnet.public_my_subnet_1.id
  route_table_id = aws_route_table.my_route_table_1.id
}

resource "aws_route_table_association" "public_route_table_association_2" {
  subnet_id      = aws_subnet.public_my_subnet_2.id
  route_table_id = aws_route_table.my_route_table_1.id
}

resource "aws_route_table_association" "private_route_table_association_1" {
  subnet_id      = aws_subnet.private_my_subnet_1.id
  route_table_id = aws_route_table.my_route_table_2.id
}

resource "aws_route_table_association" "private_route_table_association_2" {
  subnet_id      = aws_subnet.private_my_subnet_2.id
  route_table_id = aws_route_table.my_route_table_2.id
}

참고 글