Skip to content

Commit

Permalink
Merge pull request #7043 from serverless/lambda-provisioned-concurrency
Browse files Browse the repository at this point in the history
Support lambda provisioned concurrency
  • Loading branch information
medikoo committed Dec 4, 2019
2 parents e080dd2 + 0347b15 commit a45d2a2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/providers/aws/guide/functions.md
Expand Up @@ -41,6 +41,7 @@ functions:
runtime: python2.7 # optional overwrite, default is provider runtime
memorySize: 512 # optional, in MB, default is 1024
timeout: 10 # optional, in seconds, default is 6
provisionedConcurrency: 3 # optional, Count of provisioned lambda instances
reservedConcurrency: 5 # optional, reserved concurrency limit for this function. By default, AWS uses account concurrency limit
tracing: PassThrough # optional, overwrite, can be 'Active' or 'PassThrough'
```
Expand Down
1 change: 1 addition & 0 deletions docs/providers/aws/guide/serverless.yml.md
Expand Up @@ -165,6 +165,7 @@ functions:
description: My function # The description of your function.
memorySize: 512 # memorySize for this specific function.
reservedConcurrency: 5 # optional, reserved concurrency limit for this function. By default, AWS uses account concurrency limit
provisionedConcurrency: 3 # optional, Count of provisioned lambda instances
runtime: nodejs12.x # Runtime for this specific function. Overrides the default which is set on the provider level
timeout: 10 # Timeout for this specific function. Overrides the default set above.
role: arn:aws:iam::XXXXXX:role/role # IAM role which will be used for this function
Expand Down
17 changes: 17 additions & 0 deletions lib/plugins/aws/package/compile/functions/index.js
Expand Up @@ -409,6 +409,23 @@ class AwsCompileFunctions {
}
}

if (functionObject.provisionedConcurrency) {
const provisionedConcurrency = _.parseInt(functionObject.provisionedConcurrency);

if (_.isInteger(provisionedConcurrency)) {
newFunction.Properties.ProvisionedConcurrencyConfig = {
ProvisionedConcurrentExecutions: provisionedConcurrency,
};
} else {
return BbPromise.reject(
new this.serverless.classes.Error(
'You should use integer as provisionedConcurrency value on function: ' +
`${newFunction.Properties.FunctionName}`
)
);
}
}

newFunction.DependsOn = [this.provider.naming.getLogGroupLogicalId(functionName)].concat(
newFunction.DependsOn || []
);
Expand Down
38 changes: 38 additions & 0 deletions lib/plugins/aws/package/compile/functions/index.test.js
Expand Up @@ -2200,6 +2200,44 @@ describe('AwsCompileFunctions', () => {
});
});

it('should set function declared provisioned concurrency limit', () => {
const s3Folder = awsCompileFunctions.serverless.service.package.artifactDirectoryName;
const s3FileName = awsCompileFunctions.serverless.service.package.artifact
.split(path.sep)
.pop();
awsCompileFunctions.serverless.service.functions = {
func: {
handler: 'func.function.handler',
name: 'new-service-dev-func',
provisionedConcurrency: 5,
},
};
const compiledFunction = {
Type: 'AWS::Lambda::Function',
DependsOn: ['FuncLogGroup', 'IamRoleLambdaExecution'],
Properties: {
Code: {
S3Key: `${s3Folder}/${s3FileName}`,
S3Bucket: { Ref: 'ServerlessDeploymentBucket' },
},
FunctionName: 'new-service-dev-func',
Handler: 'func.function.handler',
MemorySize: 1024,
ProvisionedConcurrencyConfig: { ProvisionedConcurrentExecutions: 5 },
Role: { 'Fn::GetAtt': ['IamRoleLambdaExecution', 'Arn'] },
Runtime: 'nodejs12.x',
Timeout: 6,
},
};

return expect(awsCompileFunctions.compileFunctions()).to.be.fulfilled.then(() => {
expect(
awsCompileFunctions.serverless.service.provider.compiledCloudFormationTemplate.Resources
.FuncLambdaFunction
).to.deep.equal(compiledFunction);
});
});

it('should set function declared reserved concurrency limit even if it is zero', () => {
const s3Folder = awsCompileFunctions.serverless.service.package.artifactDirectoryName;
const s3FileName = awsCompileFunctions.serverless.service.package.artifact
Expand Down

0 comments on commit a45d2a2

Please sign in to comment.