Serverless computing is exactly what the name suggests. No servers. Simple! We don't have to worry about managing servers for running small scripts like crawlers, redirects and simple computations that don't involve storage. But if there are API's involved, storage also can be managed. Endless possibilities. Which is why I decided to have a go at it and figure out what's so special about this serverless architecture you speak of!
Endless I say! I will list out what I have covered either directly or indirectly.
Say you have a website with viewers across the globe but you want the content to be served equally from CDNs closest to you. AWS Cloudfront + Lambda is your answer.
Say you have a headless Drupal website and you want to serve your front-end differently for people in different locations or any other variables you can think of. AWS Lambda to the rescue in a fast, simple and cost-efficient way!
A domain name you own
Read my previous articles on headless Drupal
How to code in either Python, nodejs, Go, C#, Java
What I have done.
To demonstrate how AWS lambda can be used, I have created a simple example for redirecting traffic to my headless sites.
So what I have done using AWS lambda is to redirect traffic to one of my headless websites depending on where the request is coming from. I created a sub-domain “fe.abhaisasidharan.xyz” which is a trigger for my lambda script. The lambda script which is in python, redirects to one of my headless sites depending on the location of the request.
The lambda script: https://github.com/codingsasi/lambda-demo/blob/master/lambda_function.py
The flow diagram of what happens when someone visits fe.abhaisasidharan.xyz:
Breaking it down.
The Trigger (fe.abhaisasidharan.xyz)
This can be accomplished in many ways. You can even trigger your lambda script from the command line using Amazon Resource Name for your lambda script. Alternatively, you can add an API gateway to trigger the Lambda script. The API gateway has its own custom url and can be triggered when accessing that. If you want to use your custom url instead of the Amazon API url, like I have, you can do so by using Amazon Certificate Manager (ACM) and adding your custom url in ACM. I did that first, but then I screwed up my ACM configuration and abandoned that idea. Now I have just added url forwarding in my DNS config i.e., forwarded fe.abhaisasidharan.xyz to API gateway url.
The Lambda Script
The lambda is just a python script the get’s the IP address of the person trying to access fe.abhaisasidharan.xyz. It then finds the location of that IP address and redirects the user depending on the location. This is what's show in the flow chart. Here is the code for the lamda script: https://github.com/codingsasi/lambda-demo/blob/master/lambda_function.py
I hope this article gives a clear picture of AWS Lambdas. Feel free to comment if you have any questions.