skip to Main Content

I have been implementing an AWS lambda function (node.js v18) which already worked with the following dependencies in package.json

"@aws-sdk/client-apigatewaymanagementapi": "^3.428.0",
"@aws-sdk/client-cognito-identity-provider": "^3.490.0",
"@aws-sdk/client-dynamodb": "^3.405.0",
"@aws-sdk/client-ses": "^3.485.0",
"@aws-sdk/client-ssm": "^3.496.0",
"@aws-sdk/util-dynamodb": "^3.427.0"

Today, I add the new dependency "aws-jwt-verify": "^4.0.0".
Local test was successful with the new dependency used. However, when the lamda code was executed in AWS, it reported the following error in CloudWatch:

2024-01-30T21:42:29.670Z    undefined   ERROR   Uncaught Exception  
{
    "errorType": "Error",
    "errorMessage": "Cannot find package 'aws-jwt-verify' imported from /var/task/cognito.js",
    "code": "ERR_MODULE_NOT_FOUND",
    "stack": [
        "Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'aws-jwt-verify' imported from /var/task/cognito.js",
        "    at new NodeError (node:internal/errors:405:5)",
        "    at packageResolve (node:internal/modules/esm/resolve:895:9)",
        "    at moduleResolve (node:internal/modules/esm/resolve:988:20)",
        "    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:939:12)",
        "    at defaultResolve (node:internal/modules/esm/resolve:1181:79)",
        "    at nextResolve (node:internal/modules/esm/loader:163:28)",
        "    at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)",
        "    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)",
        "    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)",
        "    at link (node:internal/modules/esm/module_job:76:36)"
    ]
}

I really couldn’t see why "aws-jwt-verify" is treated differently than other packages.
In my code file cognito.js:

import { CognitoIdentityProviderClient, DescribeUserPoolClientCommand } from '@aws-sdk/client-cognito-identity-provider';
import { CognitoJwtVerifier } from 'aws-jwt-verify';

where the last line was newly added and caused the issue in aws (worked perfectly in local test).

Could someone please give me a hint?

2

Answers


  1. Chosen as BEST ANSWER

    I've figured out that "aws-jwt-verify" must be added to the nodejs runtime because it does not already exist there (like other @aws-sdk packages).


  2. Tip: if you wish to test your lambda locally with docker, here you go:

    https://docs.aws.amazon.com/lambda/latest/dg/images-create.html

    FROM public.ecr.aws/lambda/nodejs:20
    
    # Copy function code
    COPY index.js ${LAMBDA_TASK_ROOT}
      
    # Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
    CMD [ "index.handler" ]
    

    You could install your dependencies locally and set up your layer.

    You then put all your code in index.js

    exports.handler = async (event) => {
        const response = {
            statusCode: 200,
            body: JSON.stringify('Hello from Lambda!'),
        };
        return response;
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search