-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmain.tf
More file actions
130 lines (111 loc) · 4.09 KB
/
main.tf
File metadata and controls
130 lines (111 loc) · 4.09 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
resource "aws_vpc" "custom_vpc" {
cidr_block = var.networking.cidr_block
tags = {
Name = var.networking.vpc_name
}
}
# PUBLIC SUBNETS
resource "aws_subnet" "public_subnets" {
count = var.networking.public_subnets == null || var.networking.public_subnets == "" ? 0 : length(var.networking.public_subnets)
vpc_id = aws_vpc.custom_vpc.id
cidr_block = var.networking.public_subnets[count.index]
availability_zone = var.networking.azs[count.index]
map_public_ip_on_launch = true
tags = {
Name = "public_subnet-${count.index}"
}
}
# PRIVATE SUBNETS
resource "aws_subnet" "private_subnets" {
count = var.networking.private_subnets == null || var.networking.private_subnets == "" ? 0 : length(var.networking.private_subnets)
vpc_id = aws_vpc.custom_vpc.id
cidr_block = var.networking.private_subnets[count.index]
availability_zone = var.networking.azs[count.index]
map_public_ip_on_launch = false
tags = {
Name = "private_subnet-${count.index}"
}
}
# INTERNET GATEWAY
resource "aws_internet_gateway" "i_gateway" {
vpc_id = aws_vpc.custom_vpc.id
tags = {
Name = "i_gateway"
}
}
# EIPs
resource "aws_eip" "elastic_ip" {
count = var.networking.private_subnets == null || var.networking.nat_gateways == false ? 0 : length(var.networking.private_subnets)
vpc = true
depends_on = [aws_internet_gateway.i_gateway]
tags = {
Name = "eip-${count.index}"
}
}
# NAT GATEWAYS
resource "aws_nat_gateway" "nats" {
count = var.networking.private_subnets == null || var.networking.nat_gateways == false ? 0 : length(var.networking.private_subnets)
subnet_id = aws_subnet.public_subnets[count.index].id
connectivity_type = "public"
allocation_id = aws_eip.elastic_ip[count.index].id
depends_on = [aws_internet_gateway.i_gateway]
}
# PUBLIC ROUTE TABLE
resource "aws_route_table" "public_table" {
vpc_id = aws_vpc.custom_vpc.id
}
resource "aws_route" "public_routes" {
route_table_id = aws_route_table.public_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.i_gateway.id
}
resource "aws_route_table_association" "assoc_public_routes" {
count = length(var.networking.public_subnets)
subnet_id = aws_subnet.public_subnets[count.index].id
route_table_id = aws_route_table.public_table.id
}
# PRIVATE ROUTE TABLES
resource "aws_route_table" "private_tables" {
count = length(var.networking.azs)
vpc_id = aws_vpc.custom_vpc.id
}
resource "aws_route" "private_routes" {
count = length(var.networking.private_subnets)
route_table_id = aws_route_table.private_tables[count.index].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nats[count.index].id
}
resource "aws_route_table_association" "assoc_private_routes" {
count = length(var.networking.private_subnets)
subnet_id = aws_subnet.private_subnets[count.index].id
route_table_id = aws_route_table.private_tables[count.index].id
}
# SECURITY GROUPS
resource "aws_security_group" "sec_groups" {
for_each = { for sec in var.security_groups : sec.name => sec }
name = each.value.name
description = each.value.description
vpc_id = aws_vpc.custom_vpc.id
dynamic "ingress" {
for_each = try(each.value.ingress, [])
content {
description = ingress.value.description
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr_blocks
ipv6_cidr_blocks = ingress.value.ipv6_cidr_blocks
}
}
dynamic "egress" {
for_each = try(each.value.egress, [])
content {
description = egress.value.description
from_port = egress.value.from_port
to_port = egress.value.to_port
protocol = egress.value.protocol
cidr_blocks = egress.value.cidr_blocks
ipv6_cidr_blocks = egress.value.ipv6_cidr_blocks
}
}
}