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

Java Lambda working on localhost

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

Error after removing Architectures config

Resources created by the AWS Lambda Application

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:
sam buildsam 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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With