在前一页成功创建IAM资源后,我们现在可以继续创建托管我们应用程序所需的网络资源。
我们将创建一个新的VPC(Virtual Private Cloud),并拥有运行我们示例应用程序所需的所有必要资源,如下图所示。
打开terraform
文件夹中的main.tf
, 添加以下内容并保存文件
# VPC
resource "aws_vpc" "default" {
cidr_block = local.vpc.cidr_block
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "${var.namespace}-vpc"
}
}
resource "aws_subnet" "public" {
for_each = { for index, az_name in local.vpc.azs : index => az_name }
vpc_id = aws_vpc.default.id
cidr_block = cidrsubnet(aws_vpc.default.cidr_block, 8, (each.key + (length(local.vpc.azs) * 0)))
availability_zone = each.value
map_public_ip_on_launch = true
tags = {
Name = "${var.namespace}-subnet-public-${each.key}"
}
}
resource "aws_subnet" "private" {
for_each = { for index, az_name in local.vpc.azs : index => az_name }
vpc_id = aws_vpc.default.id
cidr_block = cidrsubnet(aws_vpc.default.cidr_block, 8, (each.key + (length(local.vpc.azs) * 1)))
availability_zone = each.value
tags = {
Name = "${var.namespace}-subnet-private-${each.key}"
}
}
resource "aws_subnet" "private_ingress" {
for_each = { for index, az_name in local.vpc.azs : index => az_name }
vpc_id = aws_vpc.default.id
cidr_block = cidrsubnet(aws_vpc.default.cidr_block, 8, (each.key + (length(local.vpc.azs) * 2)))
availability_zone = each.value
tags = {
Name = "${var.namespace}-subnet-private_ingress-${each.key}"
}
}
resource "aws_internet_gateway" "default" {
vpc_id = aws_vpc.default.id
tags = {
Name = "${var.namespace}-internet-gateway"
}
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.default.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.default.id
}
tags = {
Name = "${var.namespace}-route-table-public"
}
}
resource "aws_route_table" "private_ingress" {
count = length(aws_subnet.private_ingress)
vpc_id = aws_vpc.default.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.default[count.index].id
}
tags = {
Name = "${var.namespace}-route-table-private-ingress-${count.index}"
}
}
resource "aws_main_route_table_association" "default" {
vpc_id = aws_vpc.default.id
route_table_id = aws_route_table.public.id
}
resource "aws_route_table_association" "public" {
count = length(aws_subnet.public)
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
resource "aws_route_table_association" "private_ingress" {
count = length(aws_subnet.private_ingress)
subnet_id = aws_subnet.private_ingress[count.index].id
route_table_id = aws_route_table.private_ingress[count.index].id
}
resource "aws_eip" "nat_gateway" {
count = length(aws_subnet.public)
tags = {
Name = "${var.namespace}-private_ingress-nat-gateway-eip-${count.index}"
}
}
resource "aws_nat_gateway" "default" {
count = length(aws_subnet.public)
connectivity_type = "public"
subnet_id = aws_subnet.public[count.index].id
allocation_id = aws_eip.nat_gateway[count.index].id
depends_on = [aws_internet_gateway.default]
tags = {
Name = "${var.namespace}-private_ingress-nat-gateway-${count.index}"
}
}
在创建VPC网络组件,如子网、互联网网关、路由表和NAT网关。
运行命令terraform validate
来验证语法
运行命令terraform plan
运行命令terraform apply
来应用部署
导航到AWS控制台,搜索VPC,并单击VPC进入VPC控制台
在左侧导航窗格中,单击以下每个VPC组件,并验证我们已创建与Terraform部署相对应的资源:
现在我们已经确认创建了网络资源,下一节来创建安全资源。