Serverless — Hello World

Dorian Machado

Sometimes read a lot of theory doesn’t work for some and sometimes perform a simple hello world example helps a lot.

Photo by Simon Abrams on Unsplash

In this post you will create a simple Hello world app (with nodeJS) using the Serverless Framework which will help us to deploy the app in AWS.

Installing the Serverless Framework

First thing first, let’s install the Serverless Framework in your local machine with the following command or check the official docs here.

sudo npm install -g serverless

Check the installation verifying the version of the framework

serverless --version
1.49.0 (Enterprise Plugin: 1.3.4, Platform SDK: 2.1.0)

NOTE: “sls” is the shortcut for the “serverless” binary so the following command would work too.

sls --version
1.49.0 (Enterprise Plugin: 1.3.4, Platform SDK: 2.1.0)

AWS credentials

Only what remains is the credentials configuration, you can use the following command or check the official documentation here.

serverless config credentials --provider aws --key <ACCESS KEY ID> --secret <SECRET KEY>

Creating your first Serverless Application

Initializing the Project

Using the binary “serverless” you’ll be able to use the functionality “create” in order to initialize the project.

serverless create --template aws-nodejs --path myFirstServerlessApp
cd myFirstServerlessApp

The option “- -template” indicates the runtime of our functions, for this example “nodeJS” will be used. More info about the framework supported templates check the official documentation here.

The option “- -path” indicates the folder that will accommodate our project, in this case the project name will be “myFirstServerlessApp”.

After execute the creation command, the output would looks like this one:

Project file structure

Once the project is created the framework will create an easy to follow file structure for you.

myFirstServerlessApp
├── handler.js
└── serverless.yml

Where “serverless.yml” is the main configuration file of the framework (it will becomes in our best friend), and “handlers.js” is a sample javaScript file ready to deploy (thanks Serverless Framework).

serverless.yml

service: myfirstserverlessappprovider:
name: aws
runtime: nodejs10.x
functions:
hello:
handler: handler.hello

handler.js

'use strict';module.exports.hello = async event => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
},
null,
2
),
};
};

Deploy the Application

The time has come and the app is ready to launch with this command.

sls deploy

The out should looks like this one:

Execute your just deployed Serverless function

From your local machine you can invoke the just uploaded function with the following command:

sls invoke -f hello

and the output of your super serverless function will looks like this:

So nice right, you executed a javaScript function without worry about installing node in the remote servers, AWS take care about it in our behalf.

Let’s add an event to your Serverless function

Everything worked perfect at the moment so let’s take your function to another level adding an “event” to this little monster.

To do that the configuration for the function in the “serverless.yml” file will change a little bit, something like this:

service: myfirstserverlessappprovider:
name: aws
runtime: nodejs10.x
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get

Where “-http” indicates that will be a HTTP event that will work in the path “hello” and with the method “get”

After that you need to re-deploy your project

sls deploy

As you can see the output result looks a little bit different, now the “endpoint” section displays an URL related to your function.

If you wanna see something really cool open your terminal an execute the following CURL command:

curl https://hg9zbybocf.execute-api.us-east-1.amazonaws.com/dev/hello

Look what you have done, It’s alive !!!! (the output was shorten)

It’s time to show off your new app with your friends, go ahead and share the link of your endpoint.

Customising your function

Time to perform changes in the sample code in order to customize your app with this code in the “handler.js” file:

'use strict';module.exports.hello = async event => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'My first serverless application !!!
},
null,
2
),
};
};

The change was made in the “message” section with a custom value for the message.

With the serverless binary you can deploy just one function passing the function’s name as parameter but first you need to get the name with this list command:

sls deploy list functions

And now using the function’s name to deploy only this function

sls deploy -f hello

Verifying the new deploy executing the CURl command again

curl https://hg9zbybocf.execute-api.us-east-1.amazonaws.com/dev/hello

Deleting your Serverless Application

Let’s say that for some reason you need to delete your beautiful application, in this case the following command will remove everything for you.

sls remove

After the remove command the output should looks like this:

Conclusions

Serverless Framework is a swiss army knife to manage a Serverless project also brings flexibility and a lot of options to customize your projects.

In the following publications we will dig dive in the awsome world of Serverless and analyze practical examples and scenarios.

Dorian Machado

Written by

Serveless Evangelist

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