bjfletcher
bjfletcher

Reputation: 11508

Lambda container images complain about entrypoint needing handler name as the first argument

When I run an AWS Lambda container (Docker) image, for example:

docker run public.ecr.aws/lambda/java bash

I get the following error:

entrypoint requires the handler name to be the first argument

What should the handler name be?

Upvotes: 28

Views: 39597

Answers (6)

Elias Nada
Elias Nada

Reputation: 1

I ran into this problem as well, it was giving me that error because I passed some arguments in the docker run command after specifying the image

before:

docker run -p 8000:8080 my-image --name my-container

I get "entrypoint requires the handler name to be the first argument"

after:

docker run -p 8000:8080 --name my-container my-image

worked

Hope this helps someone

Upvotes: 0

Jinesh
Jinesh

Reputation: 2575

I faced similar issue. In my case the issue was that I was using CMD in my docker-compose.yml, which was conflicting with the CMD in the Dockerfile.

Removing CMD from the docker-compose.yml helped me to resolve the issue.

Upvotes: 0

InstructionDecode
InstructionDecode

Reputation: 21

I wanted to add my system, and scenario, to this in case anybody does something similar(Windows). I was getting this error message, but it was actually with testing my python Lambda and Docker container locally, before lobbing it into the cloud. My project directory structure is src/main/main.py. That made my Dockerfile look like this:

# Use the official AWS Lambda Python base image.
FROM public.ecr.aws/lambda/python:3.11

# Set the working directory.
WORKDIR /var/task

# Copy contents of local folders into /var/task/{directory-name}, in the container.
COPY ./main ./main
COPY ./utils ./utils
COPY __init__.py ./

# If you have additional module requirements, copy the dependencies.txt file and install them.
COPY dependencies.txt ./

RUN pip install -r dependencies.txt

# Set the Lambda handler function name - folder.file.function
CMD ["main.main.handler"]

For me, I this error was fixed as soon as I started using --platform linux/amd64 in the Docker command line arguments. I.e. the three step process below:

  1. run docker build --platform linux/amd64 -t my_image_name:test .
  2. docker run --platform linux/amd64 -p 9000:8080 my_image_name:test.

Order matters here^ (make sure the name of the container is last). Then finally, you can test the container with curl.

curl "http://localhost:9000/2015-03-31/functions/function/invocations"
-d '{"payload":"hello world!"}'

These commands, and this flow, are mentioned in the AWS docs here (Using an AWS base image for Python section), but I didn't follow directions! :)

Upvotes: 1

Nigrimmist
Nigrimmist

Reputation: 12378

Guys in the thread are right, i stucked at the same place with my Node.js small script.

So, for me the right solution was adding ENTRYPOINT ["/lambda-entrypoint.sh", "helloWorldFunction.handler"]

Docker :

FROM public.ecr.aws/lambda/nodejs:14
COPY helloWorldFunction.js package*.json ./
RUN npm install
ENTRYPOINT ["/lambda-entrypoint.sh", "helloWorldFunction.handler"] 
CMD [ "helloWorldFunction.handler" ]

lambda-entrypoint.sh is already placed in aws base image, don't need to add it manually

helloWorldFunction.js :

exports.handler = async (event, context) => {
    return "Hello World!";
};

Also, in case you are deploying it using visual studio and AWS Toolkit - clear "image-command" field in your aws-lambda-tools-default.json as it overrides docker's CMD

Upvotes: 0

Gabriel Devillers
Gabriel Devillers

Reputation: 4002

In my case I wanted to use the lambda image in gitlab-ci. The solution was to override the base image entrypoint by adding the following line to my Dockerfile:

ENTRYPOINT ["/bin/bash", "-l", "-c"]

Note that this means the image will not be usable in Amazon's lambdas anymore.

Upvotes: 10

bjfletcher
bjfletcher

Reputation: 11508

It depends what the language is of the runtime. For example, if it is NodeJS, then the handler name should look like:

"app.handler"

If it is Java, then it should look like:

"com.example.LambdaHandler::handleRequest"

The image will look for them in LAMBDA_TASK_ROOT so you will need to make sure that your code (or compiled code) is copied to that folder when you build the image, for example:

COPY target/* ${LAMBDA_TASK_ROOT}

Upvotes: 9

Related Questions