-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathlambda.ts
99 lines (86 loc) · 3.08 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import { Lazy, Names, Token } from '@aws-cdk/core';
import { IntegrationConfig, 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.
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
*
* @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
*
* declare const resource: apigateway.Resource;
* declare const handler: lambda.Function;
* resource.addMethod('GET', new apigateway.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 ?? true;
super({
proxy,
service: 'lambda',
path: `2015-03-31/functions/${handler.functionArn}/invocations`,
options,
});
this.handler = handler;
this.enableTest = options.allowTestInvoke ?? true;
}
public bind(method: Method): IntegrationConfig {
const bindResult = super.bind(method);
const principal = new iam.ServicePrincipal('apigateway.amazonaws.com');
const desc = `${Names.nodeUniqueId(method.api.node)}.${method.httpMethod}.${method.resource.path.replace(/\//g, '.')}`;
this.handler.addPermission(`ApiPermission.${desc}`, {
principal,
scope: method,
sourceArn: Lazy.string({ 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,
});
}
let functionName;
if (this.handler instanceof lambda.Function) {
// if not imported, extract the name from the CFN layer to reach
// the literal value if it is given (rather than a token)
functionName = (this.handler.node.defaultChild as lambda.CfnFunction).functionName;
} else {
// imported, just take the function name.
functionName = this.handler.functionName;
}
let deploymentToken;
if (!Token.isUnresolved(functionName)) {
deploymentToken = JSON.stringify({ functionName });
}
return {
...bindResult,
deploymentToken,
};
}
}