/
lambda.ts
74 lines (66 loc) · 2.35 KB
/
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
import {
HttpIntegrationType,
HttpRouteIntegrationBindOptions,
HttpRouteIntegrationConfig,
HttpRouteIntegration,
PayloadFormatVersion,
ParameterMapping,
} from '../../../aws-apigatewayv2';
import { ServicePrincipal } from '../../../aws-iam';
import { IFunction } from '../../../aws-lambda';
import { Stack } from '../../../core';
/**
* Lambda Proxy integration properties
*/
export interface HttpLambdaIntegrationProps {
/**
* Version of the payload sent to the lambda handler.
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
* @default PayloadFormatVersion.VERSION_2_0
*/
readonly payloadFormatVersion?: PayloadFormatVersion;
/**
* Specifies how to transform HTTP requests before sending them to the backend
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-parameter-mapping.html
* @default undefined requests are sent to the backend unmodified
*/
readonly parameterMapping?: ParameterMapping;
}
/**
* The Lambda Proxy integration resource for HTTP API
*/
export class HttpLambdaIntegration extends HttpRouteIntegration {
private readonly _id: string;
/**
* @param id id of the underlying integration construct
* @param handler the Lambda handler to integrate with
* @param props properties to configure the integration
*/
constructor(
id: string,
private readonly handler: IFunction,
private readonly props: HttpLambdaIntegrationProps = {}) {
super(id);
this._id = id;
}
protected completeBind(options: HttpRouteIntegrationBindOptions) {
const route = options.route;
this.handler.addPermission(`${this._id}-Permission`, {
scope: options.scope,
principal: new ServicePrincipal('apigateway.amazonaws.com'),
sourceArn: Stack.of(route).formatArn({
service: 'execute-api',
resource: route.httpApi.apiId,
resourceName: `*/*${route.path ?? ''}`, // empty string in the case of the catch-all route $default
}),
});
}
public bind(_options: HttpRouteIntegrationBindOptions): HttpRouteIntegrationConfig {
return {
type: HttpIntegrationType.AWS_PROXY,
uri: this.handler.functionArn,
payloadFormatVersion: this.props.payloadFormatVersion ?? PayloadFormatVersion.VERSION_2_0,
parameterMapping: this.props.parameterMapping,
};
}
}