Skip to content

Commit

Permalink
Merge pull request #188 from guardian/jw-provide-sensible-defaults
Browse files Browse the repository at this point in the history
Provide sensible defaults for GuLambda memorySize and timeout
  • Loading branch information
jacobwinch authored Jan 21, 2021
2 parents dc7c899 + 1a6477e commit 9f929ef
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/constructs/lambda/__snapshots__/lambda.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Object {
"S3Key": "folder/to/key",
},
"Handler": "handler.ts",
"MemorySize": 512,
"Role": Object {
"Fn::GetAtt": Array [
"lambdaServiceRole494E4CA6",
Expand All @@ -109,6 +110,7 @@ Object {
},
},
],
"Timeout": 30,
},
"Type": "AWS::Lambda::Function",
},
Expand Down
58 changes: 58 additions & 0 deletions src/constructs/lambda/lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
});
20 changes: 18 additions & 2 deletions src/constructs/lambda/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LambdaRestApiProps, "handler"> {
Expand All @@ -21,12 +22,27 @@ interface GuFunctionProps extends Omit<FunctionProps, "code"> {
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,
});

Expand Down

0 comments on commit 9f929ef

Please sign in to comment.