-
Notifications
You must be signed in to change notification settings - Fork 5
/
api-lambda.ts
90 lines (81 loc) · 3.1 KB
/
api-lambda.ts
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
import { LambdaRestApi } from "aws-cdk-lib/aws-apigateway";
import type { LambdaRestApiProps } from "aws-cdk-lib/aws-apigateway";
import type { NoMonitoring } from "../constructs/cloudwatch";
import { GuApiGateway5xxPercentageAlarm } from "../constructs/cloudwatch/api-gateway-alarms";
import type { GuStack } from "../constructs/core";
import { GuLambdaFunction } from "../constructs/lambda";
import type { GuFunctionProps } from "../constructs/lambda";
import type { ApiGatewayAlarms } from "./api-multiple-lambdas";
interface ApiProps extends Omit<LambdaRestApiProps, "handler"> {
id: string;
}
export interface GuApiLambdaProps extends Omit<GuFunctionProps, "errorPercentageMonitoring"> {
/**
* [[`LambdaRestApiProps`]] to configure for the lambda.
*/
api: ApiProps;
/**
* Alarm configuration for your API. For more details, see [[`ApiGatewayAlarms`]].
*
* If your team do not use CloudWatch, it's possible to opt-out with the following configuration:
* ```typescript
* monitoringConfiguration: { noMonitoring: true }
* ```
*/
monitoringConfiguration: NoMonitoring | ApiGatewayAlarms;
}
/**
* A pattern to create a Lambda triggered by API Gateway
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-with-lambda-integration.html
*
* This pattern should be used if you intend to serve all traffic via a single Lambda
* (for example, if your Lambda uses an application framework, like https://github.com/vendia/serverless-express).
* If you need to configure path-based routing to serve different requests with different
* Lambdas, use the [[`GuApiGatewayWithLambdaByPath`]] pattern instead.
*
* For all configuration options, see [[`GuApiLambdaProps`]].
*
* Example usage:
*
* ```typescript
* new GuApiLambda(stack, "my-lambda", {
* fileName: "my-app.zip",
* handler: "handler.ts",
* runtime: Runtime.NODEJS_14_X,
* monitoringConfiguration: {
* http5xxAlarm: { tolerated5xxPercentage: 5 },
* snsTopicName: "alerts-topic",
* },
* app: "my-app",
* api: {
* id: "my-api",
* description: "...",
* },
* });
* ```
*/
export class GuApiLambda extends GuLambdaFunction {
public readonly api: LambdaRestApi;
constructor(scope: GuStack, id: string, props: GuApiLambdaProps) {
super(scope, id, {
...props,
});
// If we have an alias, use this to ensure that all requests are routed to a published Lambda version.
// Otherwise, use the latest unpublished version ($LATEST)
const resourceToInvoke = this.alias ?? this;
this.api = new LambdaRestApi(this, props.api.id, {
handler: resourceToInvoke,
// Override to avoid clashes as default is just api ID, which is often shared across stages.
restApiName: `${scope.stack}-${scope.stage}-${props.api.id}`,
...props.api,
});
if (!props.monitoringConfiguration.noMonitoring) {
new GuApiGateway5xxPercentageAlarm(scope, {
app: props.app,
apiGatewayInstance: this.api,
snsTopicName: props.monitoringConfiguration.snsTopicName,
...props.monitoringConfiguration.http5xxAlarm,
});
}
}
}