Rachelle Rathbone

Building Your First Serverless App - Part 1

Creating a Simple Lambda Function and Deploying Your Service

December 20, 2020

Before we kick things off there are a couple of things you'll need to take care of yourself if you don't already have these set up:
  1. Install Node. If you already have Node installed, check what version you are running before proceeding. Serverless is on its second version and is only supporting versions 10 or higher of Node.
  2. Install the AWS CLI and configure it
  3. Create an AWS account

As mentioned in the description, we're going to be working with the Serverless Framework, which is open source and makes it possible to easily build applications with AWS Lambda. To get started the first thing you need to do is install the Serverless command-line on your machine by running the following npm command:

npm install serverless -g

Creating Your First Serverless App
Pick a directory on your machine where you'd like to start building out your first Serverless microservice which is going to be for a make believe store. There are obviously several aspects we could focus on but for the purpose of this series of posts, we'll be setting our sights on our customers. From your chosen directory run the following command:

serverless create --template aws-nodejs --path make-believe-store-service --name customer

You should see the following:


If instead you see something like this: zsh: command not found: serverless it means you're running a version of Node that is not supported by the serverless framework (anything below version 10).

Once your microservice has been successfully created, if you cd into the directory and open up the contents in your favourite text editor you should see the following three files:
  1. .gitignore: with a files added for npm to ignore and keep out of the package.
  2. handler.js: this is where a generic Lambda function has been declared. We're going to change up the structure of this.
  3. serverless.yml: if you look at the automatically generated contents of this file you'll notice it's quite long with a lot of commented out code. We'll be deleting most of this in the next step and adding our own code. This is where we will declare the configuration that the framework will use to declare our service.

Updating serverless.yml to Create Our Customer Endpoint
Go ahead and remove the entire contents of the yml file. You can do it the slow way and drag your mouse over all the code and then hit backspace or, you can use cmd + a (ctrl + a for the Windows users out there) to highlight everything in one fast swoop.

Now type the following into your empty file (sorry it's a screenshot and you can't copy and paste from here. Contentful's formatting for large blocks of code is laaaaaaammmmme. However, if you do want to simply cope and paste, you can get the source code here):


Let's walk through each part of this file in a little more detail:
  • service: here we are simply declaring the name of our service. You could call this whatever you wanted.
  • provider: in this section we state aws as a provider and declare our region and Node version. If you are using a different version of Node, go ahead and change the 12. Likewise, if you are outside of the us-west-2 region, update this with the right region for you.
  • functions: we'll be working on this throughout this series but to kick things off we have created our very first Lambda function called saveCustomer and pointed to a path that doesn't exist (yet... that's up next). We will use this to post new customers to a /customers endpoint. We've also added cors: true to handle preflight requests. We'll get more into the nitty gritty of cors and serverless in a later post.

Getting Rid of the Generic Handler File
If you want, you can keep all your functions in the one file and export them from there but I think we all know that isn't entirely 'hygenic'. To keep things a little cleaner, go ahead and completely delete the handler.js and create a new directory in the root of your project called api. Inside this new folder, create a file called customer.js then type up the following code:


We'll dive more into this code in an upcoming post when we work on wiring our function up to Dynamo. For now, we are essentially just returning a 200 status code and a message if the post against our /customers endpoint is successful.

Once you've got the code in customers.js and serverless.yml, run serverless deploy or sls deploy to deploy your function via AWS's CloudFormation. Both commands do the same thing, sls is just the shorthand version of serverless.

You should see a .serverless file in the root of your directory, along with the following in your terminal:


You can see the name of our function saveCustomer as well as the creation of the customers endpoint.

Before we wrap things up. Let's test that everything is working correctly by running a curl command from the terminal. Copy the following but be sure to update it with your unique domain that was generated:

curl -X POST https://38vvslkaz0.execute-api.us-west-2.amazonaws.com/dev/customers | json_pp -json_opt pretty,canonical

The last part of the above command | json_pp -json_opt pretty,canonical will give you back a more readable json object. The object you get from running this curl command should be pretty big but there are 2 key things you want to look for. The body property (this should currently be null as we aren't currently sending anything in the request) and the message property which will have the success message from customer.js.


Nice work on getting this far. In the next part of this series I'll show you how to test this same endpoint from and incredibly useful tool called Postman.
...
© 2023, Rachelle Rathbone