Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export default () => ({

// optional
memorySize: 1769, // default: `128` MB
lambdaRuntime: `22.x`, // default `20.x`
timeoutInSeconds: 3, // default: `28` seconds (this is the maximum timeout)
environment: { FOO: `bar` },
requestParameters: { foo: {}, bar: { cacheKey: true, required: true } },
Expand Down
5 changes: 4 additions & 1 deletion src/cdk/create-lambda-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Stack } from 'aws-cdk-lib';
import { getDomainName } from '../utils/get-domain-name.js';
import { getHash } from '../utils/get-hash.js';
import { getNormalizedName } from '../utils/get-normalized-name.js';
import { mapLambdaRuntime } from '../utils/lambda-runtime.js';
import { Duration, aws_ec2, aws_efs, aws_iam, aws_lambda, aws_logs } from 'aws-cdk-lib';
import { basename, dirname, extname, join } from 'path';

Expand Down Expand Up @@ -84,6 +85,8 @@ export function createLambdaFunction(
}
: undefined;

const runtime = mapLambdaRuntime(route.lambdaRuntime);

const fn = new aws_lambda.Function(stack, `Function${getHash(uniqueFunctionName)}`, {
functionName: uniqueFunctionName,
code: aws_lambda.Code.fromAsset(dirname(path)),
Expand All @@ -92,7 +95,7 @@ export function createLambdaFunction(
memorySize,
environment,
timeout: Duration.seconds(timeoutInSeconds),
runtime: aws_lambda.Runtime.NODEJS_20_X,
runtime,
tracing: aws_lambda.Tracing.PASS_THROUGH,
insightsVersion:
monitoring === true || monitoring?.lambdaInsightsEnabled
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/create-request-authorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function createRequestAuthorizer(
USERNAME: authentication.username,
PASSWORD: authentication.password,
},
runtime: aws_lambda.Runtime.NODEJS_18_X,
runtime: aws_lambda.Runtime.NODEJS_22_X,
tracing: aws_lambda.Tracing.PASS_THROUGH,
logRetention: aws_logs.RetentionDays.TWO_WEEKS,
}),
Expand Down
4 changes: 4 additions & 0 deletions src/parse-stack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ export type LambdaRoute = Omit<z.TypeOf<typeof LambdaRouteSchema>, 'onSynthesize

export type S3Route = z.TypeOf<typeof S3RouteSchema>;

const LambdaRuntimeSchema = z.enum([`20.x`, `22.x`, `LATEST`]).optional();
export type LambdaRuntime = z.TypeOf<typeof LambdaRuntimeSchema>;

const LambdaRouteSchema = z.object({
type: z.literal(`function`),
httpMethod: z.enum([`DELETE`, `GET`, `HEAD`, `PATCH`, `POST`, `PUT`]),
publicPath: z.string(),
path: z.string(),
functionName: z.string(),
memorySize: z.number().optional(),
lambdaRuntime: LambdaRuntimeSchema,
timeoutInSeconds: z.number().int().min(0).max(28).optional(),
environment: z.record(z.string()).optional(),
requestParameters: z
Expand Down
12 changes: 12 additions & 0 deletions src/utils/lambda-runtime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { mapLambdaRuntime } from './lambda-runtime.js';
import { describe, expect, test } from '@jest/globals';
import { aws_lambda } from 'aws-cdk-lib';

describe(`lambda runtime utils`, () => {
test(`map runtime`, () => {
expect(mapLambdaRuntime(undefined)).toEqual(aws_lambda.Runtime.NODEJS_20_X);
expect(mapLambdaRuntime(`20.x`)).toEqual(aws_lambda.Runtime.NODEJS_20_X);
expect(mapLambdaRuntime(`22.x`)).toEqual(aws_lambda.Runtime.NODEJS_22_X);
expect(mapLambdaRuntime(`LATEST`)).toEqual(aws_lambda.Runtime.NODEJS_LATEST);
});
});
23 changes: 23 additions & 0 deletions src/utils/lambda-runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { LambdaRuntime } from '../parse-stack-config.js';

import { aws_lambda } from 'aws-cdk-lib';

/**
* Maps the lambda runtime config enum to CDK equivalents.
*
* @param lambdaRuntime the lambda config string or undefined
* @returns the CDK runtime constant.
*/
export function mapLambdaRuntime(lambdaRuntime: LambdaRuntime): aws_lambda.Runtime {
if (!lambdaRuntime) {
return aws_lambda.Runtime.NODEJS_20_X;
}
switch (lambdaRuntime) {
case `20.x`:
return aws_lambda.Runtime.NODEJS_20_X;
case `22.x`:
return aws_lambda.Runtime.NODEJS_22_X;
case `LATEST`:
return aws_lambda.Runtime.NODEJS_LATEST;
}
}