What is AWS Lambda – simply explained

AWS Lambda is one of many Amazon services. Its an example of so called “serverless architecture”. The defining characteristic of this approach is that it abstracts the runtime environment from the code.

In simple words – you don’t need to think about the servers any more. You just put your code in a Lambda and it get’s executed when it needs to.

Besides minimising the overhead spent on server management, a nice side effect is that you pay only for the execution time, which usually is much less comparing to a server that runs 24/7.

This video explains lambda pretty well:

When to use a Lambda function?

There are several use cases for lambda:

  • batch processing – when you have a big amount of data that needs to be processed in regular intervals (for example once a day)
  • your system experiences times with high load followed by low usage, for example ticket buying system when a lot of people want to make their transaction at the same time
You might ask – do I have to move all my app to lambda when I already pay for a server?

Absolutely not. You can put into lambda as much or as little code as you wish. You can use it only for some background task, which will call other service once per day and fetch some data.

Also you can also put massive data update script there and write it in a way that execution will be split into multiple lambdas, and it will be run simultaneously.

Disclaimer

We live in a World, where cloud computing is eating more and more of the web. In this place I have to admit, that the most popular cloud services like Amazon Web Services, Microsoft Azure or Google Cloud Platform can provide you on a server in second, minute or hour basis.

This means that even if you need some big machine running for you, which you normally have to pay 1000$ per month, it can be available for you for 6 hours a month, and your cost will be recalculated respectively.

So if your code is heavy or need some specific environment setup, remember that you have many options out there.

What can trigger a Lambda?

As described in video above, lambda can be triggered by many different actions, which in some cases is super useful. A couple of lambda triggers available to configure directly from AWS console are:

  • Upload of a file to your storage folder
  • Change of some data in database
  • Click of a button in your service, which will send a request to start lambda function
  • Time schedule – you can configure time of a day or a period between triggers – as you wish
  • Alexa voice command
  • Another lambda… – this will be explained deeper.

As you see it is pretty flexible and can be adapt to many situations.

Lambda in business – case studies

  1. Lambda is a perfect solution for a REST API integration, or actually any API. Example: you manage all your clients through Salesforce. But you need to check in a government databases ( dependent on country ) if the VAT number provided by your client is valid. So instead of getting to government website and do it manually every time you want to invoice them, we can build an integration, which will based on Lambda and do it for you. And it can be triggered in a many ways:
    – on the new customer creation for this particular one
    – every morning for all of your customers, to check if their company didn’t disappear during the night
    – on a click of the button, available in Salesforce, so you can check it every time before invoice preparation
  2. Another usage of Lambda is data migration or update. No matter if it comes from another API or something is calculated inside your application, when you need to do some data update periodically, scheduled by time or activated by some other action – Lambda will be a very good choice. Also if there is some ‘big’ amount of data to process, we can always pack it into batches, and run on multiple lambdas.
  3. Attaching some mini application, which has no connection with the main module. For example we can have an app that is a platform for transport companies. After each successful journey you need to know the number of kilometers which a truck have done, and more importantly you need to know how many have been done in which country, because of different taxes and charges.
    We can build an application, that will take point A, Point B and estimate kilometers done in each particular country. Result of such function will be estimated tax to pay.
  4. Lambdas can be easily chained – this means that some operation can trigger another operation. So for example your code is checking some data in a third party service. It can return correct or incorrect data. Based on that you can prepare 2 other functions, which will be triggered based on result of previous step.

Any disadvantages?

There is a couple:

  • Running small pieces of code on Lambda is cool, but you have to decide how small and granular it have to be. When there will be too many functions running, it might be a problem to control their correctness
  • Logs are in separate files, and to check them you have to use AWS CloudWatch. This also might be an advantage – nothing mix, everything is in proper place. But when your Lambdas are connected, you have to dig through multiple pages and look for an error.
  • You are giving the execution decisions to AWS, which is a third party, so you need to have this in mind
Not a disadvantage

Time and resources limitations. A lot of articles mention, that Lambda have time limitation to 2 minutes, and memory limited to 2 GB. Which is not true anymore. You can always check current limitations here: https://docs.aws.amazon.com/lambda/latest/dg/limits.html  For a long time it was main issue of Lambda, that writing your code you need to have in mind, that it must finish sooner than in 2 minutes or it will be terminated, but it is not a problem anymore.

Hope you get some knowledge from this article. All examples in case studies were already implemented by us in TH-EY, if you have any questions or need some help with your problem, feel free to use the form below to contact us.

Thank you for reading this.
Filip