Skip to main content
AWS lambda

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!

 

The possibilities. 

Endless I say! I will list out what I have covered either directly or indirectly. 

  1. Let’s say you have changed your domain name for some reason and you don’t want to lose any visitors. You can simply write a python or javascript program to redirect all incoming requests to your new domain. But don’t we need to host this on an apache or nginx server? Wrong! Why would you do that when you can run it serverless and pay for only the number of times your program is actually run! The first 1 million requests are free!

  2. 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.

  3. 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!

 

The Requirements

  1. AWS account

  2. A domain name you own

  3. Read my previous articles on headless Drupal

  4. How to code in either Python, nodejs, Go, C#, Java

  5. Logic

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. 

As you have probably read in my previous articles, my blog abhaisasidharan.xyz is also available as an REST API at abhaisasidharan.xyz/api. This API is used in my headless websites which are: ng.abhaisasidharan.xyz, react.abhaisasidharan.xyz, vue.abhaisasidharan.xyz. To learn more about headless (decoupled) Drupal, you can go through my previous articles (https://www.abhaisasidharan.xyz/blog/headless-decoupled-drupal-using-drupal-8-and-angular-6https://www.abhaisasidharan.xyz/blog/angular-vs-react-js-vs-vue-js-comparison-front-end-javascript-frameworks-headless-decoupledhttps://www.abhaisasidharan.xyz/blog/react-vs-angular-better-head-headless-drupal). 

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:

AWS Demo Lambda

 

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.

x

Work

Therefore logo
80 Atlantic Ave, Toronto, ON Canada
Email: hello@therefore.ca
Call us: +1 4166405376
Linkedin

Let us know how we can help!