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

NodejsFunction: default runtime is NODEJS_16_X, even though it's not advised to use Node 16 anymore #30181

Closed
timges opened this issue May 13, 2024 · 5 comments
Assignees
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@timges
Copy link

timges commented May 13, 2024

Describe the bug

The default runtime of a NodejsFunction is currently set to NODEJS_16_X, which is a "soon to be depracted" runtime version. This results in [action required] email notifications by aws.

Expected Behavior

Node runtime version is the latest LTS by default, so that deploying a nodejs function without a specified runtime wont result in a [action required] notification

Current Behavior

Node runtime version is NODEJS_16_X by default, which triggers [action required] notifications

Reproduction Steps

  1. Create a NodejsFunction without specifying a runtime.
  2. Deploy
  3. Check the runtime of the lambda function

Possible Solution

Update the default value of the NodejsFunction runtime parameter to match an LTS version

Additional Information/Context

No response

CDK CLI Version

v2.141.0

Framework Version

No response

Node.js Version

20

OS

macos

Language

TypeScript

Language Version

No response

Other information

No response

@timges timges added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 13, 2024
@daschaa
Copy link
Contributor

daschaa commented May 13, 2024

I would like to take this issue if the needs-triage label is removed.

@ashishdhingra ashishdhingra self-assigned this May 13, 2024
@ashishdhingra ashishdhingra added needs-reproduction This issue needs reproduction. and removed needs-triage This issue or PR still needs to be triaged. labels May 13, 2024
@ashishdhingra
Copy link
Contributor

ashishdhingra commented May 13, 2024

@timges Good afternoon. Thanks for reporting the issue. I used AWS CDK version 2.141.0 and it appears to use Nodejs 18.x runtime:

lib/issue30181_nodejsfunction-stack

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lamndaNodejs from 'aws-cdk-lib/aws-lambda-nodejs';
import * as path from 'path';

export class Issue30181NodejsfunctionStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new lamndaNodejs.NodejsFunction(this, 'NodeJsLambda', {
      entry: path.join(__dirname, "../lambda/hello.js"),
      handler: 'hello.handler'
    });
  }
}

lambda/hello.js

exports.handler = async function(event) {
    console.log("request:", JSON.stringify(event, undefined, 2));
    return {
      statusCode: 200,
      headers: { "Content-Type": "text/plain" },
      body: `Hello, CDK! You've hit ${event.path}\n`
    };
  };

package.json

{
  "name": "issue30181_nodejsfunction",
  "version": "0.1.0",
  "bin": {
    "issue30181_nodejsfunction": "bin/issue30181_nodejsfunction.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk"
  },
  "devDependencies": {
    "@types/jest": "^29.5.12",
    "@types/node": "20.12.7",
    "jest": "^29.7.0",
    "ts-jest": "^29.1.2",
    "aws-cdk": "2.141.0",
    "ts-node": "^10.9.2",
    "typescript": "~5.4.5"
  },
  "dependencies": {
    "aws-cdk-lib": "2.141.0",
    "constructs": "^10.0.0",
    "source-map-support": "^0.5.21"
  }
}

Output of cdk synth:

[+] Building 49.0s (14/14) FINISHED                                                                                                                                                                docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                                                                                               0.0s
 => => transferring dockerfile: 1.30kB                                                                                                                                                                             0.0s
 => [internal] load metadata for public.ecr.aws/sam/build-nodejs18.x:latest                                                                                                                                        0.8s
 => [internal] load .dockerignore                                                                                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                                                                    0.0s
 => [ 1/10] FROM public.ecr.aws/sam/build-nodejs18.x:latest@sha256:f69e4886ad87521a752f04e6f35cfb729877571d0407703f3ce55ef88946256b                                                                               28.0s
 => => resolve public.ecr.aws/sam/build-nodejs18.x:latest@sha256:f69e4886ad87521a752f04e6f35cfb729877571d0407703f3ce55ef88946256b                                                                                  0.0s
 => => sha256:891b25c5f9cbca374c0e9585e1401f6b0b97331868b857e6056c0043e00b67f3 88.89kB / 88.89kB                                                                                                                   0.3s
 => => sha256:1f5033e6ae1c03020c32d453a86747c37d70048e6ec3330b6753660e4e179124 9.54kB / 9.54kB                                                                                                                     0.0s
...
...                                                                                                                     0.0s
 => => extracting sha256:947521cc9d3f8bb917b27c486cbfa4189ce8159486aa18ad7709e2b3fbc7c365                                                                                                                          0.0s
 => => extracting sha256:38bc7847684046da394cdc1adc345011750aa5e256bad3627602ebf308089750                                                                                                                          0.0s
 => [ 2/10] RUN npm install --global [email protected]                                                                                                                                                                   3.1s
 => [ 3/10] RUN npm install --global [email protected]                                                                                                                                                                   2.8s
 => [ 4/10] RUN npm install --global typescript                                                                                                                                                                    3.2s
 => [ 5/10] RUN npm install --global --unsafe-perm=true esbuild@0                                                                                                                                                  4.0s
 => [ 6/10] RUN mkdir /tmp/npm-cache &&     chmod -R 777 /tmp/npm-cache &&     npm config --global set cache /tmp/npm-cache                                                                                        1.7s
 => [ 7/10] RUN mkdir /tmp/yarn-cache &&     chmod -R 777 /tmp/yarn-cache &&     yarn config set cache-folder /tmp/yarn-cache                                                                                      1.4s
 => [ 8/10] RUN mkdir /tmp/pnpm-cache &&     chmod -R 777 /tmp/pnpm-cache &&     pnpm config --global set store-dir /tmp/pnpm-cache                                                                                1.7s
 => [ 9/10] RUN npm config --global set update-notifier false                                                                                                                                                      1.6s
 => [10/10] RUN /sbin/useradd -u 1000 user && chmod 711 /                                                                                                                                                          0.4s
 => exporting to image                                                                                                                                                                                             0.1s
 => => exporting layers                                                                                                                                                                                            0.1s
 => => writing image sha256:cb95296ab9680d4d9dcf36836a33d860116065c49a2ab1396280213744257fd3                                                                                                                       0.0s
 => => naming to docker.io/library/cdk-a21c18f6db63871ca0f26a90fcf91f56a28c010e6ea6975ce8f7ea6cd493f3a3                                                                                                            0.0s
...
...

  asset-output/index.js  286b 

⚡ Done in 52ms

Generated YAML template:

Resources:
  NodeJsLambdaServiceRoleFA44C8F8:
    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: Issue30181NodejsfunctionStack/NodeJsLambda/ServiceRole/Resource
  NodeJsLambda3BAC57CB:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket:
          Fn::Sub: cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}
        S3Key: 00c4855d52e4e00779c647ff4229fb4960f46109943177ca985007e70783d4bc.zip
      Environment:
        Variables:
          AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1"
      Handler: hello.handler
      Role:
        Fn::GetAtt:
          - NodeJsLambdaServiceRoleFA44C8F8
          - Arn
      Runtime: nodejs18.x
    DependsOn:
      - NodeJsLambdaServiceRoleFA44C8F8
    Metadata:
      aws:cdk:path: Issue30181NodejsfunctionStack/NodeJsLambda/Resource
      aws:asset:path: asset.00c4855d52e4e00779c647ff4229fb4960f46109943177ca985007e70783d4bc
      aws:asset:is-bundled: true
      aws:asset:property: Code
...
...

Upon deployment using cdk deploy, it appears to use Node.js 18.x runtime.

However, upon checking the code, NodejsFunction construct uses getRuntime() helper method. It uses lambda.Runtime.NODEJS_LATEST (with current value nodejs18.x) if feature flag LAMBDA_NODEJS_USE_LATEST_RUNTIME is enabled. in my case, feature flag is set as "@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true in cdk.json.

Per CDK Developer Guide: Feature flags, the AWS CDK uses feature flags to enable potentially breaking behaviors in a release. Flags are stored as Runtime context values in cdk.json (or ~/.cdk.json).

Issue is reproducible if I disable the feature flag @aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion in cdk.json.

This needs to be discussed with the team since the feature flag is enabled @aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion is enabled by default in latest AWS CDK version, but could be disabled in user projects. And non-depreciated NodeJs runtime should be used.

Thanks,
Ashish

@ashishdhingra ashishdhingra added p2 effort/small Small work item – less than a day of effort and removed needs-reproduction This issue needs reproduction. labels May 13, 2024
@scanlonp
Copy link
Contributor

Hey @timges, Ashish is correct, and I'll sum his comment up. There is a feature flag that changes this default from NODEJS_16_X to NODEJS_LATEST (which is currently NODEJS_18_X). Checkout the code here: function.ts. You can set the feature flag by adding this line to the context section of your cdk.json file: "@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true.

Generally, we keep changes to default values behind feature flags to maintain backwards compatibility. That was the case here. However, we will likely bump the actual default value as the Node 16 lambda runtime is deprecated and function create and update begin to be blocked by lambda itself. The deprecation date is June 12th.

@daschaa, appreciate you offering to update this! However, I think our team will take this on. Node and SDK upgrades are something we are currently focusing on, so this is on our radar. I think we will want to be the ones pulling the trigger on updating this default.

@scanlonp scanlonp assigned scanlonp and unassigned ashishdhingra May 13, 2024
@timges
Copy link
Author

timges commented May 14, 2024

@daschaa @scanlonp @ashishdhingra, Thank you so much for your investigation and fast replies! 💪🏼

While it's important to stay backwards compatible, AWS's deprecation warnings suggest that it might be a good idea to update the default runtime rather soon.

But since you're already aware of this topic, there seems to be no further action needed here. I'm comfortable having this issue closed 😊

@timges timges closed this as completed May 17, 2024
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

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

No branches or pull requests

4 participants