How We Saved Our Clients Billing by upto 50% Using AWS Lambda

Gaurav Pareek
6 min readJun 9, 2020

--

In these times of unprecedented lockdowns and pandemic. Companies are making their prime goals to reduce their cloud infrastructure cost and with no deterioration in their infrastructure performance.

Keeping this in mind recently I had worked on an assignment where we had to have reduction in their existing AWS bills. They were paying about 20,000 USD per month in AWS bills and we were given a task to reduce the billing upto 50%.

So, we rolled up our sleeves and started auditing their current architecture and in this audit we found out that they had many API systems which could be moved to serverless architecture like AWS Lambda.

I thought to share a with you about the serverless architecture and best practices with AWS Lambda. Just for spoilers, we were able to bring down their AWS bill to 9,800 USD a month. So, let’s get back to point.

What is Serverless and Why should we use it?

As the name suggests, Serverless computing is a way of providing the backend services on as use basis. A serverless architecture allows user to write and deploy code without having to worry about the backend infrastructure. In serverless, environment you can just write the code and AWS will handle all the backend infrastructure like servers, bandwidth, execution, scalability and availability.

AWS Well-Architected Framework

In terms of AWS, we use AWS Well-Architected Framework as the bible when architecting and deploying a cloud architecture. AWS proposes five components or “pillars” to every architecture. These pillars include Operational Excellence, Security, Reliability, Performance Efficiency, and Cost Optimization.

We used these pillars for our architecture as well.

1. Operational Excellence

Operational excellence means the ability to deploy, monitor, deliver and make the existing procedure better.

With AWS Lambda, you can remove many challenges like Logging, Monitoring, Debugging, Deploying and Performance Testing which you may face in a non-serverless architecture.

Logging

AWS Cloudwatch is an excellent service for storing logs. While creating a lambda function you can deliver the logs to Cloudwatch logs. You can create a LogLevel variable in your Lambda Environment variables which will store log statements generated during function runtime and then logs can be processed in many ways.

Monitoring

AWS Lambda provides Cloudwatch metrics which can provide you insights like execution duration and number of times your function was called (invocation). Along with this, you can create alarms for your lamda function based on these Cloudwatch metrics.

Deployment

AWS Code pipeline (CI/CD) has made our lives easy in terms of code deployment. There are two ways either you can directly upload your code to your lambda function or you can create a code pipeline which can deploy your code.

I would suggest you to create code pipeline to deploy your code to Lambda function using various deployment methods offered by AWS Code pipeline.

Performance & Load Testing

Performance & Load Testing your lambda function helps you to determine the right amount of memory allocation and timeout value. Debugging issues like these is crucial in terms of maintaining high availability application.

2. Security

During the process of studying for my AWS architect exam I was always told that security comes first and is above everything. In terms of serverless architecture, three things should be kept in mind application code, least privileges, API Authorization.

One IAM Role Per Function

The role should be mapped over single functions. This helps in ensuring the least privilege security policies over specific functions. One must ensure to have a 1:1 relationship with IAM Role.

API Authorization

We use AWS API Gateway along with AWS Lambda to deploy API’s. AWS API Gateway allows you to secure API Gateway points using AWS IAM, Custom Authorizers.

VPC Security

If your Lambda function needs access to resources inside a VPC you must always consider applying the least privilege security groups, NACL’s and route tables. So, that the traffic from your Lambda function reaches only to it’s required destinations.

3. Reliability

As an AWS Architect you should always have a high level understanding of how your application code is working. You should always have an understanding of how you have architected your application and where are the weak areas and strong areas to achieve high availability and fault tolerance.

High Availability

By default, Lambda functions are implemented within all the regions. Although it is crucial to design subnets over multiple availability zones when running lambda function over a custom network environment. This process ensures to keep the application alive during availability zone outage.

Fault Tolerance

Working over multi-dimensional regional architecture, it is vital to diverge over another AWS region. This includes Lambda functions, dependencies, and Event Sources that need to be considered.

4. Performance Efficiency

Performance of a Lambda function depends on three factors memory size, correct coding language and monitoring application performance with AWS X-Ray.

Allocating Right Memory Size

AWS Lambda allows you to size your RAM up and down. The amount of RAM assigned affects the amount of CPU time and network bandwidth your function receives. If you choose smallest amount of RAM that runs your function adequately fast is an anti-pattern because that would add latency to your application. So, allocation of right memory size is important because that would affect latency of your application.

Choosing Right Language Runtime

AWS Lambda supports C#, Java, Go, Python, Ruby, Node.js and PowerShell.

We went ahead with python because it’s an interpreted language and have much faster initial start-up times. But you can go ahead with languages like Java, C# which have the most significant initial startup time.

We were considering Python or Node.js because our use case was very latency-sensitive and spiky traffic. Since these two languages have much faster initial start-up times. So, it made sense for us to choose Python or Node.js

Monitoring Performance

We wanted to trace the application life cycle throughout the entire execution. So, we linked AWS X-Ray with AWS Lambda which gave us deeper look at the components of the application, showing the latency and other metrics.

5. Cost Optimization

Achieving the right amount of allocated RAM with least latency is the key in cost optimization of Lambda.
Lambda has two dimensions with cost, execution time and RAM allocated.

RAM Allocation Optimization

In AWS Lambda, allocating less RAM is an anti-pattern. So, make sure you allocate the right amount of RAM to your Lambda function.

Decoupling Code

AWS Lambda works best with decoupled code. So, we tried breaking the existing code to granular level and it was a cost effective solution as well for us.

Selecting Right Event Sources

AWS Lambda should be linked with cost effective and required events. So, selecting the right event sources is very important.

Final Word

The Serverless Application has been the latest development in a modern approach. It enables the user to forget about the worries associated with patching operating systems and servers. One can easily write desiring code adhering to business needs and implement them.

The overall Serverless Architecture allows building faster. The complete process is done keeping in mind the overall agility and minimizing the cost of ownership.

Why switch to Serverless or AWS Lambda?
1. Develop modernized and innovative applications
2. Better overall process agility
3. Minuscule cost of ownership
4. Better productivity and focus amongst developers.
5. Mitigating run time operation issues.
6. Better scaling and reliable growth opportunities.
7. Better operations over in-cloud or on-premises servers

I’m an AWS expert and freelancer. I take up AWS assignments. You can reach me on gaurav.ideabox@outlook.com or just drop a comment with your contact details. I will get back to you.

--

--

Gaurav Pareek

DevOps and AWS Certified Architect | Technology Enthusiast with deep interest in Cloud Solutions. Linkedin: https://www.linkedin.com/in/gauravpareekaws/