-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
108 lines (80 loc) · 3.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
#-----------------------------------------------------------
# API Gateway v1
#-----------------------------------------------------------
resource "aws_api_gateway_rest_api" "this" {
count = var.apigateway_version == "v1" ? 1 : 0
name = var.name
description = var.description
endpoint_configuration {
types = ["REGIONAL"]
}
tags = var.tags
}
resource "aws_api_gateway_domain_name" "this" {
count = var.custom_domain_enabled && var.apigateway_version == "v1" ? 1 : 0
domain_name = var.custom_domain_name
regional_certificate_arn = module.certificate[count.index].certificate_arn
endpoint_configuration {
types = ["REGIONAL"]
}
tags = var.tags
}
#-----------------------------------------------------------
# API Gateway v2
#-----------------------------------------------------------
resource "aws_apigatewayv2_api" "this" {
count = var.apigateway_version == "v2" ? 1 : 0
name = var.name
protocol_type = var.protocol_type
description = var.description
tags = var.tags
}
resource "aws_apigatewayv2_stage" "this" {
count = var.apigateway_version == "v2" ? 1 : 0
name = var.name
api_id = aws_apigatewayv2_api.this[count.index].id
auto_deploy = true
tags = var.tags
}
resource "aws_apigatewayv2_domain_name" "this" {
count = var.custom_domain_enabled && var.apigateway_version == "v2" ? 1 : 0
domain_name = var.custom_domain_name
domain_name_configuration {
certificate_arn = module.certificate[count.index].certificate_arn
endpoint_type = "REGIONAL"
security_policy = "TLS_1_2"
}
tags = var.tags
depends_on = [module.certificate]
}
resource "aws_apigatewayv2_api_mapping" "this" {
count = var.custom_domain_enabled && var.apigateway_version == "v2" ? 1 : 0
api_id = aws_apigatewayv2_api.this[count.index].id
domain_name = aws_apigatewayv2_domain_name.this[count.index].id
stage = aws_apigatewayv2_stage.this[count.index].id
}
#-----------------------------------------------------------
# Certificate
#-----------------------------------------------------------
module "certificate" {
count = var.custom_domain_enabled ? 1 : 0
source = "./modules/certificate"
hosted_zone = var.hosted_zone
custom_dns = var.custom_domain_name
tags = var.tags
}
#-----------------------------------------------------------
# Route53
#-----------------------------------------------------------
resource "aws_route53_record" "this" {
count = var.custom_domain_enabled ? 1 : 0
name = var.custom_domain_name
type = "A"
zone_id = module.certificate[count.index].hosted_zone_id
alias {
name = var.apigateway_version == "v1" ? aws_api_gateway_domain_name.this[count.index].regional_domain_name : aws_apigatewayv2_domain_name.this[count.index].domain_name_configuration[0].target_domain_name
zone_id = var.apigateway_version == "v1" ? aws_api_gateway_domain_name.this[count.index].regional_zone_id : aws_apigatewayv2_domain_name.this[count.index].domain_name_configuration[0].hosted_zone_id
evaluate_target_health = false
}
depends_on = [module.certificate]
}