Terraform 기반 IaC 실습 - week1

로로·2024년 2월 11일
0

조건

  • VPC 1개
  • Availability Zone 2a, 2c에 위치한 Public Subnet 2개
  • Availability Zone 2a, 2c에 위치한 Private Subnet 2개
  • 인터넷 접속을 위한 Internet Gateway 1개
  • 각 AZ에 위치한 Nat Gateway 2개
  • 각 Subnet에 연계(association)할 route table 4개

  1. VPC
    논리적으로 격리된 가상 네트워크 구성의 단위
    ✅ CIDR 대역 지정 : 10.10.0.0/16
  1. Availability Zone
    AWS 리전 내에 물리적으로 분리된 데이터 센터를 기반으로 한 가용 영역
    ap-northeast-2, ap-northeast-c

  2. Public subnet
    외부(internet)로의 연결이 설정된 서브넷
    ✅ ap-northeast-2a 용 public subnet CIDR : 10.10.1.0/24
    ✅ ap-northeast-2c 용 public subnet CIDR : 10.10.2.0/24

  3. Private subnet
    외부로의 연결이 설정되어 있지 않은 서브넷
    ✅ ap-northeast-2a 용 private subnet CIDR : 10.10.3.0/24
    ✅ ap-northeast-2c 용 private subnet CIDR : 10.10.4.0/24

  4. route table
    라우팅 룰이 표 형태로 정리된 테이블, 서브넷에 연계(association)되면 해당 서브넷은 테이블에 정의된 룰 셋에 따라 네트워크 통신 구축
    ✅ 라우팅 테이블은 4개를 생성 하고 각 서브넷마다 연계(association)

  5. Internet gateway
    외부 internet 과의 연결 접점
    Public subnet 에서 0.0.0.0/0 라우팅 의 연결 대상

  6. Nat Gateway
    공인 IP가 없는 리소스의 인터넷 연결을 위한 NAT 서비스
    공인 IP(EIP) 를 생성하여 할당


전체 코드

# 0. Aws provider  plugin 지정
provider "aws" {
# 서울 리전 사용 
  region  = "ap-northeast-2"
}

##### 1. aws 네트워크의 가장 기본이 되는 vpc생성 #####
resource "aws_vpc" "vpc-sch-1" {
  cidr_block       = "10.10.0.0/16"
#태그로 vpc이름 지정
  tags = {
    Name = "vpc-sch-1"
  }
}

##### 3. vpc에 속하게 되는 public subnet 생성 #####
resource "aws_subnet" "sbn-pub-sch-a" {
  vpc_id     = aws_vpc.vpc-sch-1.id
  cidr_block = "10.10.1.0/24"
  availability_zone = "ap-northeast-2a"
  tags = {
    Name = "sbn-pub-sch-a"                                    
  }
}
resource "aws_subnet" "sbn-pub-sch-c" {
  vpc_id     = aws_vpc.vpc-sch-1.id
  cidr_block = "10.10.2.0/24"
  availability_zone = "ap-northeast-2c"
  tags = {
    Name = "sbn-pub-sch-c"                                    
  }
}

##### 4. Private subnet 생성 ######
resource "aws_subnet" "sbn-pri-sch-a" {
  vpc_id     = aws_vpc.vpc-sch-1.id
  cidr_block = "10.10.3.0/24"
  availability_zone = "ap-northeast-2a"
  tags = {
    Name = "sbn-pri-sch-a"
  }
}
resource "aws_subnet" "sbn-pri-sch-c" {
  vpc_id     = aws_vpc.vpc-sch-1.id
  cidr_block = "10.10.4.0/24"
  availability_zone = "ap-northeast-2c"
  tags = {
    Name = "sbn-pri-sch-c"
  }
}

###### 5-1. Public Route table 생성 ######
resource "aws_route_table" "rt-pub-sch-a" {
  vpc_id = aws_vpc.vpc-sch-1.id
  tags = {
    Name = "rt-pub-sch-a"
  }
}
resource "aws_route_table" "rt-pub-sch-c" {
  vpc_id = aws_vpc.vpc-sch-1.id
  tags = {
    Name = "rt-pub-sch-c"
  }
}

