Serverless Course - Lesson 1: How to get started tutorial for beginners

duomly

Duomly

Posted on September 2, 2020

Serverless Course - Lesson 1: How to get started tutorial for beginners

This article was originally published at https://www.blog.duomly.com//lesson-1-serverless-how-to-get-started-tutorial/


Intro to serverless how to get started tutorial

Today we will go through the Serverless how to get start tutorial.

In the previous episode, I've told you what is serverless, why you should use that, and how you can save even 90% of your IT infrastructure costs.

You can find the URL here:

What is serverless

I've promised you, I will teach you how to build Serverless Node.js applications, and today we start the Serverless course.

In the first lesson, I will teach you how to install necessary dependencies, configure the project, and set up the first serverless template.

Let's start!

And if you like video, here is the youtube version:

How to install serverless and AWS CLI

In the first step, we need to install the main engine, which is serverless.

Open your terminal and type:

npm i -g serverless

Next, we need to install AWS CLI that will take responsibility for creating our infrastructure in the AWS Cloud.

There are few methods of installation depends on the OS.

In all systems, you can install that by pip:

pip install awscli

In Mac OS and Linux, you can use homebrew:

brew install awscli

How to configure AWS CLI

The next step in our Serverless journey is the AWS CLI configuration.

We need to have an access key, secret key, and region(it's optional, but better to set up the default one).

After creating a new AWS user, access, and secret key, you can get from the AWS IAM service.

Next, you need to open the terminal and type:

aws configure

How to create serverless project

When our AWS CLI is configured, we can focus on creating a project.

Today we'll focus on the Serverless Framework and use this one to build our app.

As the first step with that, we need to create our project, define the template's template, and name the project.

Open terminal and type:

sls create -t aws-nodejs -p duomly-serverless-course

Now, you can go into your project:

cd duomly-serverless-course

How to init package.json

Our project is created. Congratulations!

Next, we should initialize the npm repository inside the duomly-serverless-course directory.

To do that, we need to open the terminal inside the projects directory, and type:

npm init -y

Install dependencies (bcyptjs, bcryptjs-then, jsonwebtoken, moongoose, serverless-offline)

When our project is initialized, we should create the few necessary dependencies that we'll use in the project.

The first one is bcryptjs:

npm -i ——save bcryptjs

The next one is promise-handler for bcrypt:

npm -i ——save bcryptjs-then

Next, we should install JSON web token dependency:

npm -i ——jsonwebtoken

To handle the MongoDB database we'll mongoose:

npm -i ——save mongoose

And the last one that we need is serverless-offline. It's crucial because we'll start our serverless backend on localhost, which will improve development speed a lot:

npm -i ——save serverless-offline

How to create a MongoDB database in MongoDB atlas

In this step, we should visit mongodb.com and use the service MongoDB Atlas.

You can use the free tier for a start.
Next, you should visit your cluster, next go into collections, and click the button "create database".

Add name "duomly-serverless-course".

How to create a MongoDB table

Visit your database in Mongo DB Atlas, and create two tables (if you didn't create them during the DB creation).

The first table should be "users", and the second one should be "orders".

How to init git repository

Now we can come back into the project again.

In the current step, we should initialize our git repository.

To do that, open terminal in the projects directory, and type:

git init

How to add secrets.json to .gitignore

Next, we can go into visual studio code and visit the .gitignore file.

Inside the gitignore file, we should add a file named "secrets.json".

It's a very important step because to care about security(at least minimum), we never should put passwords, keys, or connection strings into the repository.

Just don't do it, and be happy.

# package directories
node_modules
jspm_packages

# Serverless directories
.serverless

secrets.json

How to store environment variables in serverless

To handle passwords, keys, and connection strings, we need to create a separate file, and name it "secrets.json".

Next, we should pass there two environmental variables. The first one will be "DB" with MongoDB Atlas connection string as a value.

The next one will be "JWT_SECRET", and you need to create some password as the secret key for JWT tokens.

{
  "DB": "your connection string",
  "JWT_SECRET": "your secret"
}

How to setup serverless.yml

Okay, we are really close to the end of the tutorial. Congratulations!

Now is one of the most important steps when we talk about Serverless Framework, because we will be editing the Serverless YAML template.

In the serverless.yml file, we have all the necessary information that tells AWS what to do, how to create infrastructure, how many functions, what should be secured, and what shouldn't.

It's like the brain of our serverless application.

We added only a main configuration in the first lesson, with the provider, node version, and region.
Let's take a look at the example below:

service: duomly-serverless-course

plugins:
  -serverless-offline

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: us-east-1
  environment:
    JWT_SECRET: ${file(./secrets.json):JWT_SECRET}
    DB: ${file(./secrets.json):DB}

How to run serverless local

Woohoo, our app setup is ready!

Now we should test if that works.

We can do that in two ways. One of them is deployment to the real, living environment in the cloud. 
That we can do by open the terminal and type:

SLS deploy

And the second, much faster, and very good for the local testing method, is just offline run.
We can do that by open the terminal and type:

sls offline start —skipCacheInvalidation

Conclusion of serverless how to get started tutorial

Congratulations, you have the first and own Serverless project!

After this setup, you're ready to continue with the next episodes and add more features to focus on the next lessons.

Here is today's episode's code:

https://github.com/Duomly/aws-serverlesss-nodejs/tree/serverless-course-lesson-1

In the next lesson, we will have a bit more work and much more coding because we will build the whole authentication.

You'll learn how to build serverless authentication with login, register, and jwt-token, but not only!

We will do the first MongoDB calls, create some users, and learn how to build DB models.

Duomly promo code

Thanks for reading,
Radek from Duomly

💖 💪 🙅 🚩
duomly
Duomly

Posted on September 2, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related