Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Serverless architecture with Lambdas on AWS

1. Introduction

Are you tired of microservices? Take the next step with a Serverless architecture.
Serverless is a way to build applications in which we focus on the code,
abstracting even more of the systems, machines, availability, scaling, etc.

For this we will use “lambdas”.
We will explain what this is and you will see how the microservice “micro” will now make sense.
We will implement a simple API example and put it into production.
We will use a framework called “Serverless” that will facilitate the deployment,
and we will have at one time our API deployed on the
Amazon cloud platform .

2. Environment

The tutorial is written using the following environment:

  • Hardware: MacBook Pro 15 ‘Notebook (2.2 GHz Intel Core i7, 16GB DDR3).
  • Operating System: Mac OS High Sierra
  • Development environment: Visual Studio Code
  • Software: serverless framwork, aws client, nodejs

3. Lambdas

The “lambdas” or “functions” are small functional units of code that will be executed
under the events that are configured: calls to an endpoint, events in a queue, every so often
, when a file is created / deleted from a directory …

Each cloud provider offers its solution. All are similar, they usually charge for execution time
and RAM consumed. Not for clock cycles, but for the time between the start and end of execution of
the function even if it is idle.
The most famous providers are:

4. Serverless framework

The Serverless framework is a tool to facilitate the deployment
of our code and the configuration of events, databases and more pieces of architecture.

It supports several cloud providers and makes
our code work really easy and the configuration is
very organized and centralized in the code of our program.

5. Example

To demonstrate the operation we will implement a CRUD with a simple REST API that allows creating, updating,
reading and deleting books from a database.

Of all the programming languages ​​that we could choose to implement, we have chosen JavaScript
for its simplicity and to try that anyone can follow the tutorial. But it could be implemented for
AWS Lambdas in the following languages: Node.js (JavaScript), Python, Java (compatible with Java 8), C # (.NET Core) and Go.

We will create a service with the following functions:

  • createBook – to create a record
  • updateBook – to update a specific record
  • getBooks – to get all records
  • getBook – to get a specific record
  • deleteBooks – to delete a specific record

Serverless configuration

Within our project we created a “books” directory that is an independent NodeJS project
that will be our “books” service with the 5 functions indicated above.

With a “serverless.yml” file we define all the configuration of the architecture and deployment of our service.

At the beginning of the file we mark the name of the service which will concatenate the name of all the lambdas together with the environment name / stage.

In the “provider” section we mark where it will be displayed, for what environment, in what region and programming language. We can also configure environment variables such as the name of the database that can depend on more global variables such as the name of the service and the environment.

           name: aws
           stage: dev
           region: eu-west-3
           runtime: nodejs4.3
           environment:
                DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage}

In the section “functions” we define our “lambdas” indicating which handler contains the code and under which events it is launched. We have configured http events with the verbs and routes typical of a REST API.

And finally we define the database. In this case, a DynamoDB that is the AWS NoSQL database

NodeJS code

First we configure in the file ‘dynamodb.js’ . This will include the configuration
for the connection to the database depending on whether we run locally or in the cloud.

In the file ‘handler.js’ we create the different functions.
For example, the creation function of the “book” entity would be:

The function receives three parameters: event, context and callback.

The parameter ‘event’ contains all the information of the event that launched the request. For example, if it is an event of an endpoint, it will take the parameters and the headers of the request.

The ‘context’ parameter contains information about the current execution environment of the function.

And finally the parameter ‘callback’ is the function that we must call to return the result of the function. Its first parameter is the error (if there is one) and the second one is a string with the result (ignored if the error was not null). For a JSON response, it must be parsed with “JSON.stringify (…)”.

6. Deployment

We will proceed to deploy our functions locally and in AWS. For this we must first have “serverless” installed:

npm i serverless -g

We also install the dependencies declared in the “books / package.json” file with:

npm install

Deployment in local

To be able to deploy locally, we must first install dynamoDB locally to connect to the database. To do this, we execute the following command in the “books” directory:

serverless dynamodb install

And now we proceed to start everything in local with the following command:

In the file “curls.sh” you have examples of requests to our REST API in which “curl” requests are sent to the endpoints of the 5 functions.

Deployment in AWS

We will proceed to deploy our functions in AWS. We have to have the AWS client installed and configured. If you do not have an account you can create it and use the
free AWS layer that offers more than enough for all the tests you want.

Serverless based on the configuration we have defined in the file “books / serverless.yml” will create all the necessary parts within the ecosystem of AWS with just execute:

serverless deploy

This will show us a console output where we will see which components / parts are being deployed in AWS and will show us the endpoint that you have created for each of our functions so that we can consult it. We can see all this information reflected in the AWS console .

In this case, something like this has simply been displayed:

The first piece is an “API Gateway” . This is
responsible for managing the API endpoints, connects them with the functions and offers monitoring, scaling, authorization and versioning.

The following parts are the functions that will connect us to a log collection system with “Amazon CloudWatch Logs” that we can consult with:

serverless logs -f NOMBRE_FUNCION

The last piece is the NoSQL database called DynamoDB that we can manage online from the AWS console itself.

You can modify the “curls.sh” file so that it points to your API in AWS and test it. But it should be operational and fully prepared to scale and hold a good library!

With the following command we can delete all the elements that serverless has registered in AWS automatically:

7. Conclusions

As you can see with AWS Lambdas and the Serverless framework, it is very easy and quick to create a fully operational and highly scalable API in a matter of moments.
It is ideal for creating small applications, pilots, or even with a little more architecture complex applications highly efficient.

In future tutorials we will see how the Serverless framework also facilitates the use
of other pieces of AWS such as SNS to interconnect functions
with low coupling through events. This will allow us to design event-oriented architectures such as CQRS. We can also configure custom authorization systems to be executed automatically before invoking our functions, for example to validate and decode the information of a JWT.

The post Serverless architecture with Lambdas on AWS appeared first on Target Veb.



This post first appeared on Targetveb, please read the originial post: here

Share the post

Serverless architecture with Lambdas on AWS

×

Subscribe to Targetveb

Get updates delivered right to your inbox!

Thank you for your subscription

×