forked from rpstreef/tf-apigateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
114 lines (89 loc) · 3.03 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
locals {
resource_name_prefix = "${var.namespace}-${var.resource_tag_name}"
}
data "template_file" "_" {
template = var.api_template
vars = var.api_template_vars
}
data "aws_api_gateway_domain_name" "_" {
depends_on = [aws_api_gateway_domain_name._]
count = var.api_domain_name == "" ? 0 : 1
domain_name = var.api_domain_name
}
resource "aws_api_gateway_rest_api" "_" {
name = var.api_name
api_key_source = "HEADER"
body = data.template_file._.rendered
}
resource "aws_api_gateway_deployment" "_" {
rest_api_id = aws_api_gateway_rest_api._.id
stage_name = ""
lifecycle {
create_before_destroy = true
}
# Triggers a re-deployment to the stage
triggers = {
redeployment = base64sha256(data.template_file._.template)
}
}
resource "aws_api_gateway_stage" "_" {
stage_name = var.namespace
rest_api_id = aws_api_gateway_rest_api._.id
deployment_id = aws_api_gateway_deployment._.id
xray_tracing_enabled = var.xray_tracing_enabled
tags = {
Environment = var.namespace
Name = var.resource_tag_name
}
}
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
}
}
#
# Domain Setup
#
resource "aws_api_gateway_domain_name" "_" {
domain_name = var.api_domain_name
endpoint_configuration {
types = ["REGIONAL"]
}
regional_certificate_arn = var.acm_certificate_arn
security_policy = "TLS_1_2"
count = var.api_domain_name == "" ? 0 : 1
}
resource "aws_api_gateway_base_path_mapping" "_" {
count = var.api_domain_name == "" ? 0 : 1
api_id = aws_api_gateway_rest_api._.id
domain_name = aws_api_gateway_domain_name._[0].domain_name
stage_name = aws_api_gateway_stage._.stage_name
base_path = var.api_base_path
}
# -----------------------------------------------------------------------------
# CloudWatch: API Gateway
# -----------------------------------------------------------------------------
module "cloudwatch_alarms_apigateway" {
source = "./cloudwatch-alarms-apigateway"
namespace = var.namespace
region = var.region
resource_tag_name = var.resource_tag_name
api_name = var.api_name
api_stage = aws_api_gateway_stage._.stage_name
resources = var.resources
create_latency_alarm = var.create_latency_alarm
latency_threshold_p95 = var.latency_threshold_p95
latency_threshold_p99 = var.latency_threshold_p99
latency_evaluationPeriods = var.latency_evaluationPeriods
fourRate_threshold = var.fourRate_threshold
fourRate_evaluationPeriods = var.fourRate_evaluationPeriods
fiveRate_threshold = var.fiveRate_threshold
fiveRate_evaluationPeriods = var.fiveRate_evaluationPeriods
}