Is a Class model possible with AWS Lambda Functions ?
By: Naveed Ahmad
Tags: Aws Api Gateway, Software Architecture, AWS, Dynamodb, Aws Lambda Functions

An AWS Technology Stack for a Web Application

There is a tendency among software development community that if they are using the cloud infrastructure, then there is probably no need for writing their own business, class or domain model. Writing a few Lambda functions which would be accessible through a REST or HTTP API Gateway would be sufficient. These REST or HTTP API Gateways could directly inserts and fetch data from dynamo DB.

What were the benefits of writing a business or domain model?

The business or domain model enabled us to hide the complexity of the business processes. It also enabled us to write software using the terminology of the business domain. It was much easier to add new functionality or features. Code reuse was made easy, as the business or domain model could be used in other software. Whenever there was a change in the business processes, the domain model was changed. This change was then reflected in the software.

What are the cons of writing only lambda functions which directly update the database?

As the number of lambda functions grow, there would be a need to provide a structure to these lambda functions. If the structure is not provided in terms of a class model, the number of new functions which could be written could be severely limited. There would be additional human resources required to handle the increasing number of lambda functions.

What are the benefits of combining the usage of lambda functions with a class model?

Using a class model could significantly reduce the number of Lambda function calls. If some information is available in the class model, then it wouldn’t be required to make any additional call to the Lambda function. The information could directly be returned from the business model. The number of new tables which would have to be added could be significantly reduced. The domain model provides a sort of documentation for the database schema. The rational for many important business decisions is also part of the business model.

What are the different approaches for combining a lambda functions with a class model?

Approach # 1: Keep the business logic or domain model between the API gateway and Lambda Functions

One way to introduce the business logic to the Lambda functions, is to bring a new layer between the API Gateway and the Lambda Functions. This layer would contain the business classes. These classes would be accessible from the AWS API Gateway. The APIs defined in the API gateway would call using the business logic classes to complete the API functionality. The business logic classes would make calls to the Lambda functions to perform DB operations.

Approach # 2 : Keep the business logic or domain model between the Lambda Functions and the database

A second way could be to keep the business logic or domain model between the Lambda functions and the database. This could help solve the scalability issue. If the domain model classes are accessible within the Lambda functions, as the Lambda function scale up, so would the class model. It would also keep the business logic hidden from the API layer. Each lambda function could be considered as a Façade function for the particular domain.

Which of these approach for combining a lambda function with a class model is the best approach?

If the organization has scalability in mind, Approach # 2, would be better since it provides a way to scale the class model.

If the organization wants a clear mapping between the API and the business model, then Approach # 2 is a much better option.

Conclusion :

Introducing a business model would bring the structure to the AWS Lambda functions. But how would this business model scale? How would the business model state synchronized between the different instances of the AWS Lambda functions. This is the question which we would try to answer in the next article.