Skip to content

Commit 87fd60f

Browse files
authoredNov 26, 2021
feat(lambda): function construct exposes configured timeout (#17594)
Supersedes #17000. I didn't realise that the "allow edits by maintainers" was not supported under organisation accounts, so I've forked under my account instead. Otherwise, these changes are the same as the linked PR. Thanks! 👍 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 3b5b97a commit 87fd60f

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
 

‎packages/@aws-cdk/aws-lambda/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,38 @@ myRole.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName("service-role
119119
myRole.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSLambdaVPCAccessExecutionRole")); // only required if your function lives in a VPC
120120
```
121121

122+
## Function Timeout
123+
124+
AWS Lambda functions have a default timeout of 3 seconds, but this can be increased
125+
up to 15 minutes. The timeout is available as a property of `Function` so that
126+
you can reference it elsewhere in your stack. For instance, you could use it to create
127+
a CloudWatch alarm to report when your function timed out:
128+
129+
```ts
130+
import * as cdk from '@aws-cdk/core';
131+
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
132+
133+
const fn = new lambda.Function(this, 'MyFunction', {
134+
runtime: lambda.Runtime.NODEJS_12_X,
135+
handler: 'index.handler',
136+
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
137+
timeout: cdk.Duration.minutes(5),
138+
});
139+
140+
if (fn.timeout) {
141+
new cloudwatch.Alarm(this, `MyAlarm`, {
142+
metric: fn.metricDuration().with({
143+
statistic: 'Maximum',
144+
}),
145+
evaluationPeriods: 1,
146+
datapointsToAlarm: 1,
147+
threshold: fn.timeout.toMilliseconds(),
148+
treatMissingData: cloudwatch.TreatMissingData.IGNORE,
149+
alarmName: 'My Lambda Timeout',
150+
});
151+
}
152+
```
153+
122154
## Resource-based Policies
123155

124156
AWS Lambda supports resource-based policies for controlling access to Lambda

‎packages/@aws-cdk/aws-lambda/lib/function.ts

+7
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,12 @@ export class Function extends FunctionBase {
577577
* The architecture of this Lambda Function (this is an optional attribute and defaults to X86_64).
578578
*/
579579
public readonly architecture?: Architecture;
580+
581+
/**
582+
* The timeout configured for this lambda.
583+
*/
584+
public readonly timeout?: Duration;
585+
580586
public readonly permissionsNode = this.node;
581587

582588

@@ -725,6 +731,7 @@ export class Function extends FunctionBase {
725731
});
726732

727733
this.runtime = props.runtime;
734+
this.timeout = props.timeout;
728735

729736
this.architecture = props.architecture;
730737

‎packages/@aws-cdk/aws-lambda/test/function.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,39 @@ describe('function', () => {
18421842
});
18431843
});
18441844

1845+
describe('lambda.Function timeout', () => {
1846+
test('should be a cdk.Duration when defined', () => {
1847+
// GIVEN
1848+
const stack = new cdk.Stack();
1849+
1850+
// WHEN
1851+
const { timeout } = new lambda.Function(stack, 'MyFunction', {
1852+
handler: 'foo',
1853+
runtime: lambda.Runtime.NODEJS_12_X,
1854+
code: lambda.Code.fromAsset(path.join(__dirname, 'handler.zip')),
1855+
timeout: cdk.Duration.minutes(2),
1856+
});
1857+
1858+
// THEN
1859+
expect(timeout).toEqual(cdk.Duration.minutes(2));
1860+
});
1861+
1862+
test('should be optional', () => {
1863+
// GIVEN
1864+
const stack = new cdk.Stack();
1865+
1866+
// WHEN
1867+
const { timeout } = new lambda.Function(stack, 'MyFunction', {
1868+
handler: 'foo',
1869+
runtime: lambda.Runtime.NODEJS_12_X,
1870+
code: lambda.Code.fromAsset(path.join(__dirname, 'handler.zip')),
1871+
});
1872+
1873+
// THEN
1874+
expect(timeout).not.toBeDefined();
1875+
});
1876+
});
1877+
18451878
describe('currentVersion', () => {
18461879
// see test.function-hash.ts for more coverage for this
18471880
test('logical id of version is based on the function hash', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.