deployment/terraform/aws/modules/network/main.tf (76 lines of code) (raw):
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
data "aws_availability_zones" "available" {
state = "available"
}
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
tags = var.tags
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = var.tags
}
resource "aws_nat_gateway" "main" {
count = 2
subnet_id = element(aws_subnet.public[*].id, count.index)
allocation_id = element(aws_eip.nat[*].id, count.index)
depends_on = [aws_internet_gateway.main]
tags = var.tags
}
resource "aws_eip" "nat" {
count = 2
domain = "vpc"
depends_on = [aws_internet_gateway.main]
tags = var.tags
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
tags = var.tags
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
}
resource "aws_route_table" "private" {
count = 2
vpc_id = aws_vpc.main.id
tags = var.tags
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = element(aws_nat_gateway.main[*].id, count.index)
}
}
resource "aws_route_table_association" "public" {
count = 2
subnet_id = element(aws_subnet.public[*].id, count.index)
route_table_id = aws_route_table.public.id
}
resource "aws_route_table_association" "private" {
count = 2
subnet_id = element(aws_subnet.private[*].id, count.index)
route_table_id = element(aws_route_table.private[*].id, count.index)
}
resource "aws_subnet" "public" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(var.public_cidr, 1, count.index)
availability_zone = element(data.aws_availability_zones.available.names, count.index)
map_public_ip_on_launch = true
tags = merge(
var.tags,
{
"Name" = "public-subnet-custos-${format("%02d", count.index + 1)}"
}
)
}
resource "aws_subnet" "private" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(var.private_cidr, 1, count.index)
availability_zone = element(data.aws_availability_zones.available.names, count.index)
tags = merge(
var.tags,
{
"Name" = "private-subnet-custos-${format("%02d", count.index + 1)}"
}
)
}