Getting started with AWS Lambda Functions by using serverless
In my previous blog post we have learned the basics about Box webhooks. This time we will look more into Lambda from Amazon Webservices, the serverless framework and in the next blog post we will learn how to combine both topics!
What is AWS Lambda in a nutshell?
Lambda provides an easy way to write your business code without making assumptions about your server infrastructure where this code is run.
How to start with AWS lambda?
There are many options to generate and maintain a lambda project in AWS.
- AWS provides it’s own tools with aws-cli and cloud-formation to create lambda functions and manage them.
- Also by AWS, but with a wider approach including frontend projects the amplify framework can be used.
- The serverless framework provides a multicloud concept to deploy serverless functions.
We have chosen serverless to deploy our lambda functions to AWS. In the end you have to go through similar steps:
- Installation of serverless cli via npm
npm install serverless -g
- Register your AWS Key and AWS Secret in serverless
serverless config credentials --provider aws --key <aws-key> --secret <aws-secret>
- Create a new serverless project with an aws template with nodejs as runtime and typescript as language
serverless create --template aws-nodejs-typescript -name smartbox-box-webhook
- Install the dependencies defined in the package.json via npm:
- Deploy your new generated lambda function via serverless to AWS
serverless deploy -v
That was pretty easy, wasn’t it? Maybe you can run into problems the first time. Most of the errors are related to missing or wrong permissions for your AWS user.
The log output of serverless should look like this:
Serverless: Stack update finished...
GET - https:/xxxx.execute-api.eu-west-1.amazonaws.com/dev/hello
Yeah — you have deployed your first lambda function which can get called with a HTTP GET!
To fully understand what happens in the background, login into the AWS console and please take a look into the generated AWS services. The serverless example will generate a lambda function, an API gateway and attaches AWS Cloudwatch to your lambda function.
But you may ask- what the heck of code have we deployed some seconds ago?We take a quick look in the serverless-example folder.
You will see some files — the most important configuration for serverless is the serverless.yml.
The reference guide for the file can be found here https://serverless.com/framework/docs/providers/spotinst/guide/serverless.yml/
We do not need to understand it fully now, but you should recognize that we say serverless that we need a nodejs runtime on aws (provider block) and we want a new function with name hello. The implementation of the function is provided by a document with the name handler and the method hello in this file.
Additionally this function should be exposed via http to the world wide web. For this case serverless will take care that an http endpoint with the path hello and method GET is generated by AWS ApiGateway.
We want to take a look at the handler file which contains the Lambda
The AWS lambda function has one starting point — a handler method which is called when the lambda is invoked, e.g. by calling the AWS lambda via a rest endpoint. The lambda code itself can write a response back to the caller of the lambda by returning it in the callback method.
In this very simple example we only produce a response object which is understood by AWS and call the callback method with the response as second parameter. The response contains a http status code which is send back to the caller of the lambda and a body- in this case we attach the incoming lambda event to the body.
What you can do now is to try out the example in your browser- just hit the url in your browser and you get a similar json response.
In the next blogpost you will learn more about a possible integration between Box Webhooks and AWS Lambda. This includes the developer workflow behind AWS Lambda and Box Webhooks.