Alexa and Kubernetes: Alexa Skill as a Web Server (I)
In this article, we will discuss Alexa and Kubernetes and teach you how to create an Alexa Skill using NodeJS Express Web Server.
Join the DZone community and get the full member experience.Join For Free
The very first task we have to do to run our Alexa Skill in a Kubernetes environment is to transform our Alexa Skill backend into a NodeJS app running in an Express Web server.
You can build a custom skill for Alexa by implementing a web service that accepts requests from and sends responses to the Alexa service in the cloud. The web service must meet certain requirements to handle requests sent by Alexa and adhere to the Alexa Skills Kit interface standards.
Here, you have the technologies used in this project:
- Node.js v12.x
- Visual Studio Code
NodeJS Express Server
As Wikipedia says, Express.js, or simply Express, is a backend web application framework for Node.js, released as free and open-source software under the MIT License. It is designed for building web applications and APIs. It has been called the de facto standard server framework for Node.js.
The original author, TJ Holowaychuk, described it as a Sinatra-inspired server, meaning that it is relatively minimal with many features available as plugins. Express is the backend component of the MEAN stack, together with the MongoDB database software and AngularJS front-end framework.
The first step we need to do is install the
express dependency. This will enable us to configure and run the Express Web Server.
npm install express
Once we have installed the Express Web Server, we need to install the official
ask-sdk-express-adapter. This package will help us to run the Alexa Skill instance from the
Alexa.SkillBuilders object in an Express Web Server, creating a request handler to execute all the incoming requests.
npm install ask-sdk-express-adapter
Once we have all the packages installed, we now have to configure it. It is a fact that the Amazon Alexa team has made a huge effort on the express adapter. This is because the transform of an Alexa Skill backends that are an AWS Lambda into an Express Web Server app takes only 4 lines of code.
This is a typical AWS Lambda backend of an Alexa Skill:
And, this is the Express Web server backend of an Alexa Skill:
As you can see above, instead of calling the method
lambda() and exporting that object, we will call the method
create() because that is what is required for the Express adapter. After that, we will create an instance of the express. Then, we are going to set up the Express adapter, creating a new instance of the
ExpressAdapter that has 3 parameters including:
- The Alexa Skill object we have obtained after calling the
- A boolean that enables/disables the verification of the request Signature.
- A boolean that enables/disables the verification of the request Timestamp.
Now, we have the Express adapter created. With the method
post of the
app object, we create a POST listener. This method has two parameters including:
- The URI the Express will listen to.
- The handler will manage all the requests to that URI. Here, we are going to use the handler provided calling the method
getRequestHandlers()of the Express adapter.
Everything is set. Now, we just have to call the method
listen with the port that the Express server will use to run our Web server.
You can find all the code on the
Running and Debugging the Skill With Visual Studio Code
launch.json file in
.vscode folder has the configuration for Visual Studio Code, which allows us to run our Express Server locally like so:
After configuring our launch.json file, it is time to click on the play button and make our request to our Express server.
After executing this, you can send Alexa POST requests to http://localhost:3000.
Testing Requests Locally
I’m sure you already know the famous tool called Postman. REST APIs have become the new standard in providing a public and secure interface for your service. Though REST has become ubiquitous, it’s not always easy to test. Postman makes it easier to test and manage HTTP REST APIs. Postman gives us multiple features to import, test, and share APIs, which will help you and your team be more productive in the long run.
After running your application, you will have an endpoint available at http://localhost:3000. With Postman, you can emulate any Alexa Request.
For example, you can test a
- The Official Node.js SDK Documentation
- Official Alexa Skills Kit Documentation
- Express Adapter Documentation
As you can see, with just 4 lines of code, we can transform an Alexa Skill by using AWS Lambda into an Express app, all thanks to the Amazon Alexa team and its fabulous adapter.
I hope this example project is useful to you.
You can find the code here.
That’s all folks! Happy coding!ver
Opinions expressed by DZone contributors are their own.