-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_gateway.py
308 lines (266 loc) · 11.3 KB
/
api_gateway.py
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
import json
import aws_cdk as cdk
from aws_cdk import (
aws_apigatewayv2 as apigwv2,
aws_apigatewayv2_integrations as integrations,
aws_certificatemanager as acm,
aws_cloudwatch as cw,
aws_ec2 as ec2,
aws_iam as iam,
aws_lambda as lambda_,
aws_logs as logs,
aws_route53 as route53,
aws_route53_targets as route53_targets,
aws_ssm as ssm,
)
from constructs import Construct
from infra.constructs.b1.alarm import B1Alarm
class B1ApiGateway(Construct):
"""Creates an API Gateway REST API with a Lambda function as the handler
The API Gateway also contains alarms for client errors, server errors, latency, and integration latency.
It creates an A record in the provided hosted zone for the subdomain.
All requests on the root are handled by a proxy resource.
Attributes
----------
rest_api (apigateway.RestApi): The API Gateway REST API
hosted_zone (route53.HostedZone): The hosted zone for the domain
"""
def __init__(
self,
scope: Construct,
id: str,
service_name: str,
subscription_teams: list[str],
domain_name: str,
subdomain_name: str,
throttling_rate_limit: int = 20,
throttling_burst_limit: int = 40,
hosted_zone_type: str | None = None,
cors_origins: list[str] | None = None,
latency_alarm_threshold_seconds: int = 90,
client_error_alarm_threshold: int = 10,
server_error_alarm_threshold: int = 10,
) -> None:
"""Initialize the LambdaApi construct
Args:
----
scope (cdk.Construct): Parent of this construct
id (str): Identifier for this construct
service_name (str): Name of the service
subscription_teams (list[str]): List of teams to subscribe to the alarms
domain_name (str): The domain name for the API
subdomain_name (str): The subdomain name for the API
hosted_zone_type (str): The type of hosted zone for the domain
log_retention (logs.RetentionDays, optional): Retention duration for the logs. Defaults to logs.RetentionDays.ONE_MONTH.
throttling_rate_limit (int, optional): The rate limit for the API. Defaults to 20.
throttling_burst_limit (int, optional): The burst limit for the API. Defaults to 40.
cors_origins (list[str], optional): List of CORS origins. Defaults to ["*"].
latency_alarm_threshold_seconds (int, optional): The threshold for the latency alarm in seconds. Defaults to 90.
client_error_alarm_threshold (int, optional): The threshold for the client error alarm. Defaults to 10.
server_error_alarm_threshold (int, optional): The threshold for the server error alarm. Defaults to 10.
"""
super().__init__(scope, id)
cors_origins = cors_origins or []
hosted_zone_type = hosted_zone_type or "private"
stage_name = ssm.StringParameter.value_from_lookup(
scope=self,
parameter_name="/platform/stage",
)
vpc = ec2.Vpc.from_lookup(
scope=self,
id="Vpc",
vpc_id=ssm.StringParameter.value_from_lookup(
scope=self,
parameter_name="/platform/vpc/id",
),
)
self.hosted_zone = route53.HostedZone.from_hosted_zone_attributes(
scope=self,
id="HostedZone",
hosted_zone_id=ssm.StringParameter.value_for_string_parameter(
scope=self,
parameter_name=f"/platform/dns/{domain_name}/{hosted_zone_type}-hosted-zone/id",
),
zone_name=ssm.StringParameter.value_for_string_parameter(
scope=self,
parameter_name=f"/platform/dns/{domain_name}/{hosted_zone_type}-hosted-zone/name",
),
)
certificate = acm.Certificate.from_certificate_arn(
scope=self,
id="Certificate",
certificate_arn=ssm.StringParameter.value_for_string_parameter(
scope=self,
parameter_name=f"/platform/dns/{domain_name}/{hosted_zone_type}-hosted-zone/certificate/arn",
),
)
self.security_group = ec2.SecurityGroup(
scope=self,
id="SecurityGroup",
vpc=vpc,
description="Security group for the API Gateway",
)
log_group = logs.LogGroup(
scope=self,
id="LogGroup",
retention=logs.RetentionDays.ONE_MONTH,
)
self.cors_options = apigwv2.CorsPreflightOptions(
allow_origins=[f"https://{self.hosted_zone.zone_name}", f"https://www.{self.hosted_zone.zone_name}", *cors_origins],
allow_methods=[apigwv2.CorsHttpMethod.ANY],
allow_headers=["*"],
allow_credentials=True,
)
api_domain_name = apigwv2.DomainName(
scope=self,
id="DomainName",
certificate=certificate,
domain_name=f"{subdomain_name}.{self.hosted_zone.zone_name}",
security_policy=apigwv2.SecurityPolicy.TLS_1_2,
)
self.http_api = apigwv2.HttpApi(
scope=self,
id="HttpApi",
cors_preflight=self.cors_options,
default_domain_mapping=apigwv2.DomainMappingOptions(
domain_name=api_domain_name,
),
description="API Gateway aggregating all microservices",
disable_execute_api_endpoint=True,
)
self.http_api.add_vpc_link(
vpc=vpc,
security_groups=[self.security_group],
subnets=ec2.SubnetSelection(subnet_group_name="PrivateSubnet"),
)
stage = self.http_api.add_stage(
id=stage_name,
stage_name=stage_name,
auto_deploy=True,
throttle=apigwv2.ThrottleSettings(
burst_limit=throttling_burst_limit,
rate_limit=throttling_rate_limit,
),
)
cfn_stage: apigwv2.CfnStage = stage.node.default_child
cfn_stage.access_log_settings = apigwv2.CfnStage.AccessLogSettingsProperty(
destination_arn=log_group.log_group_arn,
format=json.dumps(
{
"requestId": "$context.requestId",
"userAgent": "$context.identity.userAgent",
"sourceIp": "$context.identity.sourceIp",
"requestTime": "$context.requestTime",
"requestTimeEpoch": "$context.requestTimeEpoch",
"httpMethod": "$context.httpMethod",
"path": "$context.path",
"status": "$context.status",
"protocol": "$context.protocol",
"responseLength": "$context.responseLength",
"domainName": "$context.domainName",
},
),
)
role = iam.Role(
scope=self,
id="LogsRole",
assumed_by=iam.ServicePrincipal("apigateway.amazonaws.com"),
)
log_group.grant_write(role)
route53.ARecord(
scope=self,
id="ARecord",
zone=self.hosted_zone,
record_name=subdomain_name,
target=route53.RecordTarget.from_alias(
alias_target=route53_targets.ApiGatewayv2DomainProperties(
regional_domain_name=api_domain_name.regional_domain_name,
regional_hosted_zone_id=api_domain_name.regional_hosted_zone_id,
),
),
)
ssm.StringParameter(
scope=self,
id="HttpApiIdParameter",
parameter_name=f"/{service_name}/http-api/id",
string_value=self.http_api.api_id,
)
ssm.StringParameter(
scope=self,
id="HttpApiUrlParameter",
parameter_name=f"/{service_name}/http-api/url",
string_value=f"https://{subdomain_name}.{self.hosted_zone.zone_name}",
)
B1Alarm(
scope=self,
id="ClientErrorAlarm",
subscription_teams=subscription_teams,
alarm_description="Client error",
metric=self.http_api.metric_client_error(period=cdk.Duration.minutes(5), statistic=cw.Stats.SUM),
threshold=client_error_alarm_threshold,
evaluation_periods=3,
datapoints_to_alarm=2,
comparison_operator=cw.ComparisonOperator.GREATER_THAN_THRESHOLD,
)
B1Alarm(
scope=self,
id="ServerErrorAlarm",
subscription_teams=subscription_teams,
alarm_description="Server error",
metric=self.http_api.metric_server_error(period=cdk.Duration.minutes(5), statistic=cw.Stats.SUM),
threshold=server_error_alarm_threshold,
evaluation_periods=3,
datapoints_to_alarm=2,
comparison_operator=cw.ComparisonOperator.GREATER_THAN_THRESHOLD,
)
B1Alarm(
scope=self,
id="LatencyAlarm",
subscription_teams=subscription_teams,
alarm_description=f"Latency is greater than {latency_alarm_threshold_seconds}s",
metric=self.http_api.metric_latency(period=cdk.Duration.minutes(10), statistic=cw.Stats.AVERAGE),
threshold=latency_alarm_threshold_seconds * 1000,
evaluation_periods=5,
datapoints_to_alarm=3,
comparison_operator=cw.ComparisonOperator.GREATER_THAN_THRESHOLD,
)
B1Alarm(
scope=self,
id="IntegrationLatencyAlarm",
subscription_teams=subscription_teams,
alarm_description=f"Integration latency is greater than {latency_alarm_threshold_seconds * 0.8}s",
metric=self.http_api.metric_integration_latency(period=cdk.Duration.minutes(10), statistic=cw.Stats.AVERAGE),
threshold=latency_alarm_threshold_seconds * 1000 * 0.8,
evaluation_periods=5,
datapoints_to_alarm=3,
comparison_operator=cw.ComparisonOperator.GREATER_THAN_THRESHOLD,
)
cdk.CfnOutput(scope=self, id="ApiUrlOutput", value=f"https://{subdomain_name}.{self.hosted_zone.zone_name}")
cdk.CfnOutput(scope=self, id="ApiIdOutput", value=self.http_api.api_id)
cdk.Tags.of(self).add(key="service-name", value=service_name)
def add_lambda_route(self, path: str, handler: lambda_.IFunction) -> list[apigwv2.HttpRoute]:
"""Add a Lambda function as a route to the API Gateway
Args:
----
path (str): The path for the route
handler (lambda_.IFunction): The Lambda function to be called
Returns:
-------
list[apigwv2.HttpRoute]: The routes added to the API Gateway
"""
routes = []
routes.extend(
self.http_api.add_routes(
path=f"/{path}",
methods=[apigwv2.HttpMethod.ANY],
integration=integrations.HttpLambdaIntegration(id=f"{path}", handler=handler),
),
)
routes.extend(
self.http_api.add_routes(
path=f"/{path}/{{proxy+}}",
methods=[apigwv2.HttpMethod.ANY],
integration=integrations.HttpLambdaIntegration(id=f"{path}.proxy", handler=handler),
),
)
return routes