Going Serverless: 3-Tier Architectures Made Easy With AWS Lambda | by Ravindu Rashmin | Nov, 2020


Image by Stephan Seeber on Unsplash

A multi-tier architecture is a design pattern that is embraced by millions of developers around the world mainly because of its way of separating concerns clearly across many layers. The most widely adapted multi-tier application type is the 3-Tier architecture.

A 3 tiered architecture consists of mainly 3 layers. The Presentation Layer, Application Layer, and the Data Layer. Each layer has its own set of responsibilities and uses communication methods to interact with the other layers.

The Presentation Layer is basically the layer with which the end-user interacts. It can range from a mobile application to a single page web application.

The application layer consists of the business logic of the project. It handles the requests sent by the application layer and converts those requests to a specific domain-related issue and interacts with the Data Layer to give a suitable response to the client request.

The final layer is the Data Layer which has the responsibility of persisting data and providing the Application Layer with necessary data. This layer consists of some kind of database management system to store the domain-related data.

The most common and popular way of developing 3-tier applications is using REST APIs. Usually, a front-end application is built with some kind of front-end framework, and to interact with that, a REST API is built along with an attached database. Easy! Right?

But, not so quickly! We have to deploy our application. For that, we need to choose a vendor and set up a server to deploy our application. After some time, the no of users increase and we need to scale up our application, then we got to use some kind of load balancing technology with multiple instances of our server deployed. What happens if one of the servers went down? We have to ensure that there is no single point of failure and need to design the application architecture accordingly.

As you can see, this ‘legacy’ approach consists of too much hassle to be dealt with just to implement our domain logic in the cloud. We will spend more time designing the infrastructure and servers than developing our own business logic which can be a huge loss to a company in the long-term.

This is where the term serverless comes to the rescue. A serverless application is an application in which the developer doesn’t have to manage any servers! Literally! It is a cloud computing approach in which a third party vendor manages the servers and manages the scaling of the servers dynamically without us having to do anything. There are no operating systems to manage, no effort needs to be given to scale our application and most importantly there is no fixed cost. You pay as you go.

AWS Lambda is the serverless solution provided by Amazon and is the most widely used serverless technology in the world. Here, a developer can provide a piece of code from a language he desires and create a ‘Lambda Function’. That lambda function can be invoked using a set of triggers we define ourselves. So an external entity can send ‘requests’ to that lambda function and it will run our code which is stored inside the lambda function. If many ‘requests’ come to that certain function AWS will automatically fire up more instances of that same function so that that it can handle the incoming requests parallelly.

Here I will a simple 3-tier application using AWS Lambda and AWS Gateway services which are provided by Amazon. Below is the high-level architecture of our ‘lambda-demo’ application.

Architecture – Screenshot by Author

Here POSTMAN will act as our presentation layer and generate a HTTP request which will be received by the API Gateway and according to our request URL, it will forward the request to the relevant Lambda Function. The lambda function will process the data and provide the gateway with the necessary response.

As the first part, we need to create a piece of code to be put into our lambda function. I will use Java to create this project but you can use any language which is supported by lambda.

First, create a maven project using the quickstart archetype provided and add the following dependencies to your POM file.

Screenshot by Author

Now, in your Main class, implement the RequestHandler interface provided by the AWS-lambda-core and override its handle request method to return a simple POJO as below.

Your code is done! Now run mvn install and package your application code into a JAR.

After that go to your AWS console and search lambda and go to the create function option. Here you will be asked a function name and an execution environment. Give the desired name and Java 8 as the execution environment. Then you will be taken to the lambda management console.

After Creating Your Lambda Function – Screenshot by Author

By going to the actions menu option in the Function Code menu, you can select upload jar and upload the created jar file. Give your package name, class name, and the function name in the Runtime settings menu, in my case its


And Voila , your lambda function is done. Now you have to attach a AWS Gateway to your lambda function as the trigger. Chose the API Gateway as the trigger and chose to create a new Gateway using the following settings.

Screenshot by Author

And you are done.

The final output will look like this.

Screenshot by Author

To test the application copy the API endpoint and paste it in POSTMAN and send a GET request to that URL.

Screenshot by Author

And just like that, you can create a scalable, reliable, serverless 3-tiered application in just minutes. Note that this is a very basic example and you can add security, caching, database support, logging, and many more to this application and create an enterprise-level application using the state of the art technology which is provided by AWS.

The full Java code is available on my Github.

If you made it this far, thank you very much for reading this article. Any feedback is highly appreciated.


Source link

Write a comment