New to cdk, trying to use the tutorial with a JAVA handler and fail
Closed this issue · 11 comments
❓ Guidance Question
The Question
I was following the tutorial and at first write js handler and everything works fine. Later I want to try write a Java handler and test it. I added the java file in the same lambda directory with package name example, class name MyHandler, writing a simple java handler returning the same info as the js handler. I then go back to stack and change the props, change Runtime to JAVA_8, change handler to example.MyHandler (also tried example.MyHandler::handleRequest), and then deploy it.
But when I try to test it on lambda console, it gives ClassNotFound error, it can't find my example.MyHandler class. Anyone know what's wrong? or is it possible to add a handler version in Java to the tutorial?
Environment
- CDK CLI Version:
- Section:
- Browser:
- Language:
Other information
Hmmm I'm running into this issue as well @ydl1991, I've never created a lambda function in java before though so I'm not sure if I'm doing something wrong. I'll see if I can figure this out
This is due to Java being a compiled language. You can't deploy the code as simply as you can with js or python; you need to deploy a compiled program and its dependencies in a container or zip file.
Here's an example which does this with the CDK: Link to Stack, Link to Function
I hope this helps. We have it on our agenda to have each workshop deploy the lambda function in the same language as the workshop 😄
Thank you so much for explaining it! And yes, having each workshop deploy lambda in the same language is definitely a big help for newbies like me!
⚠️ COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
No problem @ydl1991, do you still need any help with this?
I just started looking into it, regarding how to deploy the function code as compiled program in container or zip, is it the step showing in this page? https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-package.html
still not quite get on how to mannually upload the deployed package correctly. I follow this post: https://docs.aws.amazon.com/lambda/latest/dg/java-package.html, and build the deploy package using Maven, which generates a jar file. then I go to lambda console and mannually upload the jar file to the source code section. And the test still fail.
Is there any documents you are aware of that shows the process of deploying a compiled program and in container or zip? @peterwoodworth
Gonna be honest with you @ydl1991, I've never actually deployed a lambda function in Java before. So unfortunately I won't be of much help here. Here's a tutorial which looks helpful: https://www.baeldung.com/java-aws-lambda
@peterwoodworth thanks , I was just thinking that if my handler code need to be in a seperate package from my cdk app stack package? cuz we need to compile just the java handler code itself and deploy the jar file, right?
Were you able to figure this out @ydl1991? Sorry for leaving you hanging here
Hey @ydl1991,
Did you figure out the asset deployment for Java-based lambdas? If you haven't, consider checking out the Java examples in the cdk examples repo