Skip to content
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

Open
mpetito-envative opened this issue Feb 26, 2025 · 2 comments
Labels
@aws-cdk/aws-lambda Related to AWS Lambda documentation This is a problem with documentation. effort/small Small work item – less than a day of effort p2

Comments

@mpetito-envative
Copy link

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 from aws-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

@mpetito-envative mpetito-envative added documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged. labels Feb 26, 2025
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Feb 26, 2025
@ashishdhingra ashishdhingra self-assigned this Feb 26, 2025
@ashishdhingra ashishdhingra added p2 investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. labels Feb 26, 2025
@ashishdhingra
Copy link
Contributor

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 Schedule with cron(...) as one of the acceptable formats, instead of AWS::AutoScaling::ScheduledAction. AWS::AutoScaling::ScheduledAction is used for Amazon EC2 Auto Scaling scheduled action defining Recurrence string.

@ashishdhingra ashishdhingra added effort/small Small work item – less than a day of effort and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Feb 26, 2025
@ashishdhingra ashishdhingra removed their assignment Feb 26, 2025
@mpetito-envative
Copy link
Author

I'm not sure I follow your conclusion. If you deploy the generated template you've shared, you will get a cloudformation error because Schedule: 0 8 * * * is invalid for an AWS::ApplicationAutoScaling::ScalableTarget. It does not conform to the acceptable expression syntax, which is Schedule: cron(fields).

Another example demonstrating use of aws-cdk-lib/aws-autoscaling for Amazon EC2 Auto Scaling scheduled action should be added.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda documentation This is a problem with documentation. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

No branches or pull requests

2 participants