I’ve played with deploying AWS lambdas manually, and I’ve played with SAM. Now is time to play with Serverless. The good thing with Serverless is that it is cloud-agnostic. Therefore, it can be used to deploy on a lot more platforms than simply AWS. Of course, all the Alexa skill linking will be AWS-specific. So here is how I ended up deploying an AWS lambda for Alexa using Serverless. As always, the code of the associated project can be found on my GitHub page.
Getting ready to use Serverless
Again, I had issues installing Serverless, pretty much the same as with SAM. Basically, it is not pre-built for a Raspberry Pi:
1 2 3 |
┌─[pi@raspberrypi]─(~/repos/experiments/nodejs/alexa_serverless) (master) └─[23:03]$ curl -o- -L https://slss.io/install | bash > Sorry, there's no serverless binary installer available for this platform. |
The fix, again, is to bypass the installer and install using npm: npm install serverless --global.
Running Serverless on a Raspberry Pi
Besides the installation of the framework, I could not find any issues running it. After generating a test event using SAM, I could even invoke the function locally using:
1 2 |
┌─[pi@raspberrypi]─(~/repos/experiments/nodejs/alexa_serverless) (master) └─[01:14]$ serverless invoke local --function storage-function --path test.event |
Preparing the project
Bootstrapping the project was quick and simple:
- run serverless (no arguments), which prompts a few questions and generates a basic project
- copy all files from my original project to the newly created directory
- delete the useless deploy scripts from before
Writing Serverless template
Writing the deployment template for Serverless was way easier than using SAM. It is a bit weird when you think about the fact that SAM is supported by AWS and only supports AWS. The two issues I got when using SAM were fixed trivially.
The first one, selecting a retention policy for the Log Group is as simple as adding a parameter:
1 2 3 |
provider: [...] logRetentionInDays: 5 |
The second one, linking to the Alexa skill, is done again using a simple parameter:
1 2 3 4 5 |
functions: storage-function: handler: lambda.handler events: - alexaSkill: amzn1.ask.skill.xx-xx-xx-xx-xx |
Deploying everything
Deploying the skill is done through the serverless command: serverless deploy --region us-east-1. This command will create a Cloud Formation stack for your function.
Removing your stack is again done directly through the serverless command: serverless remove --region us-east-1.
Comparison with SAM
Overall, Serverless seems easier to use and have more feature than SAM. Of course, my only testing of the frameworks is these few posts, so it is possible that things change over time.