Reputation: 1354
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"]
}
Upvotes: 1
Views: 2880
Reputation: 3249
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
Upvotes: 0
Reputation: 1354
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"
},
Upvotes: 1