-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
357 lines (315 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
350
351
352
353
354
355
356
357
# Configure the AWS Provider
provider "aws" {
region = var.aws_region
}
data "aws_availability_zones" "available" {}
data "aws_region" "current" {}
# Get Latest Ubuntu 20.04 AMI Image
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"]
}
data "aws_security_groups" "security_groups" { # needed for output
}
locals {
team = "api_mgmt_dev"
application = "corp_api"
serve_name = "ec2-${var.environment}-api-${var.variables_sub_az}"
az_names = data.aws_availability_zones.available.names
}
resource "aws_key_pair" "developer" {
key_name = "developer-${var.environment}"
public_key = try(
file("MyAWSKey.pub"),
var.my_aws_pub
)
lifecycle {
ignore_changes = [key_name]
}
}
#Define the VPC
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr
tags = {
Name = var.vpc_name
Environment = var.environment
Terraform = "true"
Region = data.aws_region.current.name
}
enable_dns_hostnames = true
}
#Deploy the public subnets
resource "aws_subnet" "public_subnets" {
for_each = var.public_subnets
vpc_id = aws_vpc.vpc.id
cidr_block = cidrsubnet(var.vpc_cidr, 8, each.value + 100)
availability_zone = try(
local.az_names[each.value],
local.az_names[0],
)
map_public_ip_on_launch = true
tags = {
Name = each.key
Terraform = "true"
}
}
#Deploy the private subnets
resource "aws_subnet" "private_subnets" {
for_each = var.private_subnets
vpc_id = aws_vpc.vpc.id
cidr_block = cidrsubnet(var.vpc_cidr, 8, each.value)
availability_zone = try(
local.az_names[each.value],
local.az_names[0]
)
tags = {
Name = each.key
Terraform = "true"
}
}
#Create Internet Gateway
resource "aws_internet_gateway" "internet_gateway" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "demo_igw"
}
}
#Create EIP for NAT Gateway
resource "aws_eip" "nat_gateway_eip" {
domain = "vpc"
depends_on = [aws_internet_gateway.internet_gateway]
tags = {
Name = "demo_igw_eip"
}
}
#Create NAT Gateway
resource "aws_nat_gateway" "nat_gateway" {
depends_on = [aws_subnet.public_subnets]
allocation_id = aws_eip.nat_gateway_eip.id
subnet_id = aws_subnet.public_subnets["public_subnet_1"].id
tags = {
Name = "demo_nat_gateway"
}
}
#Create route tables for public and private subnets
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.internet_gateway.id
#nat_gateway_id = aws_nat_gateway.nat_gateway.id
}
tags = {
Name = "demo_public_rtb"
Terraform = "true"
}
}
resource "aws_route_table" "private_route_table" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
# gateway_id = aws_internet_gateway.internet_gateway.id
nat_gateway_id = aws_nat_gateway.nat_gateway.id
}
tags = {
Name = "demo_private_rtb"
Terraform = "true"
}
}
#Create route table associations
resource "aws_route_table_association" "public" {
depends_on = [aws_subnet.public_subnets]
route_table_id = aws_route_table.public_route_table.id
for_each = aws_subnet.public_subnets
subnet_id = each.value.id
}
resource "aws_route_table_association" "private" {
depends_on = [aws_subnet.private_subnets]
route_table_id = aws_route_table.private_route_table.id
for_each = aws_subnet.private_subnets
subnet_id = each.value.id
}
# Terraform Resource Block - To Build EC2 instance in Public Subnet
resource "aws_instance" "web_server" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
subnet_id = aws_subnet.public_subnets["public_subnet_1"].id
tags = {
Name = local.serve_name
Owner = local.team
App = local.application
}
key_name = aws_key_pair.developer.key_name
vpc_security_group_ids = [aws_security_group.allow_ssh.id, aws_security_group.allow_web.id, aws_security_group.allow_grafana.id]
provisioner "local-exec" {
command = "printf '[main]\n${self.public_ip}' > aws_hosts"
}
}
resource "aws_security_group" "allow_web" {
name = "allow_web"
description = "Allow TLS inbound traffic"
vpc_id = aws_vpc.vpc.id
ingress {
description = "TLS from VPC"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Http from VPC"
from_port = 80
to_port = 80
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"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "allow_web"
}
}
resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
description = "Allow ssh inbound traffic"
vpc_id = aws_vpc.vpc.id
ingress {
description = "ssh from VPC"
from_port = 22
to_port = 22
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"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "allow_ssh"
}
}
resource "aws_security_group" "ingress-443" {
name = "web_server_inbound"
description = "Allow inbound traffic on tcp/443"
vpc_id = aws_vpc.vpc.id
ingress {
description = "Allow 443 from the Internet"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "web_server_443_inbound"
Purpose = "Intro to Resource Blocks Lab"
}
}
resource "aws_security_group" "allow_jenkins" {
name = "allow_jenkins"
description = "Allow tcp inbound port 8080"
vpc_id = aws_vpc.vpc.id
ingress {
description = "TCP to 8080 for jenkins"
from_port = 8080
to_port = 8080
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"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "allow_jenkins"
}
}
resource "aws_security_group" "allow_grafana" {
name = "allow_grafana"
description = "Allow tcp inbound port 3000"
vpc_id = aws_vpc.vpc.id
ingress {
description = "TCP to 3000 for grafana"
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"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "allow_grafana"
}
}
resource "aws_security_group" "main" {
name = "main-global"
description = "AllowDoes nothing"
vpc_id = aws_vpc.vpc.id
tags = {
Name = "main"
Purpose = "Does nothing"
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_s3_bucket" "my-new-S3-bucket" {
bucket = "my-new-tf-test-bucket-${random_id.randomness.hex}"
tags = {
Name = "My S3 Bucket"
Purpose = "Intro to Resource Blocks Lab"
}
}
resource "aws_s3_bucket_ownership_controls" "my_new_bucket_acl" {
bucket = aws_s3_bucket.my-new-S3-bucket.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_acl" "my-new-S3-bucket-acl" {
bucket = aws_s3_bucket.my-new-S3-bucket.id
acl = "private"
depends_on = [aws_s3_bucket_ownership_controls.my_new_bucket_acl]
}
resource "random_id" "randomness" {
byte_length = 16
}
resource "aws_subnet" "list_subnet" {
for_each = var.ip
vpc_id = aws_vpc.vpc.id
cidr_block = each.value
availability_zone = local.az_names[0]
tags = {
Environment = var.environment
"CIDR block" = each.value
}
}
resource "null_resource" "grafana_install" {
depends_on = [aws_instance.web_server]
provisioner "local-exec" {
command = "ansible-playbook ./playbooks/monitor-server.yml -i aws_hosts --private-key=${var.my_aws_pem} -u ubuntu"
}
}