-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodejsLambdaConstruct.ts
More file actions
268 lines (237 loc) · 7.57 KB
/
NodejsLambdaConstruct.ts
File metadata and controls
268 lines (237 loc) · 7.57 KB
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
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import { Construct } from "constructs";
import * as iam from "aws-cdk-lib/aws-iam";
import * as nodejs from "aws-cdk-lib/aws-lambda-nodejs";
import { OutputFormat } from "aws-cdk-lib/aws-lambda-nodejs";
import { Duration } from "aws-cdk-lib";
import { APIGatewayWithCognitoUserPoolConstruct } from "./APIGatewayWithCognitoUserPoolConstruct";
/**
* Defines API Gateway integration details.
*/
export interface ApiGwIntegrationProps {
/**
* The API Gateway instance to attach this Lambda function to.
*/
apiGateway: APIGatewayWithCognitoUserPoolConstruct;
/**
* The API route to be associated with the Lambda function.
*/
route: string;
/**
* The HTTP method for the API route.
*/
method: APIMethodsEnum;
/**
* Whether the API route should be protected by an API Gateway authorizer.
*/
isProtected?: boolean;
}
/***
* Enum for API Gateway HTTP methods.
*/
export enum APIMethodsEnum {
GET = "GET",
POST = "POST",
PUT = "PUT",
DELETE = "DELETE",
}
/**
* Enum for DynamoDB permissions.
*/
export enum DynamoDBPermissions {
/* Read Operations */
QUERY = "dynamodb:Query",
SCAN = "dynamodb:Scan",
GET_ITEM = "dynamodb:GetItem",
BATCH_GET_ITEM = "dynamodb:BatchGetItem",
/* Write Operations */
PUT_ITEM = "dynamodb:PutItem", // Insert a new item
UPDATE_ITEM = "dynamodb:UpdateItem", // Modify an existing item
DELETE_ITEM = "dynamodb:DeleteItem", // Remove an item
BATCH_WRITE_ITEM = "dynamodb:BatchWriteItem", // Write multiple items at once
}
/**
* Properties for configuring the LambdaConstruct.
*/
export interface NodejsLambdaConstructProps {
/**
* The application name used as a prefix in resource names.
*/
appName: string;
/**
* The name for the Lambda function.
*/
lambdaName: string;
/**
* The VPC where the Lambda function will be deployed.
*/
vpc: ec2.IVpc;
/**
* The VPC subnets for the Lambda function.
*/
vpcSubnets: ec2.SubnetSelection;
/**
* The directory path to the entry file for the Lambda function.
*/
entry: string;
/**
* The timeout for the Lambda function (default: 30 seconds).
*/
timeout?: Duration;
/**
* Additional permissions required by the Lambda function (optional).
*/
permissions?: DynamoDBPermissions[];
/**
* VPC endpoints that the Lambda function can access (optional).
*/
vpcEndpoints?: (ec2.InterfaceVpcEndpoint | ec2.GatewayVpcEndpoint)[];
/**
* Lambda layers to include in the Lambda function (optional).
*/
layers?: lambda.ILayerVersion[];
/**
* Node modules to bundle with the Lambda function (optional).
*/
nodeModules?: string[];
/**
* External modules to exclude from bundling in the Lambda function (optional).
*/
externalModules?: string[];
/**
* Environment variables to pass to the Lambda function (optional).
*/
envVariables?: { [key: string]: string };
/**
* Optional API Gateway integration settings.
*/
apiGwIntegration?: ApiGwIntegrationProps;
}
/**
* Creates a Lambda function with optional API Gateway integration.
*
* If `apiGwIntegration` is provided, the Lambda function will be automatically
* registered as an API Gateway route.
*
* @example
* // Example: Using Lambda with API Gateway
* const lambdaWithApi = new LambdaConstruct(this, 'ApiLambda', {
* appName: 'myApp',
* lambdaName: 'myApiLambda',
* vpc: myVpc,
* vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE },
* entry: 'index.ts',
* apiGwIntegration: {
* apiGateway: myApiGateway,
* route: '/projects',
* method: APIMethodsEnum.GET,
* isProtected: true,
* },
* });
*
* @example
* // Example: Using Lambda without API Gateway (e.g., for SQS/EventBridge)
* const lambdaWithoutApi = new LambdaConstruct(this, 'SqsLambda', {
* appName: 'myApp',
* lambdaName: 'mySqsLambda',
* vpc: myVpc,
* vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE },
* entry: 'sqs-handler.ts',
* });
*/
export class NodejsLambdaConstruct extends Construct {
/**
* The created Lambda function resource.
*/
public readonly lambdaFunction: lambda.Function;
/**
* Constructs a new instance of the LambdaConstruct with optional API Gateway integration.
*
* @param {Construct} scope - The parent construct, typically a CDK stack.
* @param {string} id - The unique identifier for this construct.
* @param {NodejsLambdaConstructProps} props - Properties for configuring the Lambda function and API Gateway integration.
*/
constructor(scope: Construct, id: string, props: NodejsLambdaConstructProps) {
super(scope, id);
const depsLockFilePath = props.entry.replace(/\/[^/]+\.ts$/, "/package-lock.json");
/* Create the Lambda function with configurable entry file and settings */
this.lambdaFunction = new nodejs.NodejsFunction(this, props.lambdaName, {
functionName: `${props.lambdaName}`,
vpc: props.vpc,
vpcSubnets: props.vpcSubnets,
runtime: lambda.Runtime.NODEJS_20_X,
architecture: lambda.Architecture.ARM_64,
depsLockFilePath: depsLockFilePath,
memorySize: 512,
entry: props.entry,
tracing: lambda.Tracing.ACTIVE,
timeout: props.timeout || Duration.seconds(30),
logRetention: 14,
environment: props.envVariables || undefined,
bundling: {
sourceMap: false,
nodeModules: props.nodeModules || [],
externalModules: props.externalModules || ["@aws-sdk/*", "aws-lambda"],
format: OutputFormat.ESM,
},
description: `Lambda function for ${props.lambdaName}`,
});
/* Attach additional layers to the Lambda function if provided */
if (props.layers) {
props.layers.forEach((layer) => {
this.lambdaFunction.addLayers(layer);
});
}
/* Grant the required IAM permissions to the Lambda function */
if (props.permissions) {
this.addPermissions(props.permissions);
}
/* Configure VPC endpoints if specified */
if (props.vpcEndpoints) {
this.configureVpcEndpoints(props.vpcEndpoints);
}
/* Handle API Gateway integration if specified */
if (props.apiGwIntegration) {
this.lambdaFunction.addPermission("ApiGatewayInvoke", {
principal: new iam.ServicePrincipal("apigateway.amazonaws.com"),
});
/* Automatically register the API Gateway route */
props.apiGwIntegration.apiGateway.addMethod(
props.apiGwIntegration.route,
props.apiGwIntegration.method,
this.lambdaFunction,
props.apiGwIntegration.isProtected ?? false
);
}
}
/**
* Adds additional permissions to the Lambda function's IAM role.
*
* @param {string[]} permissions - A list of actions the Lambda function is allowed to perform.
*/
private addPermissions(permissions: string[]) {
this.lambdaFunction.addToRolePolicy(
new iam.PolicyStatement({
actions: permissions,
resources: ["*"],
})
);
}
/**
* Configures VPC endpoints for the Lambda function to connect to specified endpoints.
*
* @param {(ec2.InterfaceVpcEndpoint | ec2.GatewayVpcEndpoint)[]} vpcEndpoints - Array of VPC endpoints to allow connections.
*/
private configureVpcEndpoints(vpcEndpoints: (ec2.InterfaceVpcEndpoint | ec2.GatewayVpcEndpoint)[]) {
vpcEndpoints.forEach((endpoint) => {
if (endpoint instanceof ec2.InterfaceVpcEndpoint) {
this.lambdaFunction.connections.allowTo(
endpoint,
ec2.Port.tcp(443),
`Allow Lambda to connect to VPC Interface Endpoint ${endpoint.vpcEndpointId}`
);
}
});
}
}