##### 5-2. Public Routing 생성 #####
resource "aws_route_table_association" "rta-pub-sch-a"
  subnet_id      = aws_subnet.sbn-pub-sch-a.id
  route_table_id = aws_route_table.rt-pub-sch-a.id
}

resource "aws_route_table_association" "rta-pub-sch-c"
  subnet_id      = aws_subnet.sbn-pub-sch-c.id
  route_table_id = aws_route_table.rt-pub-sch-c.id
}

####### 5-3. Private Route Table 생성 ##### 
resource "aws_route_table" "rt-pri-sch-a" {
  vpc_id = aws_vpc.vpc-sch-1.id

  tags = {
    Name = "rt-pri-sch-a"
  }
}
resource "aws_route_table" "rt-pri-sch-c" {
  vpc_id = aws_vpc.vpc-sch-1.id

  tags = {
    Name = "rt-pri-sch-c"
  }
}

###### 5-4. Private Routing  연결 생성 #####
resource "aws_route_table_association" "rta-pri-sch-a" {
  subnet_id      = aws_subnet.sbn-pri-sch-a.id
  route_table_id = aws_route_table.rt-pri-sch-a.id
}
resource "aws_route_table_association" "rta-pri-sch-c" {
  subnet_id      = aws_subnet.sbn-pri-sch-c.id
  route_table_id = aws_route_table..rt-pri-sch-c.id
}


###### 6-1. Internet gateway ######
#resource igw(internet gateway) 생성
resource "aws_internet_gateway" "igw-sch-a" {
  vpc_id = aws_vpc.vpc-sch-1.id
  tags = {
    Name = "igw-sch-a"
  }
}

###### 6-2. Route for Internet Gateway ######
resource "aws_route" "rt-pub-sch-a" {
  route_table_id              = aws_route_table.rtb-pub-sch-a.id
  destination_cidr_block      = "0.0.0.0/0"
  gateway_id              = aws_internet_gateway.igw-sch.id
}

resource "aws_route" "rt-pub-sch-c" {
  route_table_id              = aws_route_table.rtb-pub-sch-c.id
  destination_cidr_block      = "0.0.0.0/0"
  gateway_id              = aws_internet_gateway.igw-sch.id
}


##### 7-1. Nat Gateway  생성 #####
resource "aws_eip" "eip-sch-a" {
  domain   = "vpc"
  lifecycle {
    create_before_destroy = true
  }
  tags = {
    Name = "eip-sch-a"
  }

}
resource "aws_eip" "eip-sch-c" {
  domain   = "vpc"
  lifecycle {
    create_before_destroy = true
  }
  tags = {
    Name = "eip-sch-c"
  }

}

###### 7-2. nat gateway 의 연결 생성 ######
resource "aws_nat_gateway" "nat-sch-a" {
  allocation_id = aws_eip.eip-sch-a.id
  subnet_id = aws_subnet.sbn-pub-sch-a.id
  tags = {
    Name = "nat-sch-a"
  }
}
resource "aws_nat_gateway" "nat-sch-c" {
  allocation_id = aws_eip.eip-sch-c.id
  subnet_id = aws_subnet.sbn-pub-sch-c.id
  tags = {
    Name = "nat-sch-c"
  }
}

# 7-3. Private Routing Table 생성
resource "aws_route" "r-pri-sch-a" {
  route_table_id              = aws_route_table.rt-pri-sch-a.id
  destination_cidr_block      = "0.0.0.0/0"
  nat_gateway_id              = aws_nat_gateway.nat-sch-a.id
}
resource "aws_route" "r-pri-sch-c" {
  route_table_id              = aws_route_table.rt-pri-sch-c.id
  destination_cidr_block      = "0.0.0.0/0"
  nat_gateway_id              = aws_nat_gateway.nat-sch-c.id
}
profile
청로하~🏝️

0개의 댓글

관련 채용 정보