From f7469c111a97853f6cc3169f7a7ce0396ebad4aa Mon Sep 17 00:00:00 2001 From: Sam Goodwin Date: Wed, 16 Jan 2019 23:50:25 -0800 Subject: [PATCH] feat(lambda): reserved concurrent executions (#1560) feat(lambda): reserved concurrent executions --- packages/@aws-cdk/aws-lambda/README.md | 15 ++++++++ packages/@aws-cdk/aws-lambda/lib/lambda.ts | 11 +++++- .../@aws-cdk/aws-lambda/test/test.lambda.ts | 36 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-lambda/README.md b/packages/@aws-cdk/aws-lambda/README.md index ac5afe6b50df2..7e7c7ad76ad66 100644 --- a/packages/@aws-cdk/aws-lambda/README.md +++ b/packages/@aws-cdk/aws-lambda/README.md @@ -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. diff --git a/packages/@aws-cdk/aws-lambda/lib/lambda.ts b/packages/@aws-cdk/aws-lambda/lib/lambda.ts index 32042863c4d43..c3ae60f81953d 100644 --- a/packages/@aws-cdk/aws-lambda/lib/lambda.ts +++ b/packages/@aws-cdk/aws-lambda/lib/lambda.ts @@ -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; } /** @@ -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); diff --git a/packages/@aws-cdk/aws-lambda/test/test.lambda.ts b/packages/@aws-cdk/aws-lambda/test/test.lambda.ts index c74dde7129541..6e99777ce4c84 100644 --- a/packages/@aws-cdk/aws-lambda/test/test.lambda.ts +++ b/packages/@aws-cdk/aws-lambda/test/test.lambda.ts @@ -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(); } };