-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
97 lines (75 loc) · 2.42 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
################################
# Instances
################################
resource "aws_instance" "server" {
count = var.instance_count
ami = data.aws_ami.ami.id
instance_type = var.instance_type
subnet_id = element(aws_subnet.private.*.id, (count.index % var.vpc_private_subnet_count))
vpc_security_group_ids = [aws_security_group.servers_sg.id]
# iam_instance_profile = module.web_app_s3.instance_profile.name
# depends_on = [module.web_app_s3]
# user_data = templatefile("${path.module}/startup_script.tpl", {
# s3_bucket_name = module.web_app_s3.web_bucket.id
# })
tags = merge(local.common_tags, {
Name = "${local.name_tag}-server${count.index}"
})
}
################################
# S3 Bucket
################################
module "s3_bucket_for_logs" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = join("-", ["koblabs", uuid()])
acl = "log-delivery-write"
versioning = {
enabled = true
}
force_destroy = true
attach_elb_log_delivery_policy = true
}
# ################################
# # Load balancer
# ################################
resource "aws_lb" "nginx" {
name = "${local.name_tag}-alb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb_sg.id]
subnets = aws_subnet.private.*.id
enable_deletion_protection = false
access_logs {
bucket = module.s3_bucket_for_logs.s3_bucket_id
prefix = "alb-logs"
enabled = true
}
tags = merge(local.common_tags, {
Name = "${local.name_tag}-alb"
})
}
resource "aws_lb_target_group" "nginx" {
name = "${local.name_tag}-alb-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
tags = {
Name = "${local.name_tag}-alb-tg"
}
}
resource "aws_lb_listener" "nginx" {
load_balancer_arn = aws_lb.nginx.arn
port = "80"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.nginx.arn
}
tags = local.common_tags
}
resource "aws_lb_target_group_attachment" "nginx" {
count = var.instance_count
target_group_arn = aws_lb_target_group.nginx.arn
target_id = aws_instance.server[count.index].id
port = 80
}