Repository navigation
Expand file tree
/
Copy pathnetwork.tf
More file actions
177 lines (133 loc) · 5.02 KB
/
Copy pathnetwork.tf
File metadata and controls
177 lines (133 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
data "aws_availability_zones" "available" {
#checkov:skip=CKV_AWS_394:Only the first az_count zones are used
state = "available"
}
resource "aws_vpc" "this" {
cidr_block = var.advanced_settings.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = { Name = "${var.name_prefix}-vpc" }
}
# Remove all rules from the default security group.
resource "aws_default_security_group" "this" {
vpc_id = aws_vpc.this.id
tags = { Name = "${var.name_prefix}-default-sg" }
}
resource "aws_internet_gateway" "this" {
vpc_id = aws_vpc.this.id
tags = { Name = "${var.name_prefix}-igw" }
}
# Public means a route to the internet gateway. Instances do not get a public IP automatically.
resource "aws_subnet" "public" {
count = local.az_count
vpc_id = aws_vpc.this.id
cidr_block = local.public_cidrs[count.index]
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = false
tags = { Name = "${var.name_prefix}-public-${count.index}" }
}
resource "aws_subnet" "private" {
count = local.az_count
vpc_id = aws_vpc.this.id
cidr_block = local.private_cidrs[count.index]
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = false
tags = { Name = "${var.name_prefix}-private-${count.index}" }
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.this.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.this.id
}
tags = { Name = "${var.name_prefix}-public" }
}
resource "aws_route_table_association" "public" {
count = local.az_count
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
# One route table per private subnet, so each can point to the NAT gateway of its own AZ.
resource "aws_route_table" "private" {
count = local.az_count
vpc_id = aws_vpc.this.id
tags = { Name = "${var.name_prefix}-private-${count.index}" }
}
resource "aws_route_table_association" "private" {
count = local.az_count
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private[count.index].id
}
# Optional NAT gateway: private subnets reach the internet, nothing reaches them.
resource "aws_eip" "nat" {
count = local.nat_count
domain = "vpc"
tags = { Name = "${var.name_prefix}-nat-${count.index}" }
depends_on = [aws_internet_gateway.this]
}
resource "aws_nat_gateway" "this" {
count = local.nat_count
allocation_id = aws_eip.nat[count.index].id
subnet_id = aws_subnet.public[count.index].id
tags = { Name = "${var.name_prefix}-nat-${count.index}" }
depends_on = [aws_internet_gateway.this]
}
resource "aws_route" "private_nat" {
count = var.advanced_settings.enable_nat_gateway ? local.az_count : 0
route_table_id = aws_route_table.private[count.index].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.this[var.advanced_settings.nat_gateway_per_az ? count.index : 0].id
}
# Free gateway endpoint: S3 traffic from the private subnets stays on the AWS network.
resource "aws_vpc_endpoint" "s3" {
vpc_id = aws_vpc.this.id
service_name = "com.amazonaws.${var.region}.s3"
vpc_endpoint_type = "Gateway"
route_table_ids = aws_route_table.private[*].id
tags = { Name = "${var.name_prefix}-s3" }
}
# VPC flow logs
#trivy:ignore:AVD-AWS-0017 Encrypted with AWS-managed keys, customer-managed KMS is out of scope for the demo
resource "aws_cloudwatch_log_group" "flow_logs" {
#checkov:skip=CKV_AWS_338:Demo template, retention is configurable via advanced_settings.flow_log_retention_days
#checkov:skip=CKV_AWS_158:Encrypted with AWS-managed keys, customer-managed KMS is out of scope for the demo
count = var.advanced_settings.enable_flow_logs ? 1 : 0
name = "/${var.name_prefix}/vpc-flow-logs"
retention_in_days = var.advanced_settings.flow_log_retention_days
}
resource "aws_iam_role" "flow_logs" {
count = var.advanced_settings.enable_flow_logs ? 1 : 0
name = "${var.name_prefix}-vpc-flow-logs"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Service = "vpc-flow-logs.amazonaws.com" }
Action = "sts:AssumeRole"
}]
})
}
resource "aws_iam_role_policy" "flow_logs" {
count = var.advanced_settings.enable_flow_logs ? 1 : 0
name = "write-flow-logs"
role = aws_iam_role.flow_logs[0].id
policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Action = [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams",
]
Resource = "${aws_cloudwatch_log_group.flow_logs[0].arn}:*"
}]
})
}
resource "aws_flow_log" "this" {
count = var.advanced_settings.enable_flow_logs ? 1 : 0
vpc_id = aws_vpc.this.id
traffic_type = "ALL"
log_destination = aws_cloudwatch_log_group.flow_logs[0].arn
iam_role_arn = aws_iam_role.flow_logs[0].arn
}