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_lambda_nodejs: @smithy/service-error-classification Missing in Lambda 22_X Environment #33099

Open
1 task
SC-CTS opened this issue Jan 23, 2025 · 5 comments
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/medium Medium work item – several days of effort p3

Comments

@SC-CTS
Copy link

SC-CTS commented Jan 23, 2025

Describe the bug

When using the aws-xray-sdk-core package (possibly others as well) in a Lambda with the NodejsFunction using the now default @aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages flag set to true it will fail to execute as the @smithy/service-error-classification will not be bundled and seems absent from the default packages installed in the runtime.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

The version before #31639 was merged and "@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages": true was not the default.

Expected Behavior

The bundled JS should work on the NodeJS 22 runtime.

Possible solutions:

  • Use more granular --external options for the constructs bundling capabilities so the missing packages will get bundled instead of the complete exclusion of @smithy/ at
    ['@aws-sdk/*', '@smithy/*'] : ['@aws-sdk/*'];
  • Add the missing @smithy/* libraries like @smithy/service-error-classification (and possibly others) to the lambda environments bundled packages.

Current Behavior

All @smithy/* packages are externalized by esbuild even though some might not be present in the lambda environment.

Reproduction Steps

  1. npx cdk init app --language typescript
  2. Notice: "@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages": true in the cdk.json
  3. npm i esbuild @types/aws-lambda @smithy/service-error-classification

Stack:

import * as cdk from 'aws-cdk-lib';
import { Duration } from 'aws-cdk-lib';
import { Architecture, Runtime } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunction, OutputFormat } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Construct } from 'constructs';

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

    const fn = new NodejsFunction(this, 'Lambda', {
      entry: `${__dirname}/handler.ts`,
      runtime: Runtime.NODEJS_22_X,
      architecture: Architecture.X86_64,
      timeout: Duration.seconds(30),

      bundling: {
        minify: false,
        format: OutputFormat.ESM,
        mainFields: ['module', 'main'],
        //nodeModules: ['@smithy/service-error-classification'], // If not included, results in Runtime.ImportModuleError.
        banner: "import { createRequire } from 'module'; const require = createRequire(import.meta.url);", // https://github.com/evanw/esbuild/pull/2067
        target: 'node22',
      },
    });
  }
}

handler.ts

import { isClockSkewError } from '@smithy/service-error-classification';
import { Handler } from 'aws-lambda';

export const handler: Handler = async (event, context) => {
  console.log('EVENT: \n' + JSON.stringify(event, null, 2));
  return isClockSkewError(new Error(''));
};

This will result in the following asset bundle for the lambda code:

import { createRequire } from 'module'; const require = createRequire(import.meta.url);

// lib/handler.ts
import { isClockSkewError } from "@smithy/service-error-classification";
var handler = async (event, context) => {
  console.log("EVENT: \n" + JSON.stringify(event, null, 2));
  return isClockSkewError(new Error(""));
};
export {
  handler
};

And when executed will result in:

{
  "errorType": "Error",
  "errorMessage": "Cannot find package '@smithy/service-error-classification' imported from /var/task/index.mjs",
  "trace": [
    "Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@smithy/service-error-classification' imported from /var/task/index.mjs",
    "    at packageResolve (node:internal/modules/esm/resolve:844:9)",
    "    at moduleResolve (node:internal/modules/esm/resolve:913:18)",
    "    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:1043:14)",
    "    at defaultResolve (node:internal/modules/esm/resolve:1086:79)",
    "    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:650:12)",
    "    at #cachedDefaultResolve (node:internal/modules/esm/loader:599:25)",
    "    at ModuleLoader.resolve (node:internal/modules/esm/loader:582:38)",
    "    at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:241:38)",
    "    at ModuleJob._link (node:internal/modules/esm/module_job:132:49)"
  ]
}

Possible Solution

As a workaround you can add the module problematic smithy modules to the nodeModules option of the NodejsFunction bundling options to ship them in the node_modules folder.

Additional Information/Context

No response

CDK CLI Version

2.176.0

Framework Version

No response

Node.js Version

22_X

OS

macOS 15.2

Language

TypeScript

Language Version

No response

Other information

See https://dev.classmethod.jp/articles/fix-cdk-nodejs-functions-bundle-error/ for more analysis

@SC-CTS SC-CTS added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 23, 2025
@pahud
Copy link
Contributor

pahud commented Jan 24, 2025

Thank you. Are you able to provide minimal self-containable code snippets that reproduces this issue? Meanwhile I'll reach out to the maintainer for any possible inputs here.

@pahud pahud added p3 effort/medium Medium work item – several days of effort response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Jan 24, 2025
@pahud
Copy link
Contributor

pahud commented Jan 24, 2025

might related to #31639

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jan 24, 2025
@SC-CTS
Copy link
Author

SC-CTS commented Jan 27, 2025

I will try creating a minimal sample this week, sorry for not providing one immedieately. Yes, it is related to that, specifically to the line

['@aws-sdk/*', '@smithy/*'] : ['@aws-sdk/*'];
which excludes all smithy packages but not all of them are bundled with the NodeJS Lambda environments (I assume only the ones directly referenced by the SDK packages are included).

@SC-CTS
Copy link
Author

SC-CTS commented Jan 28, 2025

@pahud I have added sample code the the above issue description.

@okeeffed
Copy link

okeeffed commented Feb 1, 2025

Just ran into this myself. Switching off the feature flag introduced in the shared PR resolved it for me, although I am guessing that this isn't as intended for the Node 22 env.

"@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages": false,

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/medium Medium work item – several days of effort p3
Projects
None yet
Development

No branches or pull requests

3 participants