skip to Main Content

I built a Docker Lambda Image using the following DockerFile

FROM registry.access.redhat.com/ubi8/ubi:8.1 as base

RUN yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
RUN yum update -y
RUN yum install -y sudo bash curl wget man-db nano bash-completion
RUN yum install -y java-11-openjdk-devel
RUN java -version


ENTRYPOINT [ "/usr/bin/java", "-cp", "./*", "com.amazonaws.services.lambda.runtime.api.client.AWSLambda" ]
CMD ["optimizerMarsExecutor.OptimizerMarsExecutorFunction::handleRequest"]

After building and tagging the image, I executed the Lambda function with the following commands

docker run -p 9000:8080 mars-console-linux-image
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'

Following the executiong of the commands above, I received the following errors:

Error: Could not find or load main class com.amazonaws.services.lambda.runtime.api.client.AWSLambda
Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.lambda.runtime.api.client.AWSLambda

What am I missing?

2

Answers


  1. I suspect you’re not installing the Runtime Interface Client.

    See this example from AWS.

    There’s a pom.xml in the local folder that’s copied to the container image (ADD pom.xml .) before Maven is run (RUN mvn ...) to install the dependencies defined in the pom.xml file:

    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-serialization</artifactId>
            <version>1.1.2</version>
        </dependency>
    <dependencies>
    

    Try replicating the example in that document and you should be successful.

    Login or Signup to reply.
  2. I think the issue is with how your building the image.

    FROM registry.access.redhat.com/ubi8/ubi:8.1 AS base
    
    RUN yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
    RUN yum update -y
    RUN yum install -y sudo bash curl wget man-db nano bash-completion
    RUN yum install -y java-11-openjdk-devel
    RUN java -version
    
    # This should really be a directory under a non-root user. But we will use it for brevity.
    RUN mkdir -p /app
    
    WORKDIR /app
    
    
    # Copy all your classpath files into the container
    COPY . .
    
    # This command is long so use the shell form: https://docs.docker.com/engine/reference/builder/#cmd
    CMD /usr/bin/java 
        -cp /app 
        "com.amazonaws.services.lambda.runtime.api.client.AWSLambda" 
        "optimizerMarsExecutor.OptimizerMarsExecutorFunction::handleRequest"
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search