Skip to content

Commit

Permalink
feat(lambda): provisioned concurrency (#5308)
Browse files Browse the repository at this point in the history
fixes #5298
  • Loading branch information
nataibi authored and nija-at committed Dec 17, 2019
1 parent f3d5fc9 commit d50344a
Show file tree
Hide file tree
Showing 6 changed files with 418 additions and 5 deletions.
29 changes: 27 additions & 2 deletions packages/@aws-cdk/aws-lambda/lib/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export interface AliasProps {
* @default No additional versions
*/
readonly additionalVersions?: VersionWeight[];

/**
* Specifies a provisioned concurrency configuration for a function's alias.
*
* @default No provisioned concurrency
*/
readonly provisionedConcurrentExecutions?: number;
}

export interface AliasAttributes {
Expand Down Expand Up @@ -127,7 +134,8 @@ export class Alias extends QualifiedFunctionBase implements IAlias {
description: props.description,
functionName: this.version.lambda.functionName,
functionVersion: props.version.version,
routingConfig: this.determineRoutingConfig(props)
routingConfig: this.determineRoutingConfig(props),
provisionedConcurrencyConfig: this.determineProvisionedConcurrency(props)
});

this.functionArn = this.getResourceArnAttribute(alias.ref, {
Expand Down Expand Up @@ -200,6 +208,23 @@ export class Alias extends QualifiedFunctionBase implements IAlias {
throw new Error(`Sum of additional version weights must not exceed 1, got: ${total}`);
}
}

/**
* Validate that the provisionedConcurrentExecutions makes sense
*
* Member must have value greater than or equal to 1
*/
private determineProvisionedConcurrency(props: AliasProps): CfnAlias.ProvisionedConcurrencyConfigurationProperty | undefined {
if (!props.provisionedConcurrentExecutions) {
return undefined;
}

if (props.provisionedConcurrentExecutions <= 0) {
throw new Error('provisionedConcurrentExecutions must have value greater than or equal to 1');
}

return {provisionedConcurrentExecutions: props.provisionedConcurrentExecutions};
}
}

/**
Expand All @@ -215,4 +240,4 @@ export interface VersionWeight {
* How much weight to assign to this version (0..1)
*/
readonly weight: number;
}
}
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,15 @@ export class Function extends FunctionBase {
* @param codeSha256 The SHA-256 hash of the most recently deployed Lambda source code, or
* omit to skip validation.
* @param description A description for this version.
* @param provisionedExecutions A provisioned concurrency configuration for a function's version.
* @returns A new Version object.
*/
public addVersion(name: string, codeSha256?: string, description?: string): Version {
public addVersion(name: string, codeSha256?: string, description?: string, provisionedExecutions?: number): Version {
return new Version(this, 'Version' + name, {
lambda: this,
codeSha256,
description,
provisionedConcurrentExecutions: provisionedExecutions,
});
}

Expand Down
29 changes: 27 additions & 2 deletions packages/@aws-cdk/aws-lambda/lib/lambda-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export interface VersionProps {
* Function to get the value of
*/
readonly lambda: IFunction;

/**
* Specifies a provisioned concurrency configuration for a function's version.
*
* @default No provisioned concurrency
*/
readonly provisionedConcurrentExecutions?: number;
}

export interface VersionAttributes {
Expand Down Expand Up @@ -126,7 +133,8 @@ export class Version extends QualifiedFunctionBase implements IVersion {
const version = new CfnVersion(this, 'Resource', {
codeSha256: props.codeSha256,
description: props.description,
functionName: props.lambda.functionName
functionName: props.lambda.functionName,
provisionedConcurrencyConfig: this.determineProvisionedConcurrency(props)
});

this.version = version.attrVersion;
Expand Down Expand Up @@ -155,6 +163,23 @@ export class Version extends QualifiedFunctionBase implements IVersion {
...props
});
}

/**
* Validate that the provisionedConcurrentExecutions makes sense
*
* Member must have value greater than or equal to 1
*/
private determineProvisionedConcurrency(props: VersionProps): CfnVersion.ProvisionedConcurrencyConfigurationProperty | undefined {
if (!props.provisionedConcurrentExecutions) {
return undefined;
}

if (props.provisionedConcurrentExecutions <= 0) {
throw new Error('provisionedConcurrentExecutions must have value greater than or equal to 1');
}

return {provisionedConcurrentExecutions: props.provisionedConcurrentExecutions};
}
}

/**
Expand All @@ -172,4 +197,4 @@ export class Version extends QualifiedFunctionBase implements IVersion {
*/
function extractVersionFromArn(arn: string) {
return Fn.select(7, Fn.split(':', arn));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
{
"Resources": {
"MyLambdaAliasPCEServiceRoleF7C9F212": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
]
}
},
"MyLambdaAliasPCEServiceRoleDefaultPolicyE7418D56": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": "*",
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
},
"PolicyName": "MyLambdaAliasPCEServiceRoleDefaultPolicyE7418D56",
"Roles": [
{
"Ref": "MyLambdaAliasPCEServiceRoleF7C9F212"
}
]
}
},
"MyLambdaAliasPCED0B8D751": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "exports.handler = async function(event, context) { console.log(\"Hello from CDK! with Alias Provisioned Concurrent Exec!\");}"
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"MyLambdaAliasPCEServiceRoleF7C9F212",
"Arn"
]
},
"Runtime": "nodejs10.x"
},
"DependsOn": [
"MyLambdaAliasPCEServiceRoleDefaultPolicyE7418D56",
"MyLambdaAliasPCEServiceRoleF7C9F212"
]
},
"MyLambdaAliasPCEVersion15F479C08": {
"Type": "AWS::Lambda::Version",
"Properties": {
"FunctionName": {
"Ref": "MyLambdaAliasPCED0B8D751"
}
}
},
"Alias325C5727": {
"Type": "AWS::Lambda::Alias",
"Properties": {
"FunctionName": {
"Ref": "MyLambdaAliasPCED0B8D751"
},
"FunctionVersion": {
"Fn::GetAtt": [
"MyLambdaAliasPCEVersion15F479C08",
"Version"
]
},
"Name": "prod",
"ProvisionedConcurrencyConfig": {
"ProvisionedConcurrentExecutions": 5
}
}
},
"AliasAliasPermissionAF30F9E8": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "Alias325C5727"
},
"Principal": "cloudformation.amazonaws.com"
}
},
"MyLambdaVersionPCEServiceRole2ACFB73E": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
]
}
},
"MyLambdaVersionPCEServiceRoleDefaultPolicy229A1552": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": "*",
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
},
"PolicyName": "MyLambdaVersionPCEServiceRoleDefaultPolicy229A1552",
"Roles": [
{
"Ref": "MyLambdaVersionPCEServiceRole2ACFB73E"
}
]
}
},
"MyLambdaVersionPCEA3A0D86B": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "exports.handler = async function(event, context) { console.log(\"Hello from CDK! with Version Provisioned Concurrent Exec!\");}"
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"MyLambdaVersionPCEServiceRole2ACFB73E",
"Arn"
]
},
"Runtime": "nodejs10.x"
},
"DependsOn": [
"MyLambdaVersionPCEServiceRoleDefaultPolicy229A1552",
"MyLambdaVersionPCEServiceRole2ACFB73E"
]
},
"MyLambdaVersionPCEVersion2C704112A": {
"Type": "AWS::Lambda::Version",
"Properties": {
"FunctionName": {
"Ref": "MyLambdaVersionPCEA3A0D86B"
},
"ProvisionedConcurrencyConfig": {
"ProvisionedConcurrentExecutions": 5
}
}
},
"Alias29455D932": {
"Type": "AWS::Lambda::Alias",
"Properties": {
"FunctionName": {
"Ref": "MyLambdaVersionPCEA3A0D86B"
},
"FunctionVersion": {
"Fn::GetAtt": [
"MyLambdaVersionPCEVersion2C704112A",
"Version"
]
},
"Name": "prod"
}
},
"Alias2AliasPermission2448514B6": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "Alias29455D932"
},
"Principal": "cloudformation.amazonaws.com"
}
}
}
}
Loading

0 comments on commit d50344a

Please sign in to comment.