Writing Less Serverless code with Vandium

An important part of developing a production level system, involves controlling the pipeline. Developers need to have an automated system to build and deploy their code, using CI/CD tools which can not only automate the process, but automatically stop the process if something goes wrong. While there is already an abundance of CI/CD tools out there, these tools require the actual build process to be automatable in the first place through CLI tools. In addition, it is simply much handier for a developer working on a project locally to have a CLI tool for deployments, rather than manually uploading each code change to AWS through their web console. In the world of AWS Lambda functions, The Serverless Framework has come out as a clear frontrunner. We will now walk through the process of creating a Lambda project using The Vandium Framework, and then deploying it with Serverless.

Getting Started

Note: This tutorial should be done using Node.js version 6.10.2. If your main development environment uses a different version of Node.js, an easy way to switch between versions is to use the Node Version Manager library (NVM). It is available as a package on npm for global installation.

To start our project we first need to install The Serverless Framework. As with all global Node.js packages, run npm install serverless -g (Linux may require this command to be run as sudo). Verify the installation by running sls -v.

Now we can start implementing our project. In a new, empty directory, run npm init, and accept the default values as prompted (unless you want to specify different values).

Now that we have created a standard Node.js project, we need to make it into a Serverless project. Run sls create -t aws-nodejs. This will generate a boilerplate YML file describing the service, and a boilerplate JavaScript handler file, where your code can be written.

Finally, we must install The Vandium Framework as a dependency of the project before we can write any code. Run npm install vandium --save.

Design

For our sample project we will be building an API that does user administration (create/get/update/delete user accounts). To keep the demo simple, we will not actually read and write to a database, but simply return back to the client the changes that would be made. If the reader wishes they may further extend this project to include a database. Our example API will have 4 different operations. Retrieving, updating, and deleting a user will require the user’s ID as a URL parameter, while creating a new user won’t require any URL parameters. Therefore the path /user/{userId} will be assigned GET, PATCH, and DELETE methods. The path /user will be assigned just the POST method.

Creating our Handlers

Delete the default handler.js file created, and create two new files named user.js and userUserId.js.

The code in user.js will handle user creation, and therefore accept only POST requests.

The code in userUserId.js will handle retrieving users, updating users, and deleting users. Therefore it will accept GET, PATCH, and DELETE requests.

Now that we have created function handlers with The Vandium Framework, we can use The Serverless Framework to map them to the API we are creating. This is done by editing the serverless.yml file, already created.

Each function has an event for each HTTP endpoint it is associated with. Each endpoint is defined by both a path as well as an HTTP method. This file also specifies other global parameters such as which runtime is used (nodejs6.10) as well as the name of the service. We chose vandium-serverless-demo for this project.

Deployment

Now that we have completed writing both the code and the configuration file, it is time to try out a deployment. Run the command sls deploy (this may take a few minutes as it uses CloudFormation). Once your project has been deployed, you can sign into the AWS Console, and see what has been created using the CloudFormation, API Gateway, and Lambda consoles. Now that we have viewed the resources that we have created, it is time to test them out!

A list of endpoint URIs will have been printed in the terminal after sls deploy was run. It should look something like:

Testing

These endpoints can be called using whatever HTTP client testing tool you see fit. We recommend using Postman since it offers a simple GUI, which can easily be configured to use different HTTP methods as well as creating a JSON body on an HTTP request.

Open Postman, set the URI to the /user address, and set the HTTP method to POST. Under the Body tab, select the raw radio button, and use the adjacent dropdown menu to select JSON (application/json). Enter a sample body such as

{ "name": "john doe" }

Click the send button, and less than a second later an HTTP response should arrive with the values you sent up.

Postman testing tool

Testing the other path is similar. Enter the same URI as before with /12345 appended to the end. Switch the HTTP method back to GET. Click the send button, and some user information should be returned. Note that since this is only for demonstration purposed is always the same no matter which ID is used.

The other endpoints on the /user/{userId} path can be tested in a similar manner to the other tests described above.

Wrapping Things Up

Combining code written with The Vandium Framework and deployed with The Serverless Framework is an ideal way to build AWS Lambda functions, and associated event triggers. Vandium allows the code to be written cleanly and elegantly, while Serverless allows for deployments to be made simply, and systematically.