1

I have already deployed a Lambda Function on Node16 with JavaScript without any issues. Now I want to develop a Lambda Function on Node18 with TypeScript. I generated a Lambda Function using AWS SAM and deployed it via AWS Toolkit.

When I call the function, I get the following error message.

2023-08-03T14:59:27.311Z undefined ERROR Uncaught Exception
{ "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'index'\nRequire stack:\n- /var/runtime/index.mjs", "stack": [ "Runtime.ImportModuleError: Error: Cannot find module 'index'", "Require stack:", "- /var/runtime/index.mjs", " at _loadUserApp (file:///var/runtime/index.mjs:997:17)", " at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1029:21)", " at async start (file:///var/runtime/index.mjs:1192:23)", " at async file:///var/runtime/index.mjs:1198:1" ]

How can I solve this?

index.ts

import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';

export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
    try {
        return {
            statusCode: 200,
            body: JSON.stringify({
                message: 'hello world',
            }),
        };
    } catch (err) {
        console.log(err);
        return {
            statusCode: 500,
            body: JSON.stringify({
                message: 'some error happened',
            }),
        };
    }
};

package.json

{
  "name": "hello_world",
  "version": "0.3.0",
  "description": "",
  "main": "index.ts",
  "scripts": {
    "compile": "tsc"
  },
  "dependencies": {
    "esbuild": "^0.14.14"
  },
  "devDependencies": {
    "@types/aws-lambda": "^8.10.92",
    "@types/node": "^18.11.4",
    "ts-node": "^10.9.1",
    "typescript": "^4.8.4"
  }
}

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
  generateblogpostv1:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: index.handler
      Runtime: nodejs18.x
      CodeUri: .
      Description: ''
      MemorySize: 128
      Timeout: 900
      Role: >-
        arn:aws:iam::921222634505:role/service-role/...
      RuntimeManagementConfig:
        UpdateRuntimeOn: Auto

tsconfig.json

{
    "compilerOptions": {
      "target": "es2020",
      "strict": true,
      "preserveConstEnums": true,
      "noEmit": true,
      "sourceMap": false,
      "module":"es2015",
      "moduleResolution":"node",
      "esModuleInterop": true, 
      "skipLibCheck": true,
      "forceConsistentCasingInFileNames": true,  
    },
    "exclude": ["node_modules"]
  }

6
  • Related to commonjs/es6 modules and the type setting in package.json?
    – jarmod
    Commented Aug 3, 2023 at 15:29
  • Unfortunately, this does not solve the problem. Error is still there.
    – laprof
    Commented Aug 3, 2023 at 15:45
  • What is uploaded in your Lambda package: index.js or index.mjs?
    – jarmod
    Commented Aug 3, 2023 at 15:50
  • I am using the official AWS Toolkit for IntelliJ. This tool has the ability to Update Function Code of an existing Lambda Function. I guess it builds the project and uploads it. The tool builds an index.ts
    – laprof
    Commented Aug 3, 2023 at 16:04
  • When I run sam build on my own it builds just an index.js. But I do not know how to update an already existing Lambda Function via CLI.
    – laprof
    Commented Aug 3, 2023 at 16:10

2 Answers 2

1

I have created my own deployment script, which works fine. The AWS Toolkit is bullshit.

  "scripts": {
    "prebuild": "rm -rf dist",
    "deploy": "npm run build && aws lambda update-function-code --zip-file \"fileb://dist/index.zip\" --function-name hello-world",
    "postbuild": "cd dist && zip -r index.zip index.js*",
    "prisma:generate": "npx prisma generate"
  },
1
  • Where is the script of build? Commented Feb 6, 2024 at 7:35
0

I think your issue is with package.json file.

"scripts": {
   "compile": "tsc"
}

you need to bundle the code using esbuild

"scripts": {
   "build": "esbuild src/index.ts --bundle --minify --sourcemap --platform=node --target=es2020 --outfile=dist/index.js" 
}

Please find the official documentation here

https://docs.aws.amazon.com/lambda/latest/dg/typescript-image.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.