I'm trying to make AWS SAM CLI part of my docker-compose file, based on this project. Everything seems to come up appropriately, except when I invoke my Lambda function I get a ClassNotFoundException. I've seen several people have problems with permissions that manifest similarly, but even when I make all the files 777 there's no change. I can also see in the logs that it's decompressing the right jar file, and I've decompressed the file myself and seen the class it "can't find".
This is my Docker file:
FROM alpine:3.8
RUN apk add --no-cache python3 python3-dev gcc musl-dev && \
python3 -m ensurepip && \
rm -r /usr/lib/python*/ensurepip && \
pip3 install --upgrade pip setuptools && \
if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
rm -r /root/.cache
ENV PATH $PATH:/root/.local/bin
RUN pip3 install --user awscli
RUN pip3 install --user aws-sam-cli
COPY conf/lambda /var/opt/lambda
COPY lib/lambda.jar /var/opt/lambda/lambda.jar
RUN chmod -R 0777 /var/opt/lambda
RUN python -m site --user-base
WORKDIR /var/opt/lambda
And the relevant portion of docker-compose.yml:
sam:
build:
context: .
dockerfile: Dockerfile-sam
networks:
- sam-local
command: sam local invoke StartPiSessionFunction -e event.json --template template.yaml
hostname: sam
expose:
- 3000
ports:
- 3000:3000
volumes:
- /var/run/docker.sock:/var/run/docker.sock
SAM template:
AWSTemplate
FormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
StartPiSessionFunction:
Type: 'AWS::Serverless::Function'
Properties:
Runtime: java8
Handler: com.company.StartPiSession
Timeout: 60
CodeUri: lambda.jar
And my output logs:
2018-08-10 15:32:41 Invoking com.company.StartPiSession (java8)
2018-08-10 15:32:41 Starting new HTTP connection (1): 169.254.169.254
2018-08-10 15:32:42 Decompressing /var/opt/lambda/lambda.jar
Fetching lambci/lambda:java8 Docker container image......
2018-08-10 15:33:18 Mounting /tmp/tmpsth810ki as /var/task:ro inside runtime container
START RequestId: eb907a6f-68c3-4afe-90b7-87a73a957322 Version: $LATEST
java.lang.ClassNotFoundException: com.company.StartPiSession
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
END RequestId: eb907a6f-68c3-4afe-90b7-87a73a957322
REPORT RequestId: eb907a6f-68c3-4afe-90b7-87a73a957322 Duration: 3.84 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 3 MB