AWS Lambda on Java 8 for Alexa Skill. Basic example

AWS Lambda on Java 8 for Alexa Skill. Basic example


This is the basic example how to create AWS Lambda on Java for Alexa Skill There is the nice guideline how to setup this And it has nice examples Pretty simple And we have this “Hello World” With handler and number of individual handlers In this tutorial we will go through all details how to setup this environment First we create an Alexa Skill. And we navigate to developer console Login and select the Alexa section In your console we select Skills and create a new skill In this tutorial we will create some simple example When the Alexa responds to us with a random number So we can call them as a “dice” And to make it a unique name we will call this “MagicDice” And we hit “Next” and then we select “Custom” and create the Skill And now we need to proceed all those steps to complete the building of this Alexa Skill First we create the Invocation name This is how we will refer to the Skill when we ask Alexa It would be “magic dice” And next would be Intent. Intent is a set of utterances (sentences or requests), grouped logically So, this would be one intent in this example And it would be a “RandomNumberIntent” Several utterances – how to request a random number More utterances – better model, more precise model for recognition And then we can build the model Next – we select interfaces Audio Player, Display. We se only Audio Player Next – we choose endpoints, where our logic is located It is recommended to use AWS Lambda, and we will go for this And this is our “Skill-ID” And now we need to create the Lambda for this Navigate to AWS Console Select the section with Lambda functions And we have couple of the. We create a new one- from scratch ..”function”.. And Runtime would be Java And we use “Existing Role” And existing role would be “lambda_basic_execution”, which normally exists already We can hit on “Create Function” And for this function we get this “ARN” Which we will put here In the “AWS Lambda ARN” And we also need to add to this function a trigger In this case it is “Alexa Skills Kit” And it need to be configured And configuration is in this – it requires “Skill ID” And “Skill ID” is exactly this one So we copy it Past it here Hit “Add” And hit “Save” Function is created Click on root node And specify – which handler would have the logic And we need to upload the JAR-file We save endpoints Save model Then we can try to test it When we open this tab – it request an access to the microphone We need to allow this Skill to be tested Tick “Device Log” “Skill I/O” contains: JSON for input, JSON for output It would have the display, emulated for the Echo Show It would have some Device Log Here we can start asking Alexa Sometimes this interface does not respond And in this case the reason might be an antivirus So I go to “Preferences” of the antivirus and disable temporarily the WebShield And ask again And now the request proceed But as long as we do not have an Lambda function – we do not have a correct respond I will turn on the WebShield again Because now it will continue working This is the JSON-input This is how it requests the Lambda handler To process the request – we will use it in .a minute Next – we will need to create the function itself I am opening IntelliJ IDEA Community Which is free You can download it from JetBrains.com web-site This is the “Community” – free edition, for different platforms I would recommend to use ToolBox Where you have all products in one place Now we create a new project The “Basic Java” project, Java 8 And it would be empty And then we need to add dependencies Those two dependencies we need to add Project structure Libraries Maven This dependency would be “Ask-SDK” Then we need to create some logic Here we will create a class, which would handle these requests The name can be arbitrary Implement this interface And this is the Handler This would be the class And this would be the method And it would get the InputStream as an input And OutputStream, where we would need to put our answer And Context has some useful things Like Logger For example it can log an error This handler supposed to be part of the Lambda And here we would put this class name And then we would put the method name And if we have the package For example, we can put it to the package It would be within this package Then we need to put this package as a part of the handler Do not forget to hit “Save” Then we need to respond with the output stream We need to deserialise the input and serialise the output Thera are multiple IOUtils, and we select this one And we need to deserialise it And for this – we will use JacksonSerializer And then we need to create the response And the response would be created by the Skill And we will initialise this in the constructor And we will also initialise the JacksonSerializer in the constructor So we do not create it for every call of this Lambda function And then we need to put our respond to the output stream To create the Skill we need to register some of the Skill handlers Get RequestHandler, and it would be such handler And we need to implement those two methods And this method supposed to specify – if this handler can handle this particular input In this case we always respond “true” And this is the respond itself And this respond can be built using the RespondBuilder Then we will get this respond This respond would be static for a while But we can customise it afterwards We can build now the project It looks fine But in order to put it to AWS Services We need to create the JAR-artifact Go to the “Artifacts” Create “JAR” From this model. Keep this “Main Class” empty Because we do not have it Also get all the dependencies within the JAR-file And I also usually tick this checkbox Every build rebuilds the JAR-artifact Build the project And it also would rebuild the JAR-artifact Which would appear here Here we have the JAR-artifact And I copy the PATH Lambda Management Console Hit the “Upload” button Open this JAR-artifact, choose the file Save it File is saved. And how could we test it? “Configure test event” And for the content of the request We would get this “Skill I/O” JSON input And we would save it And select this one And hit the “Test” Now we have this respond: “Number is 10” Test it again “Number is 10” And now we can test it with the Alexa Skill Maybe we need to reload the page And re-sign-in Allow “Number is 10” Now we have just got the respond “Number is 10” Every time we get this request It is static And now we can give the random number And to make it random We can create this Within boundary of “six” And we add “one” So we get the number Between not “zero” and “five” But between “one” and “six” Dashboard web-page, and upload JAR Save it Test Like this one Alexa responds: “Number is Three” Alexa responds: “Number is One” Alexa responds: “Number is Five” All code would be available within this repository Feel free to use it In case if we want to test it without uploading the Lambda function We can create tests for this Here we have some issue Different versions of Jackson-libraries Here we have the “jackson-core-2.9.4” And here we have e “jackson-core-2.6” I will remove these two libraries – “2.6” And keep this one I need to rebuild artifact So I remove this artifact “From the model” Keep the “Main Class” empty See that we have this Jackson-2.9 Tick the checkbox Build the project And when we have this artifact Copy the PATH Labmda with this artifact Just created Save it “IntelliJ IDEA plugin connector for AWS Lambda” And it is available in the plugin repository I can go to “Preferences” “Plugins” “Browse Repositories” Type: “aws” Hit the “Connector for AWS Lambda” And install it And this is a validated version And you also can always find the freshest version In the releases folder Download the file “Install plugin from disk” “Downloads” “Open” It has the release candidates And most fresh features Then we need to restart the IntelliJ IDEA Then you would be able to see in this plugin here You can put it down And for this particuluar user you can see All the functions Within this particular user And “Refresh” There is the JAR-artifact, already built So we build it in case it is old Build.. JAR-artifact is built Available It is selected And then we just hit the “Update” And then it would be updated So we do not need to login to this web-interface And in “Run” We can get this Put it here And hit “Run test” We will get this number here as well And we can have more information And every time we call this We have these different numbers here And this allow us to reformat this And this – automatically reformats the output And “AWS Log” Allow us to see all the invocations We have the log here And in the log we have Different events And if we have the errors here Which we initially had We can see “ClassNotFoundException”, for example To work with this – you need to create the profile And it is even more information If you expand this part here You can see the role, which is used And when it was modified And what is “ARN” You can change this You can select different funtions And them test them as well Different regions Different profiles And it can work over the proxy as well You need to go to “Preferences” “System Settings” “HTTP Proxy” And you need to put proxy here And when you hit “Ok” And here you see “You do not use the proxy” And when you click this “Sync” button You’ve got this proxy, and now you can work over the proxy Within the corporate network

15 thoughts on “AWS Lambda on Java 8 for Alexa Skill. Basic example”

  1. This is the BEST tutorial I've seen after hours of scraping the web. TURN UP YOUR MICROPHONE to hear him explain EVERY SINGLE step you'll need in order to create an Alexa skill from scratch.

  2. Thanks for the awesome guide!

    I followed your instructions, but unexpectedly faced with a problem
    on the stage of testing the skill.

    Could you please suggest, what is possible reason
    for the issue, I observe during skill test:

    …..

    java.lang.NoSuchMethodError:

    com.fasterxml.jackson.datatype.jsr310.deser.LSR310DateTimeDeserializerBase.findFormatOverrides

    …..

    Error happens on line: RequestEnvelope requestEnvelope = serializer.deserialize(request, RequestEnvelope.class);

    P.S. I double checked that I use proper libraries versions.

  3. Thanks for the great tutorial it clear all of my doubts, but I got an error "import com.amazon.ask.model.RequestEnvelope" not able to import this can please help me

  4. I have a problem, i follow all steps and i have this error when do the test :
    {
    "errorMessage": "Class not found: io.github.satr.alexaskills.MagicDiceHandler",
    "errorType": "java.lang.ClassNotFoundException"
    }

  5. I have a quite basic problem but I dont know how to exactly solve it (I am not exactly using this tutorial but an official template). I dont know if thats important, but I am not using english as the skill language. and when I try to trigger the launchrequesthandler by saying (I translated the following) "open {skill-name}" , I somewhat get a sessionendrequest as an input :/

  6. Could you help me to resolve this Audio only response output. My LaunchRequest successfully delivers output but when I tried utterance configured in custom intent getting Audio only response in Alexa Simulator. But when I post intentRequest as manual Json receiving JSON output.

    Thanks in Advance

Leave a Reply

Your email address will not be published. Required fields are marked *