-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
157 lines (132 loc) · 4.71 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
# locals {
# resource_name_prefix = "${var.namespace}-${var.resource_tag_name}"
# api_url = "${aws_api_gateway_deployment._.invoke_url}${aws_api_gateway_stage._.stage_name}"
# api_name = "${local.resource_name_prefix}-${var.api_name}"
# }
data "template_file" "_" {
template = var.api_template
vars = var.api_template_vars
}
resource "aws_api_gateway_rest_api" "_" {
name = var.api_name
api_key_source = "HEADER"
binary_media_types = var.binary_media_types
description = var.api_description
body = data.template_file._.rendered
endpoint_configuration {
types = ["REGIONAL"]
}
}
resource "aws_api_gateway_deployment" "_" {
rest_api_id = aws_api_gateway_rest_api._.id
# stage_name = ""
triggers = {
redeployment = sha1(data.template_file._.rendered)
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_cloudwatch_log_group" "error_log" {
for_each = var.namespace
name = "API-Gateway-Execution-Logs_${aws_api_gateway_rest_api._.id}/${each.key}"
retention_in_days = 7
# ... potentially other configuration ...
}
resource "aws_cloudwatch_log_group" "access_log" {
for_each = var.namespace
name = "API-Gateway-Access-Logs_${aws_api_gateway_rest_api._.id}/${each.key}"
retention_in_days = 7
# ... potentially other configuration ...
}
resource "aws_api_gateway_stage" "_" {
depends_on = [aws_cloudwatch_log_group.error_log, aws_cloudwatch_log_group.access_log]
for_each = var.namespace
stage_name = each.key
variables = { url = each.value }
rest_api_id = aws_api_gateway_rest_api._.id
deployment_id = aws_api_gateway_deployment._.id
access_log_settings {
destination_arn = "arn:aws:logs:${var.region}:${var.account_id}:log-group:API-Gateway-Access-Logs_${aws_api_gateway_rest_api._.id}/${each.key}"
format = jsonencode(
{
"caller" = "$context.identity.caller"
"httpMethod" = "$context.httpMethod"
"ip" = "$context.identity.sourceIp"
"protocol" = "$context.protocol"
"requestId" = "$context.requestId"
"requestTime" = "$context.requestTime"
"resourcePath" = "$context.resourcePath"
"responseLength" = "$context.responseLength"
"status" = "$context.status"
"user" = "$context.identity.user"
}
)
}
# xray_tracing_enabled = var.xray_tracing_enabled
# tags = {
# Environment = var.namespace
# Name = var.resource_tag_name
# }
}
resource "aws_apigatewayv2_domain_name" "this" {
count = var.domain_name != "" ? 1 : 0
domain_name = var.domain_name
domain_name_configuration {
certificate_arn = var.domain_name_certificate_arn
endpoint_type = "REGIONAL"
security_policy = "TLS_1_2"
}
}
resource "aws_api_gateway_base_path_mapping" "this" {
depends_on = [aws_api_gateway_stage._]
count = var.domain_name != "" ? 1 : 0
api_id = aws_api_gateway_rest_api._.id
domain_name = aws_apigatewayv2_domain_name.this[0].id
stage_name = var.bind_stage_name
}
resource "aws_route53_record" "this" {
count = var.domain_name != "" ? 1 : 0
name = aws_apigatewayv2_domain_name.this[0].domain_name
type = "A"
zone_id = var.zone_id
alias {
name = aws_apigatewayv2_domain_name.this[0].domain_name_configuration[0].target_domain_name
zone_id = aws_apigatewayv2_domain_name.this[0].domain_name_configuration[0].hosted_zone_id
evaluate_target_health = false
}
}
resource "aws_api_gateway_usage_plan" "myusageplan" {
depends_on = [aws_api_gateway_stage._]
count = var.create_usage_plan ? 1 : 0
name = "${var.api_name}_usage_plan"
dynamic "api_stages" {
for_each = var.namespace
content {
api_id = aws_api_gateway_rest_api._.id
stage = api_stages.key
}
}
}
resource "aws_api_gateway_api_key" "mykey" {
count = var.create_usage_plan ? 1 : 0
name = "${var.api_name}_key"
}
resource "aws_api_gateway_usage_plan_key" "main" {
count = var.create_usage_plan ? 1 : 0
key_id = aws_api_gateway_api_key.mykey[0].id
key_type = "API_KEY"
usage_plan_id = aws_api_gateway_usage_plan.myusageplan[0].id
}
# resource "aws_api_gateway_method_settings" "_" {
# rest_api_id = aws_api_gateway_rest_api._.id
# stage_name = aws_api_gateway_stage._.stage_name
# method_path = "*/*"
# settings {
# throttling_burst_limit = var.api_throttling_burst_limit
# throttling_rate_limit = var.api_throttling_rate_limit
# metrics_enabled = var.api_metrics_enabled
# logging_level = var.api_logging_level
# data_trace_enabled = var.api_data_trace_enabled
# }
# }