-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
349 lines (304 loc) · 8.11 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#### This provisions a VPC
resource "aws_vpc" "CICDVPC"{
cidr_block = "10.0.0.0/16"
tags = {
"Name" = "CICDVPC"
}
}
#### This provisions our public subnet
resource "aws_subnet" "PUBSUB1" {
vpc_id = aws_vpc.CICDVPC
cidr_block = var.cidr_block_PUBSUB1
availability_zone = var.AZ1
map_public_ip_on_launch = true
tags = {
"Name" = "PUBSUB1"
}
}
#### This provisions our internet gateway
resource "aws_internet_gateway" "CICDIGW" {
vpc_id = aws_vpc.CICDVPC
}
#### This attaches the internet gateway to the VPC
resource "aws_internet_gateway_attachment" "IGWA" {
internet_gateway_id = aws_internet_gateway.CICDIGW
vpc_id = aws_vpc.CICDVPC
}
#### This generates a Keypair using ssh-keygen.exe
resource "aws_key_pair" "CICD-key" {
key_name = "CICD-key"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQD070L92OaGBwCMjVdEox3HsMhKkTw+PvivQLMxiHGkZ1nu7pkB5O5FIZbl9a8GbweQ2JSsv4b0qylx6rXnLjpqVI4NcHorkss05wqv175fdnPDwPj8cjmnXLxl4i70Q+3NDj7YVjJPKB/USDfS6gSCpoZWomq58g1RxKJaKUlrUzpNYHAqJbQz9JRTVF/cBJt80pnYfqunzMotGStyXXMj7yey48B6IlK8q8fOqVirt+UyY2xLJRoTiZkKceAWtdPDl3r5NcV6u22FAr1MYTOoEZ0PdSsTvGqKDrQVTVg+7gC62SmXPkfDEcecLdL9FPhfNf95fJ8FPJ0tlANL/6uAUj+fKWraeGi9G3lEuKRXfQBr8xvCghr/bKo7vnmqfvbkMBYoaQeS4FxFEznmx4wPSzJuaXRCkuJ5+FaYnGw+i9OJwBE49ecnXmdOWGHU9ZLUo0kXP/IsGIGgXf3jzhflmjcfOQtLqkS2mjFdwdzOwT80AiQ1q/LKodEwPYIA/VU= admin@PF313WVE-JDV"
}
#### This provisions the Jenkin's server with SG open to port 22 for SSH and 8080 fpr Jenkins, with userdata attached
resource "aws_instance" "Jenkins" {
ami = var.ami
instance_type = var.instance_type
key_name = "CICD-key"
subnet_id = aws_subnet.PUBSUB1
security_groups = [ "aws_security_group.jenkins-sg" ]
user_data = file("installjenkins.sh")
tags = {
"Name" = "Jenkins"
}
}
resource "aws_security_group" "jenkins-sg" {
name = "jenkins-sg"
description = "Allow access to port 22 and 8080"
vpc_id = aws_vpc.CICDVPC
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 9100
to_port = 9100
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
#### This provisions the SonarQube server using Ubuntu 18.04 t2 medium with SG open to port 22 for SSH and 9000 f0r Sonarqube with usedata attached
resource "aws_instance" "sonarqube" {
ami = var.ami_ubuntu
instance_type = var.instance_type
key_name = "CICD-key"
subnet_id = aws_subnet.PUBSUB1
security_groups = [ "aws_security_group.sonarqube-sg" ]
user_data = file("installsonarqube.sh")
tags = {
"Name" = "sonarqube"
}
}
resource "aws_security_group" "Sonarqube-sg" {
name = "jenkins-sg"
description = "Allow access to port 22 and 9000"
vpc_id = aws_vpc.CICDVPC
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 9000
to_port = 9000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 9100
to_port = 9100
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
#### This provisions the Nexus server with SG open to port 22 for SSH and 8081 f0r Nexus
resource "aws_instance" "Nexus" {
ami = var.ami
instance_type = var.instance_type
key_name = "CICD-key"
subnet_id = aws_subnet.PUBSUB1
security_groups = [ "aws_security_group.Nexus-sg" ]
user_data = file("installnexus.sh")
tags = {
"Name" = "Nexus"
}
}
resource "aws_security_group" "Nexus-sg" {
name = "Nexus-sg"
description = "Allow access to port 22 and 8081"
vpc_id = aws_vpc.CICDVPC
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8081
to_port = 8081
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 9100
to_port = 9100
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
#### This provisions the prometheus server with SG open to port 22 for SSH and 8081 f0r Nexus
resource "aws_instance" "Prometheus" {
ami = var.ami
instance_type = var.instance_type
key_name = "CICD-key"
subnet_id = aws_subnet.PUBSUB1
security_groups = [ "aws_security_group.prometheus-sg" ]
user_data = file("installprometheus.sh")
tags = {
"Name" = "Nexus"
}
}
resource "aws_security_group" "prometheus-sg" {
name = "prometheus-sg"
description = "Allow access to port 22 and 9090"
vpc_id = aws_vpc.CICDVPC
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 9090
to_port = 9090
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
#### This provisions the Grafana server with SG open to port 22 for SSH and 3000 f0r Grafana
resource "aws_instance" "Grafana" {
ami = var.ami
instance_type = var.instance_type
key_name = "CICD-key"
subnet_id = aws_subnet.PUBSUB1
security_groups = [ "aws_security_group.Grafana-sg" ]
user_data = file("installgrafana.sh")
tags = {
"Name" = "Nexus"
}
}
resource "aws_security_group" "Grafana-sg" {
name = "Grafana-sg"
description = "Allow access to port 22 and 8081"
vpc_id = aws_vpc.CICDVPC
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
#### This provisions the Dev-Env server with SG open to port 22, 8080, 9100
resource "aws_instance" "Dev-Env" {
ami = var.ami
instance_type = var.instance_type
key_name = "CICD-key"
subnet_id = aws_subnet.PUBSUB1
security_groups = [ "aws_security_group.Dev-Env-sg" ]
tags = {
"Name" = "Dev-Env"
}
}
resource "aws_security_group" "Dev-Env-sg" {
name = "Dev-Env-sg"
description = "Allow access to port 22 and 8081"
vpc_id = aws_vpc.CICDVPC
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 9100
to_port = 9100
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
#### This provisions the Stage-Env server with SG open to port 22, 8080, 9100
resource "aws_instance" "Stage-Env" {
ami = var.ami
instance_type = var.instance_type
key_name = "CICD-key"
subnet_id = aws_subnet.PUBSUB1
security_groups = [ "aws_security_group.Stage-Env-sg" ]
tags = {
"Name" = "Stage-Env"
}
}
resource "aws_security_group" "Stage-Env-sg" {
name = "Stage-Env-sg"
description = "Allow access to port 22 and 8081"
vpc_id = aws_vpc.CICDVPC
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 9100
to_port = 9100
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}