Image for post
Image for post

How to build a shortlink service for AWS using Serverless Framework (Part 1)

Aleix Murtra
Oct 7, 2017 · 4 min read

Hi! My name is Aleix Murtra, and I’m a DevOps engineer born at Barcelona. I’m a passionate cloud services worker. I’ve recently learnt how to build serverless applications using AWS infraestructure and I want to share it as best as I can. This is my first post :-)

Installing dependencies

First of all, we need to install all the dependencies. As we will use serverless framework, and it uses NodeJS, it’s mandatory to install it.

Also we will need an AWS account, and API Keys with access to DynamoDB, Cloudformation, API Gateway, Lambda, and more…

First steps with Serverless Framework

Once both things are installed, it’s time to create your first Serverless project. We will named it shortlinks project, and we will use aws-nodejs template.

This will create some files, the most important are the next two:

  • serverless.yml: This file has the configuration of all the service. Here we describe all the service that we want to create, and how they interact with other services like API Gateway, DynamoDB, Cloudwatch Events, and more!
  • handler.js: Here we have the entry points of all services we’ve described before at serverless.yml. For instance, we have a Hello service which calls handler.Hello function inside handler.js file.

Before starting, I’ve made the following changes into both files:

  • Rename service name to shortlinks.
  • Specify stage to pro.
  • Specify region to eu-west-1, as I use this zone for all services. Serverless will use us-east-1 by default (I guess).
  • I’ve also changed Hello function to health. I’ll use it to know if the service works. Look that I’ve enabled an http event that will listen /health.

And now, it’s time to deploy it to AWS using the next CLI execution. It’s easy, isn’t it?

At this time, it will translate your YML file and will create (or update) an AWS CloudFormation template. This will create a Lambda function for every function you have declared into YML file. Also it will create an API Gateway configuration that will listens /health path. All of this will be made only with 16 lines of code! It’s pretty awesome :-)

Image for post
Image for post

Let’s create some stuff

Okay, now think which services you’ll need to create a Shortlink service. Yes, at least two:

  • A POST endpoint to store a link. It will receive a parameter with URL you want to link, and this function will store it to database and will return an ID.
  • A GET endpoint to access to this link. We will map /to/${id} to redirect it to its related link into database.

Now, you may ask me about that database. Where can we store that data? Yes, at AWS. We will use DynamoDB as a database.

serverless.yml — save function

Save will be the function that will stores our entry to our database. We will declare it as follows, inside functions block. A POST call to /save will trigger method (declared into handler.js).

serverless.yml — go function

This will be the second function. It will listen /to/${id}. It will ask for a link with an id equals to ${id}, and will make a 301 redirect to that. Look that we’re enabling CORS.

serverless.yml — DynamoDB Resource

Serverless Framework allows us to put some piece of CloudFormation code inside YML file. Doing this will improve our project as we save all dependencies or resources in the same file, which can be gitted. Yes, we could create another CloudFormation file with this resource, but it’s better to centralize all infraestructure in the same file. Resources piece of code are in the same level as functions. More info here.

Ok, so we’re creating a DynamoDB database with a unique parameter called id, which would be the key. Also we provision a Read/Write capacity units of 1.


Before finishing this post, we’re going to create the functions that we will need. Note that I’ve copied health function.

Deploying application

Now, it’s time to deploy our application. It will create a DynamoDB database, three lambda functions (with their CloudWatch Logs configuration) and three endpoints into an API Gateway property.

Image for post
Image for post
Deploying application


You can see the development of this post here:

At the next post we will configure our functions to save an retrieve data from DynamoDB, as we will make some tests with Postman.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store