question

Miguel Berger avatar image
Miguel Berger asked

Lambda Input for Java

Hey, In a lambda function written in Java you need to write a handler. Does anyone know what the input for that handler should be? I know it is supposed to be a JSONObject but when I try to use that I get an error about not being the correct type. Thanks for your help
alexa skills kitdebugging
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jjaquinta avatar image
jjaquinta answered
Lambda Java use is not well documented in Amazon's (otherwise excellent) docs. I've got a sample and some steps up here: https://github.com/jjaquinta/EchoProofOfConcepts/tree/master/jo.echo.lambda It is the same JSON that comes into it as a web service. And you need to response with the same JSON. The steps above adapt the classes in the Alxea Skill Kit, though, so you don't need to worry about that. You can just use the speechlet class.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Miguel Berger avatar image
Miguel Berger answered
Thanks, that code is really helpful to look at, but I must have a different version of Eclipse than you because half of your steps don't show up. Such as “Project Layout” and "Java settings" when creating a new AWS Lambda Java project. I am using Version: Mars Release (4.5.0). Also I have tried following your instructions on getting even the Hello World speechlet to run and I have been unable. I zip the original files into the root of my zip file and then upload it and only get "errorMessage": "Class not found: helloworld.HelloWorldLambda". I copied the lib file directly from your source so I am pretty sure it isn't that, but maybe I am not understanding something from your instructions and am doing it wrong. Any help would be appreciated!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Miguel Berger avatar image
Miguel Berger answered
Never mind about my previous response, I had to play with how I was exporting things. Got it to work now. Thanks for your help!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jjaquinta avatar image
jjaquinta answered
No problem. I though I was dealing with the latest Eclipse. I'll have to check. I'm doing the final edits on (the first release of) my book tonight. (Those steps are part of one chapter.) I'll review them and perhaps add in more explanation of *why* rather than just *what*. If I don't get it done tonight, I'll put it into the next release. (Aiming for a release every week or two.)
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

bballp avatar image
bballp answered
I having a problem with my Lambda function for the echo and I think it has to do with how I'm packaging my app. Can you please send an example for how the zip file should look and what it should contain.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jjaquinta avatar image
jjaquinta answered
The forum doesn't allow attachments. :-) But it's pretty simple. You just need it so that your ZIP file contains the directory structure of the packages of your class files.And, if you have any dependent jars, they are in a root directory called "lib". So, if you have a class foo.bar.HelloLambda, that needed a wibble.jar file, your zip file would look something like: foo/ foo/bar/ foo/bar/HelloLambda.class lib/ lib/wibble.jar Make sense?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

bballp avatar image
bballp answered
Yes, so I basically export the lib and bin files from eclipse then take the necessary files out if the bin file?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jjaquinta avatar image
jjaquinta answered
It depends on how you've set up your project in Eclipse. If you put your source in the root directory, and make a lib folder in the root as well, you can zip it up directly. (Or export and zip.) Otherwise if you take the default, yes, zip on the contents of bin and the lib dir. (Note, you need to include the lib dir itself, and you don't want to include the bin dir.)
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

shadoof avatar image
shadoof answered
@jiaquinta, Many thanks for your answer on this thread. It's the closest to addressing my problem(s). I've done my best (I'm no pro) to study your code. You seem to be manually zipping and uploading rather than using the facilities built in to the current AWS Toolkit for Eclipse, right? Whereas, I'd like to use the project right-click 'AWS Upload function to Lambda ...' functionality but have not yet been able to get this to work for Alexa Skill Kit projects. Although you do have a handleRequest method, it does not have the same signature as the one demanded by the upload and you do not declare "implements RequestHandler" for your HelloWorldLambda class and so the AWS uploader currently reports: "No Lambda function handler class is found in the project. You need to have at least one concrete class that implements the com.amazonws.services.lambda.runtime.RequestHandler interface." I hope this makes sense to you and if you have any advice for me, it would be much appreciated, since I don't have the chops to correctly adapt your code as it stands. (Let's have better Java for ASK Lambda and Eclipse, as you say.)
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jjaquinta avatar image
jjaquinta answered
The current Lambda plugin for Eclipse is broken. That's why you can't get it to work. :-( I checked with this week's update, and it's still broken. Amazon, as with everything SDK, is tight lipped about when a fix can be expected. In the meantime, all you really need is a class in your ZIP file with a no-argument constructor, and a method that has this signature: [code]public void xxx(java.io.InputStream inputStream, java.io.OutputStream outputStream, com.amazonaws.services.lambda.runtime.Context context) throws java.io.IOException[/code] The name of the function doesn't matter. Just the profile. There are other profiles that are supported by [i]Lambda[/i]. Which is why the default code handler creates a class that implements RequestHandler. However, by experiment, those profiles don't work with [i]Alexa[/i]. Their documentation isn't very clear about that. I started by first creating a class with that profile, that only read all the content of the input stream, and then sent it to the output stream. I validate this from the Lambda console. Then I wrote one that ignore the input, and then output a fixed JSON string that was a correct invocation output. After validating this from the Lambda console, I then wired it up and invoked it from Alexa. Once that worked, I was happy my infrastructure was working, and went on to writing some handling classes. If you can't get my stuff to work out of the box, you might try this incremental approach to work out what's missing. If I've forgotten a step, please let me know!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.