Hey Alexa, build me a K8S Cluster
The first time I saw the Amazon Echo device, I won’t lie I thought to myself, why would anyone buy this device to just check the weather. My good friend Amit Jotwani, who had just started at Amazon on the Alexa Evangelist team, decided to knock some sense into me by gifting me an Echo dot for Christmas. And with that, I was totally hooked! As of writing this blog, Alexa plays music to keep our pup calm, operates our lights, arms the security system and so much more.
A few months ago, I was brainstorming ideas for my VMware Code Session at VMworld and I thought to myself, it might be interesting to build an Alexa skill that leverages API’s from the VMware Cloud Services platforms such as VMware Cloud PKS to build Kubernetes clusters or for Network Insight to look up AWS EC2 instance details amongst others.
I big shout out to Amit and his team for pointing me in the right direction. It’s been a fun learning experience. Let’s dive into how this Alexa skill is built. Before we get started, to give you an idea, here’s the sample interaction:
Me: Alexa Open vke
Alexa: Welcome to VMware Cloud Services Alexa Skills Code Session, what can I help you with?
Me: build me a vke cluster
Alexa: Sure, I can build a VKE Cluster for you. It’s pretty dope that I can do Kubernetes the easy way! What would you like to call this cluster? ?
Me: Call it John
So with that, let’s get started! Your first stop is to get a developer account on the Amazon Developer portal. There is a pretty neat skills kit available here: https://developer.amazon.com/alexa-skills-kit. The Alexa team has also put together some amazing Getting Started kits on their GitHub page, do check it out: https://github.com/alexa/. You’ll also need an AWS account and a VMware Cloud PKS account. As of this writing, there is a sweet $150 credit that can be applied to your K8S clusters. Here’s the sign-up link: https://cloud.vmware.com/vmware-cloud-pks
Now that we have the basics out of the way, I’m going to break this into two areas. First, the skill itself and second the AWS Lambda function that makes the API calls to build the clusters.
Let’s look at building the skill itself. The simplest way to get started is to hit Create Skill. It will ask you for a skill-name, here I choose to call my skill vke-skill.
Once created, we’re ready to start building. There are a few key things we need to do to get this skill configured. I’m going to breeze through the description in the interest of time/length of this blog, feel free to dive into details on the AWS/Alexa documentation links for each of these areas.
Invocation Name — this is the name that you will use to “invoke” the skill. For e.g.: Here I use an invocation name: vke, which translates to *Alexa, open vke *to start the skill. Tip from Alexa folks, use short invocation names.
Intents/Samples/Slots — Frankly, I’m not going to do this topic any justice by summarizing since there’s so much to read and learn. But, in short, intents trigger the workflows. I’ve built two intents, first, one is called *VKEBuildIntent, *which corresponds to sample utterances as shown in the screenshot below.
The second intent is called *AnswerIntent. *The intent is built to capture the name the user wants to provide to the K8S cluster. To keep this simple, I’m just taking a name from the user and building the cluster. In VMware Cloud PKS, you have the option to build a Developer or Production Cluster, provide CIDR blocks among other parameters that can be passed in the API call. As you’ll see in a bit, this is intentionally kept simple for illustration.
The *AnswerIntent *leverages “slots”. Think of slots as user provided inputs. Here I’ve defined slots be names. You have the option to use either user-generated slots or Alexa pre-defined slots. Trust me when I say there are a ton!
Once you have that built, we’re ready to define the endpoint that will host and service the skills endpoint. You guessed it — yes that’s the AWS Lambda function. Copy the skill-id that you see there and hop over to your AWS Account and open up Lambda.
The Lambda function is called *vke-lambda. *Your initial screen will look a little different since I’ve already configured the Alexa Skills Kit trigger. If you click on the Alexa Skills kit, you’ll see where to enter the skill-id that you copied in the step above. Also, this step is pretty easy and I’d rather not expose the ARN on a public blog :)
Once you have the Alexa Skills Kit trigger setup, copy the Lambda ARN from the top right and paste it back in the Alexa Developer Console under Endpoints. Lastly, don’t forget to Build Model. This is an important step and always hit “build model” if you make any changes to the above steps. This will make sure you’re always using the latest and greatest intents, utterances.
Now that you have that framework done, let’s dive into the actual AWS Lambda function that we’re going to be using. If you’ve come this far, that’s your basic framework for pretty much every Alexa skill. Give yourself a nice pat on the back and grab a beer to dive into the code.
The code I’m going to walk through below is working of the framework (yes, channeling my inner Kelsey Hightower) from the Alexa Fact Skill and Alexa Memory Game. I’m leveraging Node JS for my Lambda function, but you’re obviously free to use your favorite scripting language. I’m also going to leverage the *ask-sdk-core *package that will allow for simpler handling and functions. So with that, let’s look at two of the functions. First, let’s look at the LaunchRequestHandler.
This is largely broken into two parts — *canHandle, which *matches the “intent” type. Here we’re looking at the *LaunchRequest *and the following that is the handle function that defines the speech output. I have a constant value defined for the *Welcome_Message *attribute as shown in the sample interaction above.
Let’s now look at the second function, *VKEClusterInitHandler. *This function handler is what I’m using to correspond to *VKEBuildIntent. *I chose to call it cluster-init because we’re actually just initializing the cluser build process using this function. The code is mostly the same as above so I’m skipping detailing the various steps. Let’s now dive into the VKEBuildClusterHandler
In this function, the first step is to grab the name from the session. The *answerslot *attribute takes that value from the session and passes it down in the API body as the name and displayName fields for the API call. That brings us to the next package that is being used here. There are quite a few ways to do this, either using request, https npm packages. I chose to build using axios. I found it to be very user-friendly and easy to use.
Details on the VMware Cloud PKS (VCP) API is available once you log in under the Developer Center. It also details out steps to configure using the VCP CLI and also provides details on the API itself. P.S: yes there is a Dark Mode available in the console!
Getting back to the API call itself, the POST API call requires you to pass a few items. Firstly, you require the organization ID from VMware Cloud Services. This can be found by clicking on the top-right by your name and then clicking once again to expand the org-id.
Next, you need to provide the region where the clusters are going to be launched, in my case, I choose to launch in us-west-2. I have to pass an authorization token and a few parameters in the body. Let’s look at those quickly.
The name and *displayName *fields are coming from the slot attribute that we had defined. The folderName is vcs-cts and *projectName *is pbarathi. So what are folders and projects in VMware Cloud PKS? Projects and Folders allow you to manage your clusters and access privileges in a way that mirrors your business structure. For example, clusters belonging to the R&D business unit cannot be accessed by people from the Finance business unit and vice versa. Once you have defined the Folders and Projects, we define what kind of cluster we’re looking to build. In this case, I’ve kept it really simple and just building a Developer cluster and defined what version of the cluster I’m looking for. The Developer cluster is the bare minimum — it is built in a single AZ, single master node and shared networking.
So that’s it! That’s how it’s done. We’ve just scratched the surface here. Since there’s a lot more that can be done on both the Alexa skills kit side and also VMware Cloud PKS side. Check out some of the links below for more details. I will post the entire code on my GitHub page post re-Invent.
Also, I will be presenting this in more detail at AWS re-Invent 2018 Las Vegas at the VMware {Code} Session on Monday, November 26th at 5 pm. See you then!
Some links that helped contribute:
Memory Challenge Game: https://github.com/alexa/alexa-guided-walkthrough-using-node-sdk/blob/master/part-2/index.js
Node JS Fact Skill: https://github.com/alexa/skill-sample-nodejs-fact
Alexa Skill Recipe: https://developer.amazon.com/blogs/alexa/post/a9ef18b2-ef68-44d4-86eb-dbdb293853bb/alexa-skill-recipe-making-http-requests-to-get-data-from-an-external-api
Slots and Session Attributes: https://developer.amazon.com/blogs/alexa/post/f167aa0f-8abe-4602-b985-65118b3032ca/code-deep-dive-slots-and-session-attributes-in-the-ask-sdk-for-node-js
VMware Cloud PKS Getting Started:https://docs.vmware.com/en/VMware-Kubernetes-Engine/services/com.vmware.kubernetesengine.getstart.doc/GUID-A4BB2D93-6A19-4637-8135-7D66BC29165C.html#GUID-A4BB2D93-6A19-4637-8135-7D66BC29165C