-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.tf
166 lines (139 loc) · 3.69 KB
/
main.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
# ---- mtc networking/main.tf -----
locals {
security_groups = {
public = {
name = "public_sg"
description = "public access"
ingress = {
open = {
from = 0
to = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
tg = {
from = 8000
to = 8000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
http = {
from = 80
to = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
}
rds = {
name = "rds_sg"
description = "rds access"
ingress = {
mysql = {
from = 3306
to = 3306
protocol = "tcp"
cidr_blocks = ["10.123.0.0/16"]
}
}
}
}
}
data "aws_availability_zones" "available" {}
resource "random_integer" "random" {
min = 1
max = 99
}
resource "random_shuffle" "public_az" {
input = data.aws_availability_zones.available.names
result_count = var.max_subnets
}
resource "aws_vpc" "mtc_vpc" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "mtc_vpc-${random_integer.random.id}"
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_subnet" "mtc_public_subnet" {
count = var.public_sn_count
vpc_id = aws_vpc.mtc_vpc.id
cidr_block = var.public_cidrs[count.index]
map_public_ip_on_launch = true
availability_zone = random_shuffle.public_az.result[count.index]
tags = {
Name = "mtc_public_${count.index + 1}"
}
}
resource "aws_subnet" "mtc_private_subnet" {
count = var.private_sn_count
vpc_id = aws_vpc.mtc_vpc.id
cidr_block = var.private_cidrs[count.index]
map_public_ip_on_launch = false
availability_zone = random_shuffle.public_az.result[count.index]
tags = {
Name = "mtc_private_${count.index + 1}"
}
}
resource "aws_db_subnet_group" "mtc_rds_subnetgroup" {
count = var.db_subnet_group == "true" ? 1 : 0
name = "mtc_rds_subnetgroup"
subnet_ids = aws_subnet.mtc_private_subnet.*.id
tags = {
Name = "mtc_rds_sng"
}
}
resource "aws_internet_gateway" "mtc_internet_gateway" {
vpc_id = aws_vpc.mtc_vpc.id
tags = {
Name = "mtc_igw"
}
}
resource "aws_route_table" "mtc_public_rt" {
vpc_id = aws_vpc.mtc_vpc.id
tags = {
Name = "mtc_public"
}
}
resource "aws_route" "default_route" {
route_table_id = aws_route_table.mtc_public_rt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.mtc_internet_gateway.id
}
resource "aws_default_route_table" "mtc_private_rt" {
default_route_table_id = aws_vpc.mtc_vpc.default_route_table_id
tags = {
Name = "mtc_private"
}
}
resource "aws_route_table_association" "mtc_public_assoc" {
count = var.public_sn_count
subnet_id = aws_subnet.mtc_public_subnet.*.id[count.index]
route_table_id = aws_route_table.mtc_public_rt.id
}
resource "aws_security_group" "mtc_sg" {
for_each = local.security_groups
name = each.value.name
description = each.value.description
vpc_id = aws_vpc.mtc_vpc.id
#public Security Group
dynamic "ingress" {
for_each = each.value.ingress
content {
from_port = ingress.value.from
to_port = ingress.value.to
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr_blocks
}
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}