Serverless with AWS

Nipun Nadeeshana Liyanage
5 min readSep 23, 2022

--

First of all, before going into the content, let us clarify what serverless means. Serverless is a cloud development model that allows developers to run applications without worrying about managing and maintaining servers. The servers are obviously still present to host the applications, but they are kept feeling abstracted away from the developers.

To get started with serverless, you should first install serverless framework in your local machine. For that, open the command prompt and run

npm install -g serverless

To check if you have already installed serverless framework in your machine or to check the version, you can run the following command.

sls — version

To get a new project created from a predefined template on typescript using AWS serverless, you can run

serverless create -t aws-nodejs-typescript -p {yourprojectname}

In order to run these applications, you need to have AWS Command Line Interface (AWS-CLI) installed in your computer. You can go to the following link and select the version for your operating system and follow the clearly given instructions to install it.

Installing or updating the latest version of the AWS CLI — AWS Command Line Interface (amazon.com)

In order to create an AWS profile, you should run the following command after obtaining the key and the secret key from the AWS IAM console for the particular user.

serverless config credentials \
— provider aws \
— key {the key} \
— secret {secret key}\
— profile {your profile name}

Now if you open the project in visual studio code, it will look like this.

Fresh Project Template

Now you can open a terminal in the project and install typeorm and postgress dependencies from the following command.

npm i typeorm pg

Next, you should create an Amazon Aurora RDS instance. This can be either MySQL based or PostgreSQL based.

In this example, I have used Amazon Aurora PostgreSQL compatible edition under amazon aurora.

In the project we created earlier, I am creating a student entity, taking the first name, last name, students department name, degree name and hometown. An id for each entry will be generated as an uuid.

Then the schema is defined for the requests. This is used to validate the data received in requests. In this example. we are taking first name as a string, and it is a required field in each request.

schema.ts

create.ts file is created to create a student for every validated request using the schema we defined earlier. Middify library is used here for validation and remove duplication among many other functionalities the middify library can be used for. In this, a success response is sent back, if the operation is succesful.

create.ts

In the student-service.ts, we record the validated data that is received from requests to the database.

index.ts

The following index.ts file is used to export the student function.

In the following code snippet, we establish the database connection with the database we created in the AWS servers. In this before initiating a new connection each time, it is checked if a connection is already available and if not, a new connection is created.

The credentials for the host, port and db username and others mentioned in the above code snippet can be obtained from the aws console and are specific for your instance.

simple error handling

Simple messages for success and failure are defined with above code examples.

In the serverless file, the details regarding the service, the AWS profile you want to use, AWS serverless RDS instance (DB host names and ports) and everything regarding the deployment should be mentioned.

Then in the project terminal if we run either of the commands

serverless offline or sls offline

This will enable the AWS environment to be simulated in your local machine, so that you will not have to deploy this every time a change happens.

The output will come like this. Success!

This marks the end of today’s article. Hope you learned something new.

Thank you and see you with a new one!

--

--