forked from aws-samples/serverless-jenkins-on-aws-fargate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
193 lines (158 loc) · 4.44 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
resource "aws_security_group" "efs_security_group" {
name = "${var.name_prefix}-efs"
description = "${var.name_prefix} efs security group"
vpc_id = var.vpc_id
ingress {
protocol = "tcp"
security_groups = [aws_security_group.jenkins_controller_security_group.id]
from_port = 2049
to_port = 2049
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.tags
}
resource "aws_security_group" "jenkins_controller_security_group" {
name = "${var.name_prefix}-controller"
description = "${var.name_prefix} controller security group"
vpc_id = var.vpc_id
ingress {
protocol = "tcp"
self = true
security_groups = var.alb_create_security_group ? [aws_security_group.alb_security_group[0].id] : var.alb_security_group_ids
from_port = var.jenkins_controller_port
to_port = var.jenkins_controller_port
description = "Communication channel to jenkins leader"
}
ingress {
protocol = "tcp"
self = true
security_groups = var.alb_create_security_group ? [aws_security_group.alb_security_group[0].id] : var.alb_security_group_ids
from_port = var.jenkins_jnlp_port
to_port = var.jenkins_jnlp_port
description = "Communication channel to jenkins leader"
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.tags
}
// ALB
resource "aws_security_group" "alb_security_group" {
count = var.alb_create_security_group ? 1 : 0
name = "${var.name_prefix}-alb"
description = "${var.name_prefix} alb security group"
vpc_id = var.vpc_id
ingress {
protocol = "tcp"
from_port = 80
to_port = 80
cidr_blocks = var.alb_ingress_allow_cidrs
description = "HTTP Public access"
}
ingress {
protocol = "tcp"
from_port = 443
to_port = 443
cidr_blocks = var.alb_ingress_allow_cidrs
description = "HTTPS Public access"
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.tags
}
resource "aws_lb" "this" {
name = replace("${var.name_prefix}-crtl-alb", "_", "-")
internal = var.alb_type_internal
load_balancer_type = "application"
security_groups = var.alb_create_security_group ? [aws_security_group.alb_security_group[0].id] : var.alb_security_group_ids
subnets = var.alb_subnet_ids
dynamic "access_logs" {
for_each = var.alb_enable_access_logs ? [true] : []
content {
bucket = var.alb_access_logs_bucket_name
prefix = var.alb_access_logs_s3_prefix
enabled = true
}
}
tags = var.tags
}
resource "aws_lb_target_group" "this" {
name = replace("${var.name_prefix}-crtl", "_", "-")
port = var.jenkins_controller_port
protocol = "HTTP"
vpc_id = var.vpc_id
target_type = "ip"
health_check {
enabled = true
path = "/login"
}
tags = var.tags
depends_on = [aws_lb.this]
lifecycle {
create_before_destroy = true
}
}
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.this.arn
port = 80
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
resource "aws_lb_listener" "https" {
load_balancer_arn = aws_lb.this.arn
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08"
certificate_arn = var.alb_acm_certificate_arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.this.arn
}
}
resource "aws_lb_listener_rule" "redirect_http_to_https" {
listener_arn = aws_lb_listener.http.arn
action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
condition {
http_header {
http_header_name = "*"
values = ["*"]
}
}
}
resource "aws_route53_record" "this" {
count = var.route53_create_alias ? 1 : 0
zone_id = var.route53_zone_id
name = var.route53_alias_name
type = "A"
alias {
name = aws_lb.this.dns_name
zone_id = aws_lb.this.zone_id
evaluate_target_health = true
}
}