6 Easy Steps to Deploying Your First Serverless Application in AWS

August 13, 20186 min read

Hello there! Looking for a guide on how to deploy a serverless application? Well, you're in luck because that's exactly what you're gonna get from this article.


But first, some background...

Here at Arcanys, I was recently introduced to this new experimental project. I was to deploy an e-commerce website, both backend and frontend, leveraging the power of AWS Lambda & API Gateway, and taking out the necessity of doing the most menial parts of creating an application. Unfortunately, I didn't have any idea or experience with serverless architecture before. I spent 6 years coding PHP, you see. So my initial reaction to the idea? Mindblown.

But my interest was piqued, and so, like anyone in my shoes would've done, I googled it. But wouldn't you know? I couldn't find a single decent article on how to actually set up and deploy one. I spent 2 whole weeks of working hours and all I could manage to deploy was a simple HelloWorld.js.


Figured it out!


Anyway, fast-forward 4 months into the future, and I am now a full-fledged Serverless or, as they say, DevOps guy. And I'm now able to deploy all resources I need (for projects) in AWS with a single deployment command, which is what I'm about to show you.

So how did I do it? Read on and be enlightened.


Terminology

Some words you will frequently run into when working with serverless.


Serverless Architecture
A software concept in which you can build and run applications without needing to set up or maintain the servers yourself.


AWS Lambda
A compute service that enables you to run code without thinking about servers. AWS Lambda lets you run your code without provisioning any server. Just upload your code and Lambda takes care of everything behind the scenes.

AWS API Gateway
A fully managed service that allows you to create, maintain, and secure APIs at any scale. API Gateway acts as the “front door” for your application and enables it to access data, business logic, and functionality such as code running on AWS Lambda or any web application.

AWS CloudFormation

A free service that enables you to model and provision your cloud infrastructure resources. Serverless Framework leverages this service to deploy your application and handles the dirty job of building the CloudFormation Template for you.

AWS CloudWatch
A monitoring service for tracking and collecting logs from your Lambda to the console log, which can be very useful when debugging your application.

Serverless Framework
A toolkit for deploying and operating serverless architectures, so you can focus on the application instead of focusing on how to deploy it.
In AWS, for instance, with Lambda & API Gateway alone, you can build and deploy your API service right away.



What do you need?


You will need to have a few things set up before you can get going.
Important note: For the purpose of this demo, we’ll be using Node.js as our run-time environment. Most developers, in one way or another, have had some experience using Javascript, so Node is a perfect tool for this use case scenario.


Node.js


If you don’t have Node installed in your host machine, follow the instructions given from this link.


The Serverless Framework


By far, the Serverless framework is the best tool I’ve tried compared to several options you can find anywhere on the net. The framework leverages the AWS CloudFormation to automate the creation of the resources needed for your applications to run in the cloud.

By the way, when we say Serverless Framework or Serverless Architecture, it doesn't mean that your applications will not run on servers per se. In truth, a serverless framework is a tool that enables you to deploy your application to a cloud provider such as AWS or Microsoft Azure, and at the same time, it automatically creates resources defined in your configuration. Serverless architecture, on the other hand, is the idea of how you build the application without needing to think also about the servers. With these, you can simply focus on writing code and deployment. It's called "serverless" not because there are no servers, but because you don't have to think about them.


Getting started

Now that we know what we need, let’s get on with it, shall we?

Note: This basic guide assumes you have more than basic knowledge of JavaScript. If you’re totally new to JavaScript or Node though, it might not be the best idea to jump right into a serverless framework. Master the basics first, then come back!


Step 1: Install Serverless Framework.


Install serverless-cli globally:

$ npm -g install serverless

Test it by running:

$ serverless --help


If all goes well, you’ll see something similar to the image shown below:

You can also find more in-depth information about the Serverless framework from their official website.
Note: You only have to do this once.


Step 2: Set up your AWS Access Keys.


Before you can deploy your application, you need both aws_key & aws_secret_key setup in your host machine for you to be able to authenticate with AWS and allow you to proceed with the deployment process. To get the AWS Keys needed, follow the steps provided here:

// run this command to setup your aws key in serverless $ sls config credentials --provider aws --key {your_aws_key} --secret {your_aws_secret_key}


The Big Finish

And here’s where we set up the “main attraction”, so to speak.


Step 3: Make the Serverless App.


You can create this by doing the following command:

$ mkdir my-serverless-app && cd my-serverless-app 
$ touch serverless.yml
$ touch hello-world.js


Copy this code and paste it in the file you just created.

exports.handler = (event, context, callback) => {
// callback(error, success)
// This tells the lambda to response back to the client
callback(null, {
statusCode: 200,
body: JSON.stringify({
message: 'Hello World'
})
});
};

Step 4: Deploy your Serverless App.


You accomplish this by invoking the following command:

$ sls deploy — verbose


Step 5: Make sure everything works.


Test your API using Postman or some other tool that can do the same.



Congratulations! You’ve successfully deployed your first serverless app in AWS.
You may experience some issues related to permission during the deployment process but that is an easy fix. The framework will tell you exactly what permissions you need to add.


Cool Bonus: Serve Offline


The Serverless Framework has a lot of powerful plugins available on npm such as these:

  • serverless-webpack
  • serverless-dynamodb-local
  • serverless-finch
  • serverless-offline


Step 6: Serve your services offline and invoking your API locally.

$ npm init 
$ npm install serverless-offline -D -y



Add serverless-offline in the plugins section in your serverless.yml file. It should look the same as below:

// start serving your app offline 
$ sls offline start



Conclusion

As a beginner, it’s not so easy to start learning how to be serverless. And it’s quite difficult to find any straightforward instructions on how to do it, too. That's why I felt compelled to write this post (which would be my first) because I'd like to save my fellow developers the hassle and time.

And yes, Serverless Architecture can be intimidating at first. But with the right tools, you can deploy your application without breaking a sweat!

Wanna see how Arcanys can help with serverless? Drop us a line here or hit us up at hello@arcanys.com and tell us about your projects.

Shem (known by many as Ash) is a Web Developer at Arcanys. He has recently discovered the amazing benefits of serverless technology and vows to never look back. He loves taking shortcuts and simplifying stuff, and absolutely has no space for drama in his life. When he's not scribbling codes, he's playing the guitar, reading inspirational self-help books, and watching cool informative YouTube videos (particularly VueConf, Laracon, and CCF channels).

Need a better tech hire option?

Let’s connect