Skip to content

Commit

Permalink
Add a more sensible default for timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwinch committed Jan 21, 2021
1 parent 74a8f85 commit e186ea8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/constructs/lambda/__snapshots__/lambda.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Object {
},
},
],
"Timeout": 30,
},
"Type": "AWS::Lambda::Function",
},
Expand Down
29 changes: 29 additions & 0 deletions src/constructs/lambda/lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,33 @@ describe("The GuLambdaFunction class", () => {
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,
});
});
});
4 changes: 3 additions & 1 deletion src/constructs/lambda/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { LambdaFunction } from "@aws-cdk/aws-events-targets";
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 Down Expand Up @@ -38,10 +39,11 @@ 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);
const { memorySize, ...rest } = props;
const { memorySize, timeout, ...rest } = props;
super(scope, id, {
...rest,
memorySize: defaultMemorySize(props.runtime, memorySize),
timeout: props.timeout ? props.timeout : Duration.seconds(30),
code,
});

Expand Down

0 comments on commit e186ea8

Please sign in to comment.