Nano Services based ServerLess Architecture using AWS

Serverless vs Server is the new hot topic among developers. Both have pros and cons, decision has to be made considering the spicifics of applications.

Here I am sharing by expereince with creating a Serverless application in AWS, which uses what I call as Nano Services.

Amazon Web Service and Lambda

If you are a developer, there’s no way you haven’t heard it and for most it’s their daily routine. Lambda is a on demand basis instance which can perform actions just like any other server without actually maintaining a Server.

Lambda’s can be triggered based on events which can involved other AWS services. Any change in state of any AWS services can be directed to trigger the lambda.For example if a new row is inserted in DynamoDB, you can have a specific lambda triggered or when a new object is added to S3 or a call to SNS or change in AWS IOT and so on.

Lambda as Nano Services

While Micro services are all around developers are in search for a smaller and lighter services. Lambda fits the spot perfectly, so it can be refered to as Nano Service. While Micro Service handles a complete logic of a service. Nano Service generally is just a class of logic in it. Since Lambda’s have a cold start time, you don’t want to over burden it.It only deals with a small logic.Apart from Buissness logic Nano services needs to have code that connects to DB or Queue or something else, this is apart from the core logic.

An application can have hundreds to thousands of these Nano Services that work in co-ordination. One disadvantage is that there can be code duplication, for example the lot of these services need to connect to DB, so that code will be in all the services, these leads to code duplication and when you want to change something there, you will end up changing things inn every services.

Another problems is since you will be having lot Nano services communicating with each other like Nerual network, if it becomes too many then debugging and origanizing them might be tricky but for small and medium size applications, it will be easy and it will reduce you of the burden of maintaining a server.

Invoking a Lambda Synchronous vs Asynchronous

When you want to call a Lambda from another Lambda there is two ways you can do it Synchronous and Asynchronous.

You need to call it synchronous when the lambda you invoke returns you a response and you need to consume that response. AWS has in-build libraries for this inn lot of languages, like boto3 in python. Bear in mind that in this approach you will have to pay for both of the lambda’s time so use it only when it’s needed.You can pass a payload in the form of JSON to the invoking lambda.

On the other hand if you don’t need the return value of the invoking lambda, then you can call the other lambda asynchronously. You can do it using SNS.Create a new topic in SNS and add it as trigger to the invoking lambda. From the parent lambda just ‘notify’ that SNS topic, it will invoke the child Lambda. Note that I used ‘Notify’ not call. There’s a difference between two. Your parent Lambda won’t wait under child lambda is completed in case of notify.

Billing of Lambda

Cost of aws services depends upon which region you are hosting it. Most region offers a good amount of free tier.

The cost of Lambda is calculated based on two critirias,

  1. Number of triggers
  2. Total Duration of all triggers.

Of these two 90% of the cost will be from second critirea.

Debugging Lambda

By default all lambdas will be linked to cloudwatch. It is logging mechanism, whcih stores logs of all the lambda triggers.This is where you see the output of the print statements. Bear in mind that cloudwatch will be charged. Sometimes more than the actual cost of Lambda itself.

One more loggin tool is XRay which is similar, but also has the ability to run on client.

Envirnoment Variables In Lambda

You can give any number of Envirnoment Variables in Lambda, It can be accessed just as you access in any other Server.

However, you Envirnoment variables are visible in the dashboard prompting security concerns. This can be overcomed encrypting the Envirnoment variables using AWS KMS.

KMS is a Key management Service in AWS which helps you in Encrypting and Decrypting strings.You can use your regular Access and Secret keys of AWS to call KMS.

Calling Lambda’s from other systems:

If you want to call lambda from other systems you need to have required authenications to do that. You need access and secret token to do that, the same keys can be used for all other AWS services.

Lambda can be called from shell commands,Android, ios, from any other external server.

Adding Trigger to Lambda

As I mentioned Lambda is a event based on-demand server instance. There needs to be an event to invoke a lambda. That event can come from any aws services. You can link most of the aws services to as trigger for lambda.For example, You can have Lambda invoked when a new row is inserted in DynamoDB, or an Iot shadow is updated.when Lambda is invoked in this way, the context in which it is invoked is also passed on to that.

Concurrency

This is where the real power of lambda can be felt. You can have virtually any number of instance running concurrently.However by default you can run only 1000 concurrent instance, after which lambda will be throttled. But you can increase the limit based on your demand.

This is a big advantage because while designing the architecture of application you don’t need to worry about number of concurrent users as in the case of designing server architecture.This is what true scalling is all about.

Lambda as running a Mainframe job

This is one area where lambda fails straight out against a Server application.It’s difficult to run a scheduled tasks in Lambda.Although, you can schedule a lambda to be triggered as particular timing using the cloudwatch, Each instance of lambda has a limited power.

Each instance of lambda can only have a RAM of a little less than 3GB , with a time limit of 300sec. This specifications will not be enough to run high intense scheduled MainFrame jobs.

There are few ways in which this can be overcome . Like using multiple lambdas instead, just like Mulithreading, split the task into mulitple lambdas. This of course has its own set of complexity.Cost benefit analysis has to be done in those cases.

Conclusion

Serverless Architecture is gaining traction among small and mid level IT firms, and rightfully so, because, it requires less time and cost to built, host it. It saves good amount of money in the long run for hosting services and the biggest advantage is scalable at run time which can’t be dreamt of in a Server application. Serverless can’t completely replace Server applications as of now but it sure is taking away it’s share of pi.