forked from databricks/terraform-databricks-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc.tf
139 lines (114 loc) · 3.55 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
data "aws_availability_zones" "available" {}
resource "aws_vpc" "mainvpc" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
enable_dns_support = true
tags = merge(var.tags, {
Name = "${local.prefix}-vpc"
})
}
# Public subnets collection, default 1
resource "aws_subnet" "public_subnets" {
count = length(var.public_subnets_cidr)
vpc_id = aws_vpc.mainvpc.id
cidr_block = var.public_subnets_cidr[count.index]
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = true
tags = merge(var.tags, {
Name = "${local.prefix}-${aws_vpc.mainvpc.id}-public-subnet"
})
}
# Private subnets collection for Private Link (VPC endpoints), default 1
resource "aws_subnet" "privatelink" {
count = length(var.privatelink_subnets_cidr)
vpc_id = aws_vpc.mainvpc.id
cidr_block = var.privatelink_subnets_cidr[count.index]
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = false // explicit private subnet
tags = merge(var.tags, {
Name = "${local.prefix}-${aws_vpc.mainvpc.id}-pl-vpce-subnet"
})
}
resource "aws_route_table" "pl_subnet_rt" {
vpc_id = aws_vpc.mainvpc.id
tags = merge(var.tags, {
Name = "${local.prefix}-pl-local-route-tbl"
})
}
resource "aws_route_table_association" "dataplane_vpce_rtb" {
count = length(var.privatelink_subnets_cidr)
subnet_id = aws_subnet.privatelink[count.index].id
route_table_id = aws_route_table.pl_subnet_rt.id
}
# Nat gateway EIP
resource "aws_eip" "nat_gateway_elastic_ips" {
count = length(var.public_subnets_cidr)
vpc = true
}
# Nat gateway
resource "aws_nat_gateway" "nat_gateways" {
count = length(var.public_subnets_cidr)
allocation_id = aws_eip.nat_gateway_elastic_ips[count.index].id
subnet_id = aws_subnet.public_subnets[count.index].id
tags = merge(var.tags, {
Name = "${local.prefix}-${aws_vpc.mainvpc.id}-nat-gateway"
})
}
// Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.mainvpc.id
}
# Public route table
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.mainvpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = merge(var.tags, {
Name = "${local.prefix}-public-rt"
})
}
# Public route table association
resource "aws_route_table_association" "public_route_table_associations" {
count = length(var.public_subnets_cidr)
subnet_id = aws_subnet.public_subnets[count.index].id
route_table_id = aws_route_table.public_route_table.id
}
// Security Group
resource "aws_security_group" "sg" {
vpc_id = aws_vpc.mainvpc.id
depends_on = [aws_vpc.mainvpc]
name = "databricks-vpc-security-group-${local.prefix}"
description = "databricks vpc security group for ${local.prefix}"
dynamic "ingress" {
for_each = local.sg_ingress_protocol
content {
from_port = 0
to_port = 65535
protocol = ingress.value
self = true
}
}
dynamic "egress" {
for_each = local.sg_egress_protocol
content {
from_port = 0
to_port = 65535
protocol = egress.value
self = true
}
}
dynamic "egress" {
for_each = local.sg_egress_ports
content {
from_port = egress.value
to_port = egress.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
tags = {
Name = "${local.prefix}-dataplane-sg"
}
}