-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(aws-cdk-lib.aws_lambda): First lambda autoscaling example has incorrect import #33593
Comments
The first example at https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda-readme.html#autoscaling, modified version below: import * as cdk from 'aws-cdk-lib';
import * as autoscaling from 'aws-cdk-lib/aws-autoscaling';
import * as lambda from 'aws-cdk-lib/aws-lambda';
export class CdktestStackNew extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const fn = new lambda.Function(this, 'MyLambda', {
code: new lambda.InlineCode('exports.handler = async () => { console.log(\'hello world\'); };'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_LATEST,
});
const alias = fn.addAlias('prod');
// Create AutoScaling target
const as = alias.addAutoScaling({ maxCapacity: 50 });
// Configure Target Tracking
as.scaleOnUtilization({
utilizationTarget: 0.5,
});
// Configure Scheduled Scaling
as.scaleOnSchedule('ScaleUpInTheMorning', {
schedule: autoscaling.Schedule.cron({ hour: '8', minute: '0' }),
minCapacity: 20,
});
}
} synthesizes to below CloudFormation template: 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
Metadata:
aws:cdk:path: CdktestStackNew/MyLambda/ServiceRole/Resource
MyLambdaCCE802FB:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: exports.handler = async () => { console.log('hello world'); };
Handler: index.handler
Role:
Fn::GetAtt:
- MyLambdaServiceRole4539ECB6
- Arn
Runtime: nodejs18.x
DependsOn:
- MyLambdaServiceRole4539ECB6
Metadata:
aws:cdk:path: CdktestStackNew/MyLambda/Resource
MyLambdaCurrentVersionE7A382CCe21d042dca0bfd565457befbbbeaab23:
Type: AWS::Lambda::Version
Properties:
FunctionName:
Ref: MyLambdaCCE802FB
Metadata:
aws:cdk:path: CdktestStackNew/MyLambda/CurrentVersion/Resource
MyLambdaAliasprodE5E3E731:
Type: AWS::Lambda::Alias
Properties:
FunctionName:
Ref: MyLambdaCCE802FB
FunctionVersion:
Fn::GetAtt:
- MyLambdaCurrentVersionE7A382CCe21d042dca0bfd565457befbbbeaab23
- Version
Name: prod
Metadata:
aws:cdk:path: CdktestStackNew/MyLambda/Aliasprod/Resource
MyLambdaAliasprodAliasScalingTargetCBB1F7C5:
Type: AWS::ApplicationAutoScaling::ScalableTarget
Properties:
MaxCapacity: 50
MinCapacity: 1
ResourceId:
Fn::Join:
- ""
- - "function:"
- Fn::Select:
- 6
- Fn::Split:
- ":"
- Ref: MyLambdaAliasprodE5E3E731
- :prod
RoleARN: arn:aws:iam::<<ACCOUNT-ID>>:role/aws-service-role/lambda.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_LambdaConcurrency
ScalableDimension: lambda:function:ProvisionedConcurrency
ScheduledActions:
- ScalableTargetAction:
MinCapacity: 20
Schedule: 0 8 * * *
ScheduledActionName: ScaleUpInTheMorning
ServiceNamespace: lambda
Metadata:
aws:cdk:path: CdktestStackNew/MyLambda/Aliasprod/AliasScaling/Target/Resource
MyLambdaAliasprodAliasScalingTargetTracking364B3A43:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
Properties:
PolicyName: CdktestStackNewMyLambdaAliasprodAliasScalingTargetTrackingFF2E0ABB
PolicyType: TargetTrackingScaling
ScalingTargetId:
Ref: MyLambdaAliasprodAliasScalingTargetCBB1F7C5
TargetTrackingScalingPolicyConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: LambdaProvisionedConcurrencyUtilization
TargetValue: 0.5
Metadata:
aws:cdk:path: CdktestStackNew/MyLambda/Aliasprod/AliasScaling/Target/Tracking/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Analytics: v2:deflate64:H4sIAAAAAAAA/12MwWrDMBAFvyV3eeuEHnJNCz0XO+Ra1uut2ViWglZqKEL/XlTXOfQ0w/B4B9gfW2h3eNeGxrmxMkDuI9Js8K4f2eIyjAj5LTmK4p15/XQPv3DQv7bpyQpqDb9SjOACufOWa6ssBm83K4T1AVP0SmjFTZBfULkntDhYPsUYZEiRzVbOGCaO9eVfWXEOSLO4qV/f3r0V+t7Wj1CK6Vh9CsTFOD8yXPXpa3+EQwvPu6uKNCG5KAtDt/IHXl1cByABAAA=
Metadata:
aws:cdk:path: CdktestStackNew/CDKMetadata/Default
Parameters:
BootstrapVersion:
Type: AWS::SSM::Parameter::Value<String>
Default: /cdk-bootstrap/hnb659fds/version
Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip] It outputs AWS::ApplicationAutoScaling::ScalableTarget resource defining ScheduledAction having
|
I'm not sure I follow your conclusion. If you deploy the generated template you've shared, you will get a cloudformation error because
The example referenced is in the aws_lambda module, so I'm not sure why an EC2 Auto Scaling example would be appropriate to show in the lambda docs. Unless I've missed something, EC2 Auto Scaling does not apply to lambda environments since lambda functions are not members of EC2 auto scaling groups. |
Describe the issue
The first lambda autoscaling example has an incorrect import. Instead of importing from
aws-cdk-lib/aws-autoscaling
it should instead be importing fromaws-cdk-lib/aws-applicationautoscaling
.The use of the Schedule class from the autoscaling lib generates a string value that will synth but produces incompatible cron expressions for the Schedule attribute since they will not be wrapped with
cron(...)
.Links
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda-readme.html#autoscaling
The text was updated successfully, but these errors were encountered: