-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lambda): autoscaling for lambda aliases (#8883)
- Loading branch information
Showing
9 changed files
with
584 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/@aws-cdk/aws-lambda/lib/private/scalable-function-attribute.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as appscaling from '@aws-cdk/aws-applicationautoscaling'; | ||
import { Construct, Token } from '@aws-cdk/core'; | ||
import { IScalableFunctionAttribute, UtilizationScalingOptions } from '../scalable-attribute-api'; | ||
|
||
/** | ||
* A scalable lambda alias attribute | ||
*/ | ||
export class ScalableFunctionAttribute extends appscaling.BaseScalableAttribute implements IScalableFunctionAttribute{ | ||
constructor(scope: Construct, id: string, props: ScalableFunctionAttributeProps){ | ||
super(scope, id, props); | ||
} | ||
|
||
/** | ||
* Scale out or in to keep utilization at a given level. The utilization is tracked by the | ||
* LambdaProvisionedConcurrencyUtilization metric, emitted by lambda. See: | ||
* https://docs.aws.amazon.com/lambda/latest/dg/monitoring-metrics.html#monitoring-metrics-concurrency | ||
*/ | ||
public scaleOnUtilization(options: UtilizationScalingOptions) { | ||
if ( !Token.isUnresolved(options.utilizationTarget) && (options.utilizationTarget < 0.1 || options.utilizationTarget > 0.9)) { | ||
throw new Error(`Utilization Target should be between 0.1 and 0.9. Found ${options.utilizationTarget}.`); | ||
} | ||
super.doScaleToTrackMetric('Tracking', { | ||
targetValue: options.utilizationTarget, | ||
predefinedMetric: appscaling.PredefinedMetric.LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION, | ||
...options, | ||
}); | ||
} | ||
|
||
/** | ||
* Scale out or in based on schedule. | ||
*/ | ||
public scaleOnSchedule(id: string, action: appscaling.ScalingSchedule) { | ||
super.doScaleOnSchedule(id, action); | ||
} | ||
} | ||
|
||
/** | ||
* Properties of a scalable function attribute | ||
*/ | ||
export interface ScalableFunctionAttributeProps extends appscaling.BaseScalableAttributeProps { | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/@aws-cdk/aws-lambda/lib/scalable-attribute-api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as appscaling from '@aws-cdk/aws-applicationautoscaling'; | ||
import { IConstruct } from '@aws-cdk/core'; | ||
|
||
|
||
/** | ||
* Interface for scalable attributes | ||
*/ | ||
export interface IScalableFunctionAttribute extends IConstruct { | ||
/** | ||
* Scale out or in to keep utilization at a given level. The utilization is tracked by the | ||
* LambdaProvisionedConcurrencyUtilization metric, emitted by lambda. See: | ||
* https://docs.aws.amazon.com/lambda/latest/dg/monitoring-metrics.html#monitoring-metrics-concurrency | ||
*/ | ||
scaleOnUtilization(options: UtilizationScalingOptions): void; | ||
/** | ||
* Scale out or in based on schedule. | ||
*/ | ||
scaleOnSchedule(id: string, actions: appscaling.ScalingSchedule): void; | ||
} | ||
|
||
/** | ||
* Options for enabling Lambda utilization tracking | ||
*/ | ||
export interface UtilizationScalingOptions extends appscaling.BaseTargetTrackingProps { | ||
/** | ||
* Utilization target for the attribute. For example, .5 indicates that 50 percent of allocated provisioned concurrency is in use. | ||
*/ | ||
readonly utilizationTarget: number; | ||
} | ||
|
||
/** | ||
* Properties for enabling Lambda autoscaling | ||
*/ | ||
export interface AutoScalingOptions { | ||
/** | ||
* Minimum capacity to scale to | ||
* | ||
* @default 1 | ||
*/ | ||
readonly minCapacity?: number; | ||
|
||
/** | ||
* Maximum capacity to scale to | ||
*/ | ||
readonly maxCapacity: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
packages/@aws-cdk/aws-lambda/test/integ.autoscaling.lit.expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
{ | ||
"Resources": { | ||
"MyLambdaServiceRole4539ECB6": { | ||
"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" | ||
] | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
"MyLambdaCCE802FB": { | ||
"Type": "AWS::Lambda::Function", | ||
"Properties": { | ||
"Code": { | ||
"ZipFile": "exports.handler = async () => {\nconsole.log('hello world');\n};" | ||
}, | ||
"Handler": "index.handler", | ||
"Role": { | ||
"Fn::GetAtt": [ | ||
"MyLambdaServiceRole4539ECB6", | ||
"Arn" | ||
] | ||
}, | ||
"Runtime": "nodejs10.x" | ||
}, | ||
"DependsOn": [ | ||
"MyLambdaServiceRole4539ECB6" | ||
] | ||
}, | ||
"MyLambdaVersion16CDE3C40": { | ||
"Type": "AWS::Lambda::Version", | ||
"Properties": { | ||
"FunctionName": { | ||
"Ref": "MyLambdaCCE802FB" | ||
}, | ||
"Description": "integ-test" | ||
} | ||
}, | ||
"Alias325C5727": { | ||
"Type": "AWS::Lambda::Alias", | ||
"Properties": { | ||
"FunctionName": { | ||
"Ref": "MyLambdaCCE802FB" | ||
}, | ||
"FunctionVersion": { | ||
"Fn::GetAtt": [ | ||
"MyLambdaVersion16CDE3C40", | ||
"Version" | ||
] | ||
}, | ||
"Name": "prod" | ||
} | ||
}, | ||
"AliasAliasScalingTarget7449FF0E": { | ||
"Type": "AWS::ApplicationAutoScaling::ScalableTarget", | ||
"Properties": { | ||
"MaxCapacity": 50, | ||
"MinCapacity": 3, | ||
"ResourceId": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"function:", | ||
{ | ||
"Fn::Select": [ | ||
6, | ||
{ | ||
"Fn::Split": [ | ||
":", | ||
{ | ||
"Ref": "Alias325C5727" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
":prod" | ||
] | ||
] | ||
}, | ||
"RoleARN": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":iam::", | ||
{ | ||
"Ref": "AWS::AccountId" | ||
}, | ||
":role/aws-service-role/lambda.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_LambdaConcurrency" | ||
] | ||
] | ||
}, | ||
"ScalableDimension": "lambda:function:ProvisionedConcurrency", | ||
"ServiceNamespace": "lambda", | ||
"ScheduledActions": [ | ||
{ | ||
"ScalableTargetAction": { | ||
"MinCapacity": 20 | ||
}, | ||
"Schedule": "cron(0 8 * * ? *)", | ||
"ScheduledActionName": "ScaleUpInTheMorning" | ||
}, | ||
{ | ||
"ScalableTargetAction": { | ||
"MaxCapacity": 20 | ||
}, | ||
"Schedule": "cron(0 20 * * ? *)", | ||
"ScheduledActionName": "ScaleDownAtNight" | ||
} | ||
] | ||
} | ||
}, | ||
"AliasAliasScalingTargetTrackingA7718D48": { | ||
"Type": "AWS::ApplicationAutoScaling::ScalingPolicy", | ||
"Properties": { | ||
"PolicyName": "awslambdaautoscalingAliasAliasScalingTargetTrackingD339330D", | ||
"PolicyType": "TargetTrackingScaling", | ||
"ScalingTargetId": { | ||
"Ref": "AliasAliasScalingTarget7449FF0E" | ||
}, | ||
"TargetTrackingScalingPolicyConfiguration": { | ||
"PredefinedMetricSpecification": { | ||
"PredefinedMetricType": "LambdaProvisionedConcurrencyUtilization" | ||
}, | ||
"TargetValue": 0.5 | ||
} | ||
} | ||
} | ||
}, | ||
"Outputs": { | ||
"FunctionName": { | ||
"Value": { | ||
"Ref": "MyLambdaCCE802FB" | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.