An introduction to serverless on AWS with NodeJs and Typescript

Felipe Gaya Scandiffio
6 min readApr 30, 2023

--

This a little project to get your feet wet on Serverless and make you aware of the possibilities

If you are reading this article I suppose you already have NodeJs knowledge, this post git: https://github.com/felipegs31/message-slack

First of all, what we will do:

We want to hit a function on the cloud that just returns a hello world (then later you can do whatever you want with it), we want this process to be triggered every Monday at 5 am and by an HTTP request (as a manual trigger)

Why serverless:

Scalability: Serverless architectures can easily scale up or down to meet demand, allowing you to handle high traffic without having to worry about server capacity or performance.

Cost-effectiveness: With serverless, you only pay for what you use, so you can avoid the costs of maintaining and running a server infrastructure.

Reduced management overhead: Serverless providers handle the management of servers and infrastructure, so you don’t have to worry about patching, upgrading, or maintaining the underlying infrastructure.

Faster time-to-market: Serverless architectures allow you to focus on your code and functionality, without having to spend time setting up and managing servers.

Flexibility: Serverless architectures allow you to use a variety of programming languages and services, so you can choose the best tools for your project.

Overall, serverless can be a great choice for projects that require flexibility, scalability, and cost-effectiveness, and that do not require low-level server management.

Let's get to the code!

We are going to use Serverless Framework to help us to create this project https://www.serverless.com in their website you can check the advantages of using this framework, I can write a following post about it

Having NodeJs installed, you should run in a terminal

$ npm install -g serverless

and then go to the folder you want to create your project in, and on the terminal type:

$ sls create --template aws-nodejs-typescript --path <PROJECT-NAME>

open this folder on vscode and you will see

1- serverless.ts: the heart of the application, its where you declare all services, functions, and environment variables you will use (more on that later)
2- libs folder: libs we use on the project
3- functions folder: every function that we run, so here is where we write our logic

In this boilerplate, we have already a lambda that gets the body of an HTTP request and returns a `Hello ${event.body.name}, welcome to the exciting Serverless world!`

let's deploy this boilerplate as it is so it gets more graphic, but first we need AWS credentials, otherwise how serverless framework would know where to deploy and under which account?

Go to your AWS account and go to the IAM service

then go to users

and then click on the button Add Users

click on next after putting the user name

Now this user will have admin privileges, so it can do anything on AWS, altho this works for a tutorial, its important to scope those privileges correctly when creating AWS users in a real scenario

Hit next and then create the user.

Back on the users table filter for you newly created user and click on it

Go to security credentials

Scroll down to Access keys and create one

Check this option and hit Next => Create

And now you have your credentials!

Store those in a secure place because you cannot retrieve them, just create new ones

Now we need to let our computer aware of those keys, to do that we need to download aws-sdk, and of course, there is a package on npm for it

npm install aws-sdk

And then type

aws configure --profile <any name you like>

and on the prompt put the credentials we created on AWS!

To use this newly created profile to deploy our project lets add this line

And now let's deploy using:

sls deploy

And now its live!!

The terminal is telling us which endpoint we can send a POST request to hit our function, there is an endpoint because we used API GATEWAY, which is another AWS service, amazing right? You have a function running on the cloud without configuring any servers and its highly available and supports a tremendous load

Lets hit this endpoint using postman

Now we want that this function to trigger automatically every Monday at 5 am, for this, we would use another AWS service called event bridge, the beauty of using serverless framework is that we can do it all via code

Lets make our first change

Following serverless framework docs: https://www.serverless.com/framework/docs/providers/aws/events/event-bridge

And now a simple change on our handler logic so the message makes sense

And as always, after every change, we need to run

sls deploy

This is like pushing our code to github, we have to make every time we want the code to reach the cloud

To check our code on AWS we can go to Lambda dashboard

Find our newly deployed function

And now we see we have 2 triggers for this lambda!!!

Congrats guys, this is huge! Now you know how to run any code on the cloud using a cron job or via manual trigger!

You can access the details about event bridge going here:

And you can even check the next time this trigger will be fired

How to delete eveything:

Just so you dont get billed, you can delete all your project using

sls remove

before doing that you can go to cloud formation and check the previous deployment

and now lets destroy it!

There you go! Now you know how to deploy a simple serverless function, 2 ways of triggering it and how to destroy it. Super cool! On my next post Ill teach you know to create a CI pipeline so you dont have to run `sls deploy` on every change

--

--