3

I have a project that I'm able to run locally, but it fails on AWS. To run the project locally I do sam build and then sam local start-api --host 0.0.0.0. Then I call http://localhost:3000/skill2/task1 and get the hello world response back. When I call the api gateway https://p4x0n2nemc.execute-api.us-east-2.amazonaws.com/Prod/skill2/task1 it fails. The error I see in cloudwatch is

Class not found: helloworld.App: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: helloworld.App. Current classpath: file:/var/task/

I created the helloworld app with this command and copied it into my AWS Lambda Application.

sam init --name java11-demo-app --runtime java11 --dependency-manager gradle --app-template hello-world

Here is the Github repo for the AWS Lambda Application with the Java resource

https://github.com/bennebbenneb/sample-app

How can I update this AWS Lambda Application so that it creates Java resources in addition to the NodeJS resources? I'm not trying to manually zip up the Java project and upload it. I want the Java code to be built as part of the pipeline.

Local build output sam build

enter image description here

Java Lambda working on localhost

enter image description here

AWS Lambda Applications can be created from the AWS Console. These are CloudFormation projects. https://us-east-2.console.aws.amazon.com/lambda/home?region=us-east-2#/create/application

Error when adding sam build to buildspec.yml enter image description here

Error after removing Architectures config enter image description here

Resources created by the AWS Lambda Application enter image description here

4
  • Are you trying to run both java and node js together? Commented Jan 13, 2022 at 19:22
  • @AnandVarkeyPhilips I'm trying to have the project build one lambda that is NodeJS and another separate lambda that is Java. This is working on my localhost Commented Jan 13, 2022 at 19:34
  • #1 Did you solve your problem? #2 Are you trying to execute java code inside a nodejs lambda on aws? #3 Why not implement it with pure nodejs or pure java? Commented Jan 16, 2022 at 15:54
  • @JRichardsz #1 I understand the problem better now, but it's not solved. I plan to accept the answer below before the bounty expires if there isn't a simpler solution to this. #2 I am trying to create 2 separate lambdas from the project. One lambda being Java and the other being NodeJS. #3 Each lambda is written in only one language. Also there isn't an option to select a Docker image with Java preinstalled, only NodeJS. I'm hoping that AWS allows us to customize the build image in the future to make this simpler Commented Jan 16, 2022 at 21:11

2 Answers 2

1
+100

buildspec.yml if you want to deploy using AWS CodeBuild (would be an unusual flow):

---
version: '0.2'
phases:
  install:
    commands:
    - echo Entered the install phase...
    - npm install --global lerna
    - lerna bootstrap --concurrency=1 -- --production
  build:
    commands:
    - sam build
    - sam deploy --stack-name stackoverflow-stack --capabilities CAPABILITY_IAM --no-fail-on-empty-changeset
      --s3-bucket your-bucket-name-here --parameter-overrides AppId=your-app-id-here

buildspec.yml if you only want to produce a CloudFormation template file which you can directly deploy (using CodeDeploy or other means). It is almost the same as what you already have, with the following difference:

  • Adds sam build
  • Uses sam package instead of aws cloudformation package. sam package uses .aws-sam/template.yaml (output of sam build) instead of template.yaml as the first default.
---
version: '0.2'
phases:
  install:
    commands:
    - echo Entered the install phase...
    - npm install --global lerna
    - lerna bootstrap --concurrency=1 -- --production
  build:
    commands:
    - sam build
    - sam package --s3-bucket your-bucket-name-here --output-template-file
      template-export.yml
artifacts:
  files:
  - template-export.yml

Edit2: OP's build project was created by Lambda applications. The build project uses aws/lambda/nodejs:10-1.0 which has two issues:

  • The SAM CLI version is outdated
  • Java isn't installed because the image is meant to be used for nodejs based Lambda functions. Java is required to build the Java based AWS Lambda function

The following buildspec.yaml can be used to fix both these issues:

---
version: '0.2'
phases:
  install:
    commands:
    - echo Entered the install phase...
    - npm install --global lerna
    - lerna bootstrap --concurrency=1 -- --production
    - yum install -y amazon-linux-extras which
    - amazon-linux-extras install java-openjdk11
    - pip install aws-sam-cli --upgrade
  build:
    commands:
    - sam build
    - sam package --s3-bucket cf-templates-fgoi5xu7d2hs-us-east-1 --output-template-file
      template-export.yml
artifacts:
  files:
  - template-export.yml
Sign up to request clarification or add additional context in comments.

13 Comments

Hey Kaustubh, I've tried adding sam build to the build phase of the buildspec.yml file before and first it said that Architectures is not a valid config value. I tried delete that and then it said nodejs14.x is not a valid Runtime config value. I didn't try going any further than that. Was I on the right track? I updated my question to with the error message at the bottom
Yes, both the errors indicate that the sam cli version was old, since both are features released in 2021. I was actually able to run both the above buildspec.yaml and deploy your resources in my account, the only change I made was commenting out the permission boundary since it is account specific.
For reference, I was using the image aws/codebuild/standard:5.0 for codebuild
Is my project pointed to an old version of codebuild? This project is only a couple weeks old so it should have a pretty recent version already. Do I need to update the version of sam as part of the buildspec.yml build script?
Which docker image are you using in your build project? The default version of SAM probably depends on this. aws/codebuild/standard:5.0 worked for me. You can try updating the SAM CLI version in your buildspec.yaml if nothing else works.
|
0

There are Docker images for Java and NodeJS. I would suggest to use Docker Images as the base for your lambdas.

For Java

Example Dockerfile below:

FROM public.ecr.aws/lambda/java:11

# Copy function code and runtime dependencies from Gradle layout
COPY build/classes/java/main ${LAMBDA_TASK_ROOT}
COPY build/dependency/* ${LAMBDA_TASK_ROOT}/lib/

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "com.example.LambdaHandler::handleRequest" ]

Example Gradle task definition to prepare the runtime dependencies:

task copyRuntimeDependencies(type: Copy) {
    from configurations.runtimeClasspath
    into 'build/dependency'
}

For NodeJS

Example Dockerfile below:

FROM public.ecr.aws/lambda/nodejs:12

# Copy function code
COPY app.js ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.handler" ]

To test both the images locally

To build your image:

docker build -t <image name> .

To run your image locally:

docker run -p 9000:8080 <image name>

In a separate terminal, you can then locally invoke the function using cURL:

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

Reference:

You can also try to create multi layer lambda. Not sure if it works.

1 Comment

My project is an AWS Lambda Application which is built on CloudFormation, but doesn't give me the ability to customize the image for AWS CodeBuild. What I want to do is customize the AWS CodeBuild image so it has both NodeJS and Java installed. Then it can build the NodeJS lambda in addition to the Java lambda. The project I have works locally, but fails to build on the AWS CodeBuild instance because Java is not installed. The other answer goes into installing Java on the build machine, but what I really want is for Java to be there already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.