/
lambda.ts
76 lines (66 loc) · 2.25 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
75
76
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import { Lazy } from '@aws-cdk/core';
import { IntegrationOptions } from '../integration';
import { Method } from '../method';
import { AwsIntegration } from './aws';
export interface LambdaIntegrationOptions extends IntegrationOptions {
/**
* Use proxy integration or normal (request/response mapping) integration.
* @default true
*/
readonly proxy?: boolean;
/**
* Allow invoking method from AWS Console UI (for testing purposes).
*
* This will add another permission to the AWS Lambda resource policy which
* will allow the `test-invoke-stage` stage to invoke this handler. If this
* is set to `false`, the function will only be usable from the deployment
* endpoint.
*
* @default true
*/
readonly allowTestInvoke?: boolean;
}
/**
* Integrates an AWS Lambda function to an API Gateway method.
*
* @example
*
* const handler = new lambda.Function(this, 'MyFunction', ...);
* api.addMethod('GET', new LambdaIntegration(handler));
*
*/
export class LambdaIntegration extends AwsIntegration {
private readonly handler: lambda.IFunction;
private readonly enableTest: boolean;
constructor(handler: lambda.IFunction, options: LambdaIntegrationOptions = { }) {
const proxy = options.proxy === undefined ? true : options.proxy;
super({
proxy,
service: 'lambda',
path: `2015-03-31/functions/${handler.functionArn}/invocations`,
options,
});
this.handler = handler;
this.enableTest = options.allowTestInvoke === undefined ? true : false;
}
public bind(method: Method) {
super.bind(method);
const principal = new iam.ServicePrincipal('apigateway.amazonaws.com');
const desc = `${method.api.node.uniqueId}.${method.httpMethod}.${method.resource.path.replace(/\//g, '.')}`;
this.handler.addPermission(`ApiPermission.${desc}`, {
principal,
scope: method,
sourceArn: Lazy.stringValue({ produce: () => method.methodArn }),
});
// add permission to invoke from the console
if (this.enableTest) {
this.handler.addPermission(`ApiPermission.Test.${desc}`, {
principal,
scope: method,
sourceArn: method.testMethodArn,
});
}
}
}