aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.51k stars 1.17k forks source link

[java8] sam local invoke throws java.lang.ClassNotFoundException when CodeUri points to a zip #1209

Closed yekkan closed 5 years ago

yekkan commented 5 years ago

Description

[java8] sam local invoke throws java.lang.ClassNotFoundException when CodeUri points to a zip

Steps to reproduce

Init an example application:

sam init --runtime java8 --dependency-manager gradle

Make following changes in HelloWorldFunction/build.gradle and template.yaml:

HelloWorldFunction/build.gradle
12a13,20
> 
> task buildZip(type: Zip) {
>     from compileJava
>     from processResources
>     into('lib') {
>         from configurations.compileClasspath
>     }
> }
template.yaml
17c17
<       CodeUri: HelloWorldFunction
---
>       CodeUri: ./HelloWorldFunction/build/distributions/HelloWorldFunction.zip

Build zip:

HelloWorldFunction$ ./gradlew buildZip

Observed result

$ sam local invoke --no-event --debug
2019-06-04 15:56:16 Using SAM Template at /home/vagrant/projects/sam-app/java8/template.yaml
2019-06-04 15:56:16 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-06-04 15:56:16 Changing event name from before-call.apigateway to before-call.api-gateway
2019-06-04 15:56:16 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-06-04 15:56:16 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-06-04 15:56:16 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-06-04 15:56:16 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-06-04 15:56:16 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section
2019-06-04 15:56:16 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-06-04 15:56:16 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-06-04 15:56:16 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-06-04 15:56:16 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-06-04 15:56:16 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-06-04 15:56:16 Changing event name from before-call.apigateway to before-call.api-gateway
2019-06-04 15:56:16 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-06-04 15:56:16 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-06-04 15:56:16 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-06-04 15:56:16 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-06-04 15:56:16 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section
2019-06-04 15:56:16 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-06-04 15:56:16 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-06-04 15:56:16 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-06-04 15:56:16 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-06-04 15:56:16 local invoke command is called
2019-06-04 15:56:16 No Parameters detected in the template
2019-06-04 15:56:16 2 resources found in the template
2019-06-04 15:56:16 Found Serverless function with name='HelloWorldFunction' and CodeUri='./HelloWorldFunction/build/distributions/HelloWorldFunction.zip'
2019-06-04 15:56:16 Trying paths: ['/home/vagrant/.docker/config.json', '/home/vagrant/.dockercfg']
2019-06-04 15:56:16 No config file found
2019-06-04 15:56:16 Trying paths: ['/home/vagrant/.docker/config.json', '/home/vagrant/.dockercfg']
2019-06-04 15:56:16 No config file found
2019-06-04 15:56:16 http://localhost:None "GET /v1.35/_ping HTTP/1.1" 200 2
2019-06-04 15:56:16 Looking for credentials via: env
2019-06-04 15:56:16 Looking for credentials via: assume-role
2019-06-04 15:56:16 Looking for credentials via: shared-credentials-file
2019-06-04 15:56:16 Found credentials in shared credentials file: ~/.aws/credentials
2019-06-04 15:56:16 Loading JSON file: /home/linuxbrew/.linuxbrew/Cellar/aws-sam-cli/0.16.1/libexec/lib/python3.7/site-packages/botocore/data/endpoints.json
2019-06-04 15:56:16 Event choose-service-name: calling handler <function handle_service_name_alias at 0x7fb161204048>
2019-06-04 15:56:16 Loading JSON file: /home/linuxbrew/.linuxbrew/Cellar/aws-sam-cli/0.16.1/libexec/lib/python3.7/site-packages/botocore/data/lambda/2015-03-31/service-2.json
2019-06-04 15:56:16 Event creating-client-class.lambda: calling handler <function add_generate_presigned_url at 0x7fb1612bcae8>
2019-06-04 15:56:16 Assuming an endpoint for lambda, apa: https://lambda.apa.amazonaws.com
2019-06-04 15:56:16 The s3 config key is not a dictionary type, ignoring its value of: None
2019-06-04 15:56:16 Setting lambda timeout as (60, 60)
2019-06-04 15:56:16 Loading JSON file: /home/linuxbrew/.linuxbrew/Cellar/aws-sam-cli/0.16.1/libexec/lib/python3.7/site-packages/botocore/data/_retry.json
2019-06-04 15:56:16 Registering retry handlers for service: lambda
2019-06-04 15:56:16 Trying paths: ['/home/vagrant/.docker/config.json', '/home/vagrant/.dockercfg']
2019-06-04 15:56:16 No config file found
2019-06-04 15:56:16 Trying paths: ['/home/vagrant/.docker/config.json', '/home/vagrant/.dockercfg']
2019-06-04 15:56:16 No config file found
2019-06-04 15:56:16 Found one Lambda function with name 'HelloWorldFunction'
2019-06-04 15:56:16 Invoking helloworld.App::handleRequest (java8)
2019-06-04 15:56:16 Environment variables overrides data is standard format
2019-06-04 15:56:16 Loading AWS credentials from session with profile 'default'
2019-06-04 15:56:16 Resolving code path. Cwd=/home/vagrant/projects/sam-app/java8, CodeUri=./HelloWorldFunction/build/distributions/HelloWorldFunction.zip
2019-06-04 15:56:16 Resolved absolute path to code is /home/vagrant/projects/sam-app/java8/HelloWorldFunction/build/distributions/HelloWorldFunction.zip
2019-06-04 15:56:16 Decompressing /home/vagrant/projects/sam-app/java8/HelloWorldFunction/build/distributions/HelloWorldFunction.zip
2019-06-04 15:56:16 Skipping building an image since no layers were defined
2019-06-04 15:56:16 Trying paths: ['/home/vagrant/.docker/config.json', '/home/vagrant/.dockercfg']
2019-06-04 15:56:16 No config file found
2019-06-04 15:56:16 Trying paths: ['/home/vagrant/.docker/config.json', '/home/vagrant/.dockercfg']
2019-06-04 15:56:16 No config file found
2019-06-04 15:56:16 http://localhost:None "GET /v1.35/images/lambci/lambda:java8/json HTTP/1.1" 200 None
2019-06-04 15:56:16 Looking for auth config
2019-06-04 15:56:16 No auth config in memory - loading from filesystem
2019-06-04 15:56:16 Trying paths: ['/home/vagrant/.docker/config.json', '/home/vagrant/.dockercfg']
2019-06-04 15:56:16 No config file found
2019-06-04 15:56:16 Looking for auth entry for 'docker.io'
2019-06-04 15:56:16 No entry found
2019-06-04 15:56:16 No auth config found
2019-06-04 15:56:18 http://localhost:None "POST /v1.35/images/create?tag=java8&fromImage=lambci%2Flambda HTTP/1.1" 200 None

Fetching lambci/lambda:java8 Docker container image......
2019-06-04 15:56:18 Mounting /tmp/tmp47cz_0cp as /var/task:ro,delegated inside runtime container
2019-06-04 15:56:18 http://localhost:None "POST /v1.35/containers/create HTTP/1.1" 201 201
2019-06-04 15:56:18 http://localhost:None "GET /v1.35/containers/4226ab8286e3623beafb00a0839155c47936b81ed9d9052bb26ccde80e8b4ff0/json HTTP/1.1" 200 None
2019-06-04 15:56:18 http://localhost:None "GET /v1.35/containers/4226ab8286e3623beafb00a0839155c47936b81ed9d9052bb26ccde80e8b4ff0/json HTTP/1.1" 200 None
2019-06-04 15:56:19 http://localhost:None "POST /v1.35/containers/4226ab8286e3623beafb00a0839155c47936b81ed9d9052bb26ccde80e8b4ff0/start HTTP/1.1" 204 0
2019-06-04 15:56:19 Starting a timer for 20 seconds for function 'HelloWorldFunction'
2019-06-04 15:56:19 http://localhost:None "GET /v1.35/containers/4226ab8286e3623beafb00a0839155c47936b81ed9d9052bb26ccde80e8b4ff0/json HTTP/1.1" 200 None
2019-06-04 15:56:19 http://localhost:None "POST /containers/4226ab8286e3623beafb00a0839155c47936b81ed9d9052bb26ccde80e8b4ff0/attach?stdout=1&stderr=1&logs=1&stream=1&stdin=0 HTTP/1.1" 101 0
START RequestId: c5786d8a-d34f-4a15-92f1-1238c1fe9612 Version: $LATEST
java.lang.ClassNotFoundException: helloworld.App
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    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: c5786d8a-d34f-4a15-92f1-1238c1fe9612
REPORT RequestId: c5786d8a-d34f-4a15-92f1-1238c1fe9612  Duration: 8.63 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 1 MB   
2019-06-04 15:56:20 http://localhost:None "GET /v1.35/containers/4226ab8286e3623beafb00a0839155c47936b81ed9d9052bb26ccde80e8b4ff0/json HTTP/1.1" 200 None
2019-06-04 15:56:20 http://localhost:None "DELETE /v1.35/containers/4226ab8286e3623beafb00a0839155c47936b81ed9d9052bb26ccde80e8b4ff0?v=False&link=False&force=True HTTP/1.1" 204 0

The zip itself is found:

2019-06-04 15:56:16 Decompressing /home/vagrant/projects/sam-app/java8/HelloWorldFunction/build/distributions/HelloWorldFunction.zip

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Linux admnode 4.15.0-20-generic #21-Ubuntu SMP Tue Apr 24 06:16:15 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  2. sam --version: SAM CLI, version 0.16.1
  3. python --version Python 2.7.15rc1
  4. docker --version Docker version 18.06.1-ce, build e68fc7a
awood45 commented 5 years ago

I was able to do this successfully:

sam init --runtime java8 --dependency-manager gradle --name java-gradle-experiment
cd java-gradle-experiment
# Perfomed the changes you described above
cd HelloWorldFunction
./gradlew buildZip
sam local invoke --no-event HelloWorldFunction

Can you upgrade to the latest version of the AWS SAM CLI and try again? I'm going to close for now as this appears to work, but if you continue to see issues feel free to reopen.