-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc.tf
executable file
·231 lines (185 loc) · 5.04 KB
/
vpc.tf
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#####
# VPC configuration for EKS
#####
# lock down default security group
resource "aws_default_security_group" "default" {
vpc_id = module.vpc.vpc_id
tags = merge(
var.tags,
{
Name = "${local.name_prefix}-default-sg"
}
)
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.1.0"
name = "${local.name_prefix}-vpc"
azs = var.availability_zones
cidr = var.vpc_cidr
private_subnets = var.private_subnets_cidrs
public_subnets = var.public_subnets_cidrs
enable_dns_hostnames = true
enable_dns_support = true
enable_nat_gateway = true
enable_vpn_gateway = false
single_nat_gateway = var.vpc_single_nat_gateway
one_nat_gateway_per_az = var.vpc_one_nat_gateway_per_az
private_subnet_tags = {
"kubernetes.io/role/internal-elb" = "1",
"mapPublicIpOnLaunch" = "FALSE"
}
public_subnet_tags = {
"kubernetes.io/role/elb" = "1",
"mapPublicIpOnLaunch" = "TRUE"
}
tags = merge(
var.tags,
{
"kubernetes.io/cluster/${local.name_prefix}" = "shared"
},
)
}
#####
# VPC endpoints values
#####
data "aws_vpc_endpoint_service" "s3" {
service_type = "Interface"
filter {
name = "service-name"
values = ["*us-east-1.s3*"]
}
}
resource "aws_vpc_endpoint" "s3" {
vpc_id = module.vpc.vpc_id
service_name = data.aws_vpc_endpoint_service.s3.service_name
vpc_endpoint_type = "Interface"
security_group_ids = [aws_security_group.vpc_endpoint.id]
subnet_ids = module.vpc.private_subnets
private_dns_enabled = false
tags = var.tags
}
data "aws_vpc_endpoint_service" "ecr_dkr" {
service_type = "Interface"
filter {
name = "service-name"
values = ["*ecr.dkr*"]
}
}
resource "aws_vpc_endpoint" "ecr_dkr" {
vpc_id = module.vpc.vpc_id
service_name = data.aws_vpc_endpoint_service.ecr_dkr.service_name
vpc_endpoint_type = "Interface"
security_group_ids = [aws_security_group.vpc_endpoint.id]
subnet_ids = module.vpc.private_subnets
private_dns_enabled = false
tags = var.tags
}
#####
# Security Group configuration for VPC endpoints
#####
resource "random_id" "vpc_endpoint_sg_suffix" {
byte_length = 4
}
resource "aws_security_group" "vpc_endpoint" {
name = "${local.name_prefix}-vpc-endpoint-sg-${random_id.vpc_endpoint_sg_suffix.hex}"
description = "Security Group used by VPC Endpoints."
vpc_id = module.vpc.vpc_id
tags = merge(
var.tags,
{
"Name" = "${local.name_prefix}-vpc-endpoint-sg-${random_id.vpc_endpoint_sg_suffix.hex}"
}
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "vpc_endpoint_egress" {
security_group_id = aws_security_group.vpc_endpoint.id
type = "egress"
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
resource "aws_security_group_rule" "vpc_endpoint_self_ingress" {
security_group_id = aws_security_group.vpc_endpoint.id
type = "ingress"
protocol = "-1"
from_port = 0
to_port = 0
source_security_group_id = aws_security_group.vpc_endpoint.id
}
#####
# Security Group configuration for Public LB
#####
resource "random_id" "lb_sg_suffix" {
byte_length = 4
}
resource "aws_security_group" "lb_sg" {
name = "${local.name_prefix}-lb-sg-${random_id.lb_sg_suffix.hex}"
description = "Security Group used by Public LB"
vpc_id = module.vpc.vpc_id
tags = merge(
var.tags,
{
"Name" = "${local.name_prefix}-lb-sg-${random_id.lb_sg_suffix.hex}"
}
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "lb_egress" {
security_group_id = aws_security_group.lb_sg.id
type = "egress"
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
resource "aws_security_group_rule" "lb_http_ingress" {
security_group_id = aws_security_group.lb_sg.id
type = "ingress"
protocol = "tcp"
from_port = 80
to_port = 80
cidr_blocks = ["${chomp(data.http.myip.body)}/32"]
}
resource "aws_security_group_rule" "lb_https_ingress" {
security_group_id = aws_security_group.lb_sg.id
type = "ingress"
protocol = "tcp"
from_port = 443
to_port = 443
cidr_blocks = ["${chomp(data.http.myip.body)}/32"]
}
#####
# VPC Flow logs
#####
module "vpc-flow-logs" {
source = "umotif-public/vpc-flow-logs/aws"
version = "~> 1.1.2"
name_prefix = local.name_prefix
vpc_id = module.vpc.vpc_id
traffic_type = "ALL"
tags = var.tags
}
#####
# Outputs
#####
output "vpc_id" {
value = module.vpc.vpc_id
}
output "public_subnet_ids" {
value = module.vpc.public_subnets
}
output "private_subnet_ids" {
value = module.vpc.private_subnets
}
output "private_route_table_ids" {
value = module.vpc.private_route_table_ids
}