diff --git a/src/constructs/lambda/__snapshots__/lambda.test.ts.snap b/src/constructs/lambda/__snapshots__/lambda.test.ts.snap index 7ff68a2ec..15170802f 100644 --- a/src/constructs/lambda/__snapshots__/lambda.test.ts.snap +++ b/src/constructs/lambda/__snapshots__/lambda.test.ts.snap @@ -84,6 +84,7 @@ Object { "S3Key": "folder/to/key", }, "Handler": "handler.ts", + "MemorySize": 512, "Role": Object { "Fn::GetAtt": Array [ "lambdaServiceRole494E4CA6", @@ -109,6 +110,7 @@ Object { }, }, ], + "Timeout": 30, }, "Type": "AWS::Lambda::Function", }, diff --git a/src/constructs/lambda/lambda.test.ts b/src/constructs/lambda/lambda.test.ts index 09ddc31f4..d34c2a719 100644 --- a/src/constructs/lambda/lambda.test.ts +++ b/src/constructs/lambda/lambda.test.ts @@ -143,4 +143,62 @@ describe("The GuLambdaFunction class", () => { }, }); }); + + it("should use the memorySize provided via props when it is defined", () => { + const stack = simpleGuStackForTesting(); + + new GuLambdaFunction(stack, "lambda", { + code: { bucket: "bucket1", key: "folder/to/key" }, + handler: "handler.ts", + runtime: Runtime.JAVA_8, + memorySize: 2048, + }); + + expect(stack).toHaveResource("AWS::Lambda::Function", { + MemorySize: 2048, + }); + }); + + it("should add a sensible default memorySize if none is provided", () => { + const stack = simpleGuStackForTesting(); + + new GuLambdaFunction(stack, "lambda", { + code: { bucket: "bucket1", key: "folder/to/key" }, + handler: "handler.ts", + runtime: Runtime.JAVA_8, + }); + + expect(stack).toHaveResource("AWS::Lambda::Function", { + MemorySize: 1024, + }); + }); + + it("should use the timeout provided via props when it is defined", () => { + const stack = simpleGuStackForTesting(); + + new GuLambdaFunction(stack, "lambda", { + code: { bucket: "bucket1", key: "folder/to/key" }, + handler: "handler.ts", + runtime: Runtime.JAVA_8, + timeout: Duration.seconds(60), + }); + + expect(stack).toHaveResource("AWS::Lambda::Function", { + Timeout: 60, + }); + }); + + it("should add a sensible default timeout if none is provided", () => { + const stack = simpleGuStackForTesting(); + + new GuLambdaFunction(stack, "lambda", { + code: { bucket: "bucket1", key: "folder/to/key" }, + handler: "handler.ts", + runtime: Runtime.JAVA_8, + }); + + expect(stack).toHaveResource("AWS::Lambda::Function", { + Timeout: 30, + }); + }); }); diff --git a/src/constructs/lambda/lambda.ts b/src/constructs/lambda/lambda.ts index ef0f2977a..e1e74efae 100644 --- a/src/constructs/lambda/lambda.ts +++ b/src/constructs/lambda/lambda.ts @@ -3,9 +3,10 @@ import { LambdaRestApi } from "@aws-cdk/aws-apigateway"; import type { Schedule } from "@aws-cdk/aws-events"; import { Rule } from "@aws-cdk/aws-events"; import { LambdaFunction } from "@aws-cdk/aws-events-targets"; -import { Code, Function } from "@aws-cdk/aws-lambda"; -import type { FunctionProps } from "@aws-cdk/aws-lambda"; +import { Code, Function, RuntimeFamily } from "@aws-cdk/aws-lambda"; +import type { FunctionProps, Runtime } from "@aws-cdk/aws-lambda"; import { Bucket } from "@aws-cdk/aws-s3"; +import { Duration } from "@aws-cdk/core"; import type { GuStack } from "../core"; interface ApiProps extends Omit { @@ -21,12 +22,27 @@ interface GuFunctionProps extends Omit { apis?: ApiProps[]; } +function defaultMemorySize(runtime: Runtime, memorySize?: number): number { + if (memorySize) { + return memorySize; + } else { + switch (runtime.family) { + case RuntimeFamily.JAVA: + return 1024; + default: + return 512; + } + } +} + export class GuLambdaFunction extends Function { constructor(scope: GuStack, id: string, props: GuFunctionProps) { const bucket = Bucket.fromBucketName(scope, `${id}-bucket`, props.code.bucket); const code = Code.fromBucket(bucket, props.code.key); super(scope, id, { ...props, + memorySize: defaultMemorySize(props.runtime, props.memorySize), + timeout: props.timeout ?? Duration.seconds(30), code, });