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.

Learn more here:

You can write code in your favourite language like Javascript or Java and AWS will provide a runtime for it. In the end your code and some metadata infos are uploaded to AWS and AWS provisions the infrastructure where your lambda function runs on. In this context the serverless term makes sense because you do not care about servers in this scenario.

How to start with AWS lambda?

There are many options to generate and maintain a lambda project in AWS.

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:
npm install
  • 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...
Service Information
service: smartbox-box-webhook-lambda
stage: dev
region: eu-west-1
stack: smartbox-box-webhook-lambda-dev
api keys:
GET - https:/
hello: smartbox-box-webhook-lambda-dev-hello

Stack Outputs
HelloLambdaFunctionQualifiedArn: arn:aws:lambda:eu-west-1:601320840799:function:smartbox-box-webhook-lambda-dev-hello:1
ServerlessDeploymentBucketName: smartbox-box-webhook-lam-serverlessdeploymentbuck-ya62w89cu8ri

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.

Lambda Function in AWS-Console

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

our serverless definition

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.

HTTP Response of the example lambda.

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.