intern story

AWS Lambda — Writing Slick, Seamless, and Scalable Software

Sony research india

Vishakhavel Shanmuganathan, Cloud Solutions Intern at Sony Research India, gives you an insight into the computing prowess of the serverless compute service offered by Amazon Web Services.
Blog9_Image1
Vishakhavel Shanmuganathan, Cloud Solutions Intern
Lambda is a serverless compute service offered by Amazon Web Services. With Lambda, you can forget about provisioning and configuring servers and just focus on code. The super-quick deployments ensure extreme speed and make sure that your application does not experience any downtime. Lambda was launched only in 2014 and has gained immense popularity amongst larger companies that require heavy computing power.

Let’s take a look at a few instances of companies that rely on Lambda to deliver high-quality products to their customers.
Fender Musical Instruments, one of the largest names in the stringed instruments manufacturing business, rely on Lambda for delivering over 700 TB of high-quality music video lessons, to their customers. Netflix and Amazon prime, arguably the two largest video streaming applications, rely on Lambda to stream high-quality movies and documentaries to millions of customers in over 60 countries. MLBAM (Major League Baseball Advanced Media) uses Lambda to enhance their real-time gameplay data to their broadcasters. Financial Engines have moved their core engineering platform to AWS Lambda and other serverless services to reduce downtime, save costs, and scale seamlessly. iRobot, an IoT company that manufactures the popular self-cleaning robot vacuum, also uses Lambda and AWS IoT, to handle communication between these devices. PhotoVogue, The Seattle Times, T-Mobile… the list is endless!

So why are all these big companies using serverless?

Is it worth investing the time and the engineering manpower required to migrate to a serverless core architecture? The answer is a definite YES!. The advent of serverless computing has made it extremely simple and cost-efficient to implement a microservice architecture. All you need to do is create another API Gateway Route that points to a different AWS Lambda function, and we have a serverless-microservice architecture, without having to worry about networking, SSL termination, load balancing, and scaling. All of this, coupled with the cost-effective ‘pay as you use’ model, eliminates the need to pay hourly costs for severs making it a no-brainer to migrate.
Lambda is event-driven and gets triggered by several events, like AWS SNS, S3, API Gateway, etc. This makes it possible for a variety of situations to exploit the computing capabilities of Lambda. Here’s naming a few of them:
  • Real-time Big data processing
  • Serverless websites
  • Microservices
  • Batch processing
  • Deploying Machine Learning algorithms
  • Processing files uploaded to S3
  • Websockets
Now let’s look at how we can use this service to mock a backend web server that functions as a user-management application. We will also be using AWS RDS (MySQL) as the database and API Gateway to expose HTTP APIs.

STEP 1. Create the AWS RDS MySQL instance

Blog9_Image4

Note: Choosing DynamoDB would have made this a purely serverless application, but since the main focus of this article is the serverless computing service — AWS Lambda, let’s keep things simple by sticking to a regular RDS MySQL instance.

Make sure to:
  1. Whitelist your IP address from the security group of the RDS instance, in case you want to query the database from a local client (like MySQL Workbench) and test it out.
  2. Enable “Password Authentication” for the authentication mechanism.
  3. Initialize a DB from the “Additional configuration” option, while creating the instance.

STEP 2: SET UP API GATEWAY

Five APIs have been set up for this demo: 2 GET, 1 POST, 1 PATCH, and 1 DELETE. These act as a route to the backend AWS Lambda function, where we will handle the routing logic.

STEP 3: SETUP LAMBDA

Let’s look at a simple CRUD application that uses AWS Lambda, AWS RDS, and AWS API Gateway. This is a basic user-management application that can create and store new users, login with, modify and delete the existing ones.
Note: One can use the pymysql python library to connect to the RDS instance. This is a library that Lambda won’t recognize by simply using the import statement because it is not available in its environment. To bypass this issue, one has to manually download the dependencies using “pip install” commands and zip them along with the code, and upload them to Lambda.

Routing mechanism:

In this demo, we route requests to the necessary blocks of code using if statements on the ‘rawPath’ data parameter of the ‘event’ function parameter, which is a part of the lambda_handler signature. This is not an ideal approach to routing, as we are not taking into account the type of request (GET, POST, PATCH, DELETE, etc) and routing based on the path alone. However, since we don’t have different HTTP requests with the same path and different request types, this will suffice for this demo.
One must always connect to the database from outside the “lambda_handler” context to avoid establishing a database connection every time the function is triggered.

Sample Code:

1. The code to get all users from the database.
2. The code handling the Login mechanism and its related exceptions

Exception Handling:

AWS Cloudwatch logging has been enabled for this Lambda function, which is the easiest way to troubleshoot. Exceptions have been handled in the code, which means the error will get logged in Cloudwatch and the developer can view them, but the client will always get a 200 OK HTTP response from the function with an appropriate error message.
Having taken you through the process of writing simple code that is used for routing as well as handling web traffic without having to configure any servers or network components, we have only scratched the surface of the endless capabilities of serverless computing. If this doesn’t convince you to give the world of serverless computing a try, I don’t know what will!
Skip to content