AWS Tip

Best AWS, DevOps, Serverless, and more from top Medium writers .

Follow publication

Going Serverless — Part 1

--

The goal of this article series is to get started with Serverless. During the series, you will learn how to build an application locally and deploy it to AWS with CI.

Going Serverless
image: https://www.serverless.com/blog/serverless-framework-v2

For this example, I’m using NodeJS to create an HTTP API. I assume that you already have NodeJS installed. If not, you can download the LTS version of Node for the downloads page.

Note: I’m using NodeJS 14.17.6 and serverless V2 for this project.

Step 1: Install Serverless globally

npm install -g serverless

Once installed, you are ready to get started.

Step 2: Create the serverless project

serverless

Once you run the above command, it will ask you some questions to get started. For this example, I’m using below:

  1. What do you want to make?
    AWS — Node.js — HTTP API
  2. What do you want to call this project?
    sample-aws-serverless-project (This is the name of the project. You can use any name you like.)
  3. Do you want to login/register to Serverless Dashboard?
    No
  4. Do you want to deploy your project?
    No (We will set this up with AWS)

Great. You have now set up the serverless framework. You will see 4 files inside your new project, and let’s focus on two files.

  • serverless.yml — This file contains all functions and infrastructure resources of the project.
service: sample-aws-serverless-project
frameworkVersion: '2'

provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: '20201221'

functions:
hello:
handler: handler.hello
events:
- httpApi:
path: /
method: get

All the endpoints are listed under functions:. When the project is set up, it has one function included as an example.

  • hello: — this is the example function, which will be the first endpoint in our app. You can change this to whatever you like.
  • handler: — This indicates the location of the function to be executed when a request is made to the endpoint. The file path is related to the root directory.
  • events: — This indicates how this function can be triggered. In this case, the endpoint named “hello” can be called by a GET request to /

Note: In the provider section, the runtime is indicated as nodejs12.x. You can change it to nodejs14.x which is the LTS at the time of writing.

  • handler.js — This is the file that includes function that executes when you make a GETrequest to /

Step 3: Add offline support for development

After setting up serverless, we need to get it up and running locally for development. To do that, we will create a package.json file and install serverless-offline.

npm init -ynpm i -D serverless-offline

Now, we need to add the following lines to your serverless.yml file.

plugins:
- serverless-offline

Now, your serverless.yml should look like this:

service: sample-aws-serverless-project
frameworkVersion: '2'

provider:
name: aws
runtime: nodejs14.x
lambdaHashingVersion: '20201221'

plugins:
- serverless-offline

functions:
hello:
handler: handler.hello
events:
- httpApi:
path: /
method: get

Step 4: See it in action

Now the project is ready for local development. Run the following command to start serverless locally.

serverless offline

Open your browser and go to http://localhost:3000 and you will see the output from your first serverless function.

Step 5: Adding one more endpoint

First, we will add some new lines to the serverless.yml file. The new code starts with mySecondFunction:.

service: sample-aws-serverless-project
frameworkVersion: '2'

provider:
name: aws
runtime: nodejs14.x
lambdaHashingVersion: '20201221'

plugins:
- serverless-offline

functions:
hello:
handler: handler.hello
events:
- httpApi:
path: /
method: get
mySecondFunction: # function/endpoint name
handler: mySecondFunction.hello # where the function is located
events:
- httpApi:
path: /my-second-function # what is the endpoint
method: get

Now create a file named mySecondFunction.js and add the following lines: (Basically, the content is the same as hello.js. I just changed the message and removed the input: event line.)

"use strict";

module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: "My second function",
},
null,
2
),
};
};

Step 6: Test it.

Stop the server and restart it by running serverless offline

Open your browser and go to http://localhost:3000/my-second-function and you will see the output from your second serverless function.

Note that whenever you make a change to the serverless.yml file, you need to restart the server.

What’s next?

Now that you have successfully set up the project locally, you can continue to develop it. In the next article, I will guide you to deploy this application to AWS using CodeBuild.

Things to do before the next article:

  1. Refactor the code. The refactored code example is available in GitHub.
  2. Upload the code to a remote repository.

Next: Going Serverless — Part 2: Deploy the serverless application to AWS

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in AWS Tip

Best AWS, DevOps, Serverless, and more from top Medium writers .

Written by Mahesh Samudra

I'm a full-stack JavaScript developer based in Sri Lanka 🇱🇰. Love to work with ReactJS and Serverless

No responses yet

Write a response