-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
392 lines (346 loc) · 11.8 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
terraform {
required_version = ">= 0.13.2"
required_providers {
aws = ">= 3.0"
}
}
# ==================== Locals ====================
locals {
alb_name = "${var.app_name}-alb" // ALB name has a restriction of 32 characters max
app_domain_url = var.domain_url != null ? var.domain_url : "${var.app_name}.${var.hosted_zone.name}" // Route53 A record name
use_zip = var.zip_filename != null
use_codedeploy = var.codedeploy_service_role_arn != null
hooks = local.use_codedeploy ? setsubtract([
for hook in keys(var.codedeploy_lifecycle_hooks) :
zipmap([hook], [lookup(var.codedeploy_lifecycle_hooks, hook, null)])
], [
{
BeforeAllowTraffic = null
},
{
AfterAllowTraffic = null
}
]) : null
}
# ==================== ALB ====================
resource "aws_alb" "alb" {
name = local.alb_name
subnets = var.public_subnet_ids
security_groups = [aws_security_group.alb-sg.id]
tags = var.tags
}
resource "aws_security_group" "alb-sg" {
name = "${local.alb_name}-sg"
description = "Controls access to the ${local.alb_name}"
vpc_id = var.vpc_id
// allow access to the ALB from anywhere for 80 and 443
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
// if test listner port is specified, allow traffic
dynamic "ingress" {
for_each = var.codedeploy_test_listener_port != null ? [1] : []
content {
from_port = var.codedeploy_test_listener_port
to_port = var.codedeploy_test_listener_port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
// allow any outgoing traffic
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.tags
}
resource "aws_alb_target_group" "tg" {
name = "${var.app_name}-tg"
target_type = "lambda"
tags = var.tags
depends_on = [aws_alb.alb]
}
resource "aws_alb_target_group" "tst_tg" {
count = local.use_codedeploy ? 1 : 0
name = "${var.app_name}-tst"
target_type = "lambda"
tags = var.tags
depends_on = [aws_alb.alb]
}
resource "aws_alb_listener" "https" {
load_balancer_arn = aws_alb.alb.arn
port = 443
protocol = "HTTPS"
certificate_arn = var.https_certificate_arn
default_action {
type = "forward"
target_group_arn = aws_alb_target_group.tg.arn
}
lifecycle {
ignore_changes = [default_action[0].target_group_arn]
}
depends_on = [
aws_alb_target_group.tg
]
}
resource "aws_alb_listener" "test_https" {
count = local.use_codedeploy ? 1 : 0
load_balancer_arn = aws_alb.alb.arn
port = 4443
protocol = "HTTPS"
certificate_arn = var.https_certificate_arn
default_action {
type = "forward"
target_group_arn = aws_alb_target_group.tst_tg[0].arn
}
lifecycle {
ignore_changes = [default_action[0].target_group_arn]
}
depends_on = [
aws_alb_target_group.tst_tg
]
}
resource "aws_alb_listener" "http_to_https" {
load_balancer_arn = aws_alb.alb.arn
port = 80
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
status_code = "HTTP_301"
port = aws_alb_listener.https.port
protocol = aws_alb_listener.https.protocol
}
}
}
resource "aws_lambda_permission" "with_lb" {
statement_id = "AllowExecutionFromlb"
action = "lambda:InvokeFunction"
function_name = local.use_zip ? aws_lambda_function.zip_api[0].function_name : aws_lambda_function.docker_api[0].function_name
principal = "elasticloadbalancing.amazonaws.com"
source_arn = aws_alb_target_group.tg.arn
qualifier = local.use_codedeploy ? aws_lambda_alias.live_codedeploy[0].name : aws_lambda_alias.live[0].name
}
resource "aws_lambda_permission" "with_tst_lb" {
count = local.use_codedeploy ? 1 : 0
statement_id = "AllowExecutionFromlb"
action = "lambda:InvokeFunction"
function_name = local.use_zip ? aws_lambda_function.zip_api[0].function_name : aws_lambda_function.docker_api[0].function_name
principal = "elasticloadbalancing.amazonaws.com"
source_arn = aws_alb_target_group.tst_tg[0].arn
}
resource "aws_alb_target_group_attachment" "live_attachment" {
target_group_arn = aws_alb_target_group.tg.arn
target_id = local.use_codedeploy ? aws_lambda_alias.live_codedeploy[0].arn : aws_lambda_alias.live[0].arn #Live
depends_on = [aws_lambda_permission.with_lb]
}
resource "aws_alb_target_group_attachment" "tst_attachment" {
count = local.use_codedeploy ? 1 : 0
target_group_arn = aws_alb_target_group.tst_tg[0].arn
target_id = local.use_zip ? aws_lambda_function.zip_api[0].arn : aws_lambda_function.docker_api[0].arn # Latest
depends_on = [aws_lambda_permission.with_tst_lb]
}
# ==================== Route53 ====================
resource "aws_route53_record" "a_record" {
name = local.app_domain_url
type = "A"
zone_id = var.hosted_zone.id
alias {
evaluate_target_health = true
name = aws_alb.alb.dns_name
zone_id = aws_alb.alb.zone_id
}
}
resource "aws_route53_record" "aaaa_record" {
name = local.app_domain_url
type = "AAAA"
zone_id = var.hosted_zone.id
alias {
evaluate_target_health = true
name = aws_alb.alb.dns_name
zone_id = aws_alb.alb.zone_id
}
}
# ==================== Lambda ====================
resource "aws_iam_role" "iam_for_lambda" {
name = "${var.app_name}-role"
permissions_boundary = var.role_permissions_boundary_arn
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_eni_attach" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaENIManagementAccess"
role = aws_iam_role.iam_for_lambda.name
}
resource "aws_iam_role_policy_attachment" "lambda_policy_attach" {
count = length(var.lambda_policies)
policy_arn = element(var.lambda_policies, count.index)
role = aws_iam_role.iam_for_lambda.name
}
resource "aws_security_group" "lambda_sg" {
count = var.lambda_vpc_config != null ? 1 : 0
name = "${var.app_name}-lambda-sg"
description = "Controls access to the Lambda"
vpc_id = var.vpc_id
# ingress not needed as ALB invokes Lambda via AWS API, not direct network traffic
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.tags
}
resource "aws_lambda_function" "zip_api" {
count = local.use_zip ? 1 : 0
function_name = var.app_name
role = aws_iam_role.iam_for_lambda.arn
filename = var.zip_filename
source_code_hash = filebase64sha256(var.zip_filename)
handler = var.zip_handler
runtime = var.zip_runtime
publish = true
layers = var.lambda_layers
timeout = var.timeout
memory_size = var.memory_size
architectures = [var.architecture]
tracing_config {
mode = var.xray_enabled == true ? "Active" : "PassThrough"
}
dynamic "environment" {
for_each = var.environment_variables != null ? [1] : []
content {
variables = var.environment_variables
}
}
dynamic "vpc_config" {
for_each = var.lambda_vpc_config == null ? [] : [var.lambda_vpc_config]
content {
subnet_ids = var.lambda_vpc_config.subnet_ids
security_group_ids = concat([aws_security_group.lambda_sg[0].id], var.lambda_vpc_config.security_group_ids)
}
}
}
resource "aws_lambda_function" "docker_api" {
count = var.image_uri != null ? 1 : 0
function_name = var.app_name
role = aws_iam_role.iam_for_lambda.arn
package_type = "Image"
image_uri = var.image_uri
publish = true
timeout = var.timeout
memory_size = var.memory_size
architectures = [var.architecture]
tracing_config {
mode = var.xray_enabled == true ? "Active" : "PassThrough"
}
dynamic "environment" {
for_each = var.environment_variables != null ? [1] : []
content {
variables = var.environment_variables
}
}
dynamic "vpc_config" {
for_each = var.lambda_vpc_config == null ? [] : [var.lambda_vpc_config]
content {
subnet_ids = var.lambda_vpc_config.subnet_ids
security_group_ids = concat([aws_security_group.lambda_sg[0].id], var.lambda_vpc_config.security_group_ids)
}
}
}
resource "aws_lambda_alias" "live" {
count = !local.use_codedeploy ? 1 : 0
name = "live"
description = "ALB sends traffic to this version"
function_name = local.use_zip ? aws_lambda_function.zip_api[0].function_name : aws_lambda_function.docker_api[0].function_name
function_version = local.use_zip ? aws_lambda_function.zip_api[0].version : aws_lambda_function.docker_api[0].version
}
resource "aws_lambda_alias" "live_codedeploy" {
count = local.use_codedeploy ? 1 : 0
name = "live"
description = "ALB sends traffic to this version"
function_name = local.use_zip ? aws_lambda_function.zip_api[0].function_name : aws_lambda_function.docker_api[0].function_name
# Get the version of the lambda when it is first created
function_version = local.use_zip ? aws_lambda_function.zip_api[0].version : aws_lambda_function.docker_api[0].version
# Let CodeDeploy handle changes to the function version that this alias refers to
lifecycle {
ignore_changes = [
function_version
]
}
}
# ==================== CodeDeploy ====================
resource "aws_codedeploy_app" "app" {
count = local.use_codedeploy ? 1 : 0
compute_platform = "Lambda"
name = "${var.app_name}-cd"
}
resource "aws_codedeploy_deployment_group" "deployment_group" {
count = local.use_codedeploy ? 1 : 0
app_name = aws_codedeploy_app.app[0].name
deployment_group_name = "${var.app_name}-dg"
service_role_arn = var.codedeploy_service_role_arn
deployment_config_name = "CodeDeployDefault.LambdaAllAtOnce"
deployment_style {
deployment_option = "WITH_TRAFFIC_CONTROL"
deployment_type = "BLUE_GREEN"
}
auto_rollback_configuration {
enabled = true
events = ["DEPLOYMENT_FAILURE"]
}
}
# ==================== CloudWatch ====================
resource "aws_cloudwatch_log_group" "log_group" {
name = "/aws/lambda/${var.app_name}"
retention_in_days = var.log_retention_in_days
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "lambda_cloudwatch_attach" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
role = aws_iam_role.iam_for_lambda.name
}
# ==================== AppSpec file ====================
resource "local_file" "appspec_json" {
count = local.use_codedeploy ? 1 : 0
filename = var.codedeploy_appspec_filename != null ? var.codedeploy_appspec_filename : "${path.cwd}/appspec.json"
content = jsonencode({
version = 1
Resources = [{
apiLambdaFunction = {
Type = "AWS::Lambda::Function"
Properties = {
Name = var.app_name
Alias = aws_lambda_alias.live_codedeploy[0].name
CurrentVersion = aws_lambda_alias.live_codedeploy[0].function_version
TargetVersion = local.use_zip ? aws_lambda_function.zip_api[0].version : aws_lambda_function.docker_api[0].version
}
}
}],
Hooks = local.hooks
})
}