Skip to content

Commit

Permalink
feat(lambda): reserved concurrent executions (#1560)
Browse files Browse the repository at this point in the history
feat(lambda): reserved concurrent executions
  • Loading branch information
Sam Goodwin committed Jan 17, 2019
1 parent 3270b47 commit f7469c1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,18 @@ const fn = new lambda.Function(this, 'MyFunction', {
```
See [the AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html)
to learn more about AWS Lambda's X-Ray support.

### Lambda with Reserved Concurrent Executions

```ts
import lambda = require('@aws-cdk/aws-lambda');

const fn = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.NodeJS810,
handler: 'index.handler',
code: lambda.Code.inline('exports.handler = function(event, ctx, cb) { return cb(null, "hi"); }'),
reservedConcurrentExecutions: 100
});
```
See [the AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html)
managing concurrency.
11 changes: 10 additions & 1 deletion packages/@aws-cdk/aws-lambda/lib/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ export interface FunctionProps {
* @default undefined X-Ray tracing disabled
*/
tracing?: Tracing;

/**
* The maximum of concurrent executions you want to reserve for the function.
*
* @default no specific limit - account limit
* @see https://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html
*/
reservedConcurrentExecutions?: number;
}

/**
Expand Down Expand Up @@ -339,7 +347,8 @@ export class Function extends FunctionBase {
memorySize: props.memorySize,
vpcConfig: this.configureVpc(props),
deadLetterConfig: this.buildDeadLetterConfig(props),
tracingConfig: this.buildTracingConfig(props)
tracingConfig: this.buildTracingConfig(props),
reservedConcurrentExecutions: props.reservedConcurrentExecutions
});

resource.addDependency(this.role);
Expand Down
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/test.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,42 @@ export = {
Runtime: 'ruby2.5' },
DependsOn: [ 'MyLambdaServiceRole4539ECB6' ] } } });
test.done();
},
'support reserved concurrent executions'(test: Test) {
const stack = new cdk.Stack();

new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NodeJS,
reservedConcurrentExecutions: 10
});

expect(stack).toMatch({ Resources:
{ MyLambdaServiceRole4539ECB6:
{ Type: 'AWS::IAM::Role',
Properties:
{ AssumeRolePolicyDocument:
{ Statement:
[ { Action: 'sts:AssumeRole',
Effect: 'Allow',
Principal: { Service: 'lambda.amazonaws.com' } } ],
Version: '2012-10-17' },
ManagedPolicyArns:
// arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
// tslint:disable-next-line:max-line-length
[{'Fn::Join': ['', ['arn:', {Ref: 'AWS::Partition'}, ':iam::aws:policy/service-role/AWSLambdaBasicExecutionRole']]}],
}},
MyLambdaCCE802FB:
{ Type: 'AWS::Lambda::Function',
Properties:
{ Code: { ZipFile: 'foo' },
Handler: 'index.handler',
ReservedConcurrentExecutions: 10,
Role: { 'Fn::GetAtt': [ 'MyLambdaServiceRole4539ECB6', 'Arn' ] },
Runtime: 'nodejs' },
DependsOn: [ 'MyLambdaServiceRole4539ECB6' ] } } });
test.done();
}
};

Expand Down

0 comments on commit f7469c1

Please sign in to comment.