I have a small app (just testing it out for now) written in typescript and I would like to deploy to lambda. I have followed the official tutorial for creating lambda container images in AWS official guide. I have only changed the location of the handler to src/index.ts
When I run curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
I get curl: (56) Recv failure: Connection reset by peer
.
Dockerfile
FROM public.ecr.aws/lambda/nodejs:14
COPY . ${LAMBDA_TASK_ROOT}
# Install NPM dependencies for function
RUN npm install
RUN npm run build
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "dist/index.handler" ]
package.json
{
...
"scripts": {
"build": "tsc",
"start": "yarn run build && node dist/index.js",
"lint": "eslint . --ext .ts",
"test": "jest"
},
...
"dependencies": {
...
"typescript": "^4.5.4"
},
"devDependencies": {
"@types/node": "14",
"jest": "^27.4.5"
}
}
src/index.ts
export const handler = (event, context, callback) => {
console.log("It ran");
return callback(null, {
statusCode: 200,
message: "Hello",
body: "Hello"
})
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"declaration": true,
},
"lib": ["es2015"]
}
From my understanding AWS container images uses aws-lambda-runtime-interface-emulator. Visiting their github page theres literally nothing regarding debugging. No way to get the logs, no way to understand what is running or not.
From this answer it looks like that the app inside the container doesn’t have a port assigned to it, but then again, no way to debug or see logs.
When I deploy the function to aws lambda and test it, it works as expected:
Question
How can I debug what is going on? Why am I receiving a curl: (56) Recv failure: Connection reset by peer
?
2
Answers
The problem was that when building the image I mapped port
9000
to8000
when the container application binds to8080
. It solved the issue by changing:to
If after following the accepted answer and still getting the error. Just make sure you don’t have another container running on port 9000.
Just run
docker container ls
and check if PORTS column does not contain 9000 for another container.To fix it just choose another port that you know is not being used for example
docker run -p 9001:8080 xxxx
and update your curl request accordingly