Cloud Infra Architecture (AWS)/AWS 자동배포

terraform 기본 인프라 생성하기 (vpc, igw, nat, subnet, routing table)

seongduck 2024. 6. 24. 23:38
vi vpc.tf #생성하고 나서 아래 작성
#vpc main
resource "aws_vpc" "main" {
    cidr_block = "172.31.190.0/24"

    tags = {
        Name = "amcamp"
    }

}


#create the internet gateway
resource "aws_internet_gateway" "internet-gateway" {
    vpc_id = aws_vpc.main.id

    tags = {
        Name = "igw-amcamp"
    }
}

#create the nat gateway
resource "aws_eip" "nat-gateway-eip" {
    domain = "vpc"

    tags = {
        Name = "nat-gateway-eip"
    }
}

resource "aws_nat_gateway" "nat-gateway" {
    allocation_id = aws_eip.nat-gateway-eip.id
    subnet_id = aws_subnet.sbn-amcamp-public-resource-az2a.id

    tags = {
        Name = "nat-gateway"
    }
}




#public subnet az#a
resource "aws_subnet" "sbn-amcamp-public-resource-az2a" {
    vpc_id = aws_vpc.main.id
    cidr_block = "172.31.190.0/27"
    availability_zone = "ap-northeast-2a"

    tags = {
        Name = "sbn-amcamp-public-resource-az2a"
    }
}

#create the route table for the public subnets
resource "aws_route_table" "rt-amcamp-public-resource-az2a" {
    vpc_id = aws_vpc.main.id

    route {
        cidr_block = "0.0.0.0/0"
        gateway_id = aws_internet_gateway.internet-gateway.id
    }

    tags = {
        Name = "rt-amcamp-public-resource-az2a"
    }
}

#create the route table association for the public subnets
resource "aws_route_table_association" "rt-public-resource-az2a-assoc" {
    subnet_id = aws_subnet.sbn-amcamp-public-resource-az2a.id
    route_table_id = aws_route_table.rt-amcamp-public-resource-az2a.id
}




#public subnet az#c
resource "aws_subnet" "sbn-amcamp-public-resource-az2c" {
    vpc_id = aws_vpc.main.id
    cidr_block = "172.31.190.32/27"
    availability_zone = "ap-northeast-2c"

    tags = {
        Name = "sbn-amcamp-public-resource-az2c"
    }
}

#create the route table for the public subnets
resource "aws_route_table" "rt-amcamp-public-resource-az2c" {
    vpc_id = aws_vpc.main.id

    route {
        cidr_block = "0.0.0.0/0"
        gateway_id = aws_internet_gateway.internet-gateway.id
    }

    tags = {
        Name = "rt-amcamp-public-resource-az2c"
    }
}

#create the route table association for the public subnets
resource "aws_route_table_association" "rt-public-resource-az2c-assoc" {
    subnet_id = aws_subnet.sbn-amcamp-public-resource-az2c.id
    route_table_id = aws_route_table.rt-amcamp-public-resource-az2c.id
}




#private subnet az#a
resource "aws_subnet" "sbn-amcamp-private-resource-az2a" {
    vpc_id = aws_vpc.main.id
    cidr_block = "172.31.190.64/26"
    availability_zone = "ap-northeast-2a"

    tags = {
        Name = "sbn-amcamp-private-resource-az2a"
    }
}

#create the route table for the private subnets
resource "aws_route_table" "rt-amcamp-private-resource-az2a" {
    vpc_id = aws_vpc.main.id

    route {
        cidr_block = "0.0.0.0/0"
        nat_gateway_id = aws_nat_gateway.nat-gateway.id
    }

    tags = {
        Name = "rt-amcamp-private-resource-az2a"
    }
}

#create the route table association for the private subnets
resource "aws_route_table_association" "rt-private-resource-az2a-assoc" {
    subnet_id = aws_subnet.sbn-amcamp-private-resource-az2a.id
    route_table_id = aws_route_table.rt-amcamp-private-resource-az2a.id
}




#private subnet az#c
resource "aws_subnet" "sbn-amcamp-private-resource-az2c" {
    vpc_id = aws_vpc.main.id
    cidr_block = "172.31.190.128/26"
    availability_zone = "ap-northeast-2c"

    tags = {
        Name = "sbn-amcamp-private-resource-az2c"
    }
}

#create the route table for the private subnets
resource "aws_route_table" "rt-amcamp-private-resource-az2c" {
    vpc_id = aws_vpc.main.id

    route {
        cidr_block = "0.0.0.0/0"
        nat_gateway_id = aws_nat_gateway.nat-gateway.id
    }

    tags = {
        Name = "rt-amcamp-private-resource-az2c"
    }
}

#create the route table association for the private subnets
resource "aws_route_table_association" "rt-private-resource-az2c-assoc" {
    subnet_id = aws_subnet.sbn-amcamp-private-resource-az2c.id
    route_table_id = aws_route_table.rt-amcamp-private-resource-az2c.id
}



#private subnet db az#a
resource "aws_subnet" "sbn-amcamp-private-db-az2a" {
    vpc_id = aws_vpc.main.id
    cidr_block = "172.31.190.192/28"
    availability_zone = "ap-northeast-2a"

    tags = {
        Name = "sbn-amcamp-private-db-az2a"
    }
}

#create the route table for the private subnets
resource "aws_route_table" "rt-amcamp-private-db-az2a" {
    vpc_id = aws_vpc.main.id

    tags = {
        Name = "rt-amcamp-private-db-az2a"
    }
}

#create the route table association for the private subnets
resource "aws_route_table_association" "rt-private-db-az2a-assoc" {
    subnet_id = aws_subnet.sbn-amcamp-private-db-az2a.id
    route_table_id = aws_route_table.rt-amcamp-private-db-az2a.id
}



#private subnet db az#c
resource "aws_subnet" "sbn-amcamp-private-db-az2c" {
    vpc_id = aws_vpc.main.id
    cidr_block = "172.31.190.208/28"
    availability_zone = "ap-northeast-2c"

    tags = {
        Name = "sbn-amcamp-private-db-az2c"
    }
}

#create the route table for the private subnets
resource "aws_route_table" "rt-amcamp-private-db-az2c" {
    vpc_id = aws_vpc.main.id

    tags = {
        Name = "rt-amcamp-private-db-az2c"
    }
}

#create the route table association for the private subnets
resource "aws_route_table_association" "rt-private-db-az2c-assoc" {
    subnet_id = aws_subnet.sbn-amcamp-private-db-az2c.id
    route_table_id = aws_route_table.rt-amcamp-private-db-az2c.id
}