Mistakes to Avoid When Implementing Serverless Architecture with AWS Lambda

https://aws.amazon.com/blogs/architecture/mistakes-to-avoid-when-implementing-serverless-architecture-with-lambda/

There are many articles that show you how to use AWS Lambda , but these articles usually don’t cover what you shouldn’t do with the service. Because technologies emerge and become outdated quickly, solutions that may look like the right solution, otherwise known as anti-patterns, can prevent you from building a cost-optimized, performant, and resilient IT system.

In this post, I highlight eight common anti-patterns I’ve seen and provide some recommendations to avoid them to ensure your system is performing at its best.

1. Using monolithic Lambda functions with a large code base

During a lengthy project, your Lambda function code might grow bigger and bigger. This leads to longer Lambda invocation times and makes maintaining the function code more complex.

Here are my recommendations to avoid this:

  • Break up large Lambda functions into smaller ones with less business logic inside.
  • Use AWS Step Functions to orchestrate multiple Lambda functions. This lets you control your Lambda implementation flow and benefit from features like error condition handlingloops, and input and output processing.
  • Use Amazon EventBridge to exchange information between your serverless components. EventBridge is an event bus with reach functionality on filteringlogging, and forwarding event messages, which allows you to turn any event into a native AWS event. EventBridge supports more than 200 AWS native and partner event sources.

Figure 1. Monolithic versus microservice approach

Advertisements

2. Using synchronous architectures

I often see architectures where a Lambda function is responsible for various tasks that run one after another. For example, Lambda is configured to receive user-uploaded files through the Amazon API Gateway, transform the file, save the operation to an Amazon Simple Storage Service (Amazon S3) bucket, and return the HTTP reply back to the user.

Such use cases lead to longer run times and higher Lambda implementation costs.

My suggested improvement for this case:

Figure 2. Reengineering example of monolithic API calls

3. Not managing the shared code base

Advertisements

I see many projects where people repeat the common Lambda function code while creating their applications. This leads to implementation inconsistency and technical debt, because sooner or later developers will modify the common code base in different parts of the project.

To avoid this anti-pattern, introduce the following:

  • Lambda layers – The same code base or libraries can be mounted to your Lambda functions.
  • Shared API – Replacing the common code base with a shared microservice API will help simplify the code base management and abstract its implementation for dependent services.

Figure 3. Lambda layers for the shared Lambda functions codebase

4. Choosing the wrong Lambda function memory configuration

Right-sizing the Lambda function is similar to Amazon Elastic Compute Cloud (Amazon EC2) sizing. I see a lot of cases where Lambda functions are provisioned with extensive memory configuration, which can lead to added costs.

I recommend using the following goals to develop a “just right” sized Lambda function:

  • Test Lambda function to ensure that it is configured with the right memory and timeout values.
  • Confirm that your code is optimized to run as efficiently as possible.

Determining the Lambda function’s right size may be challenging. It highly depends on the runtime environment and your code. To simplify this challenge, Alex Casalboni introduced a framework for Lambda Power Tuning. He created a state machine powered by Step Functions that helps you optimize your Lambda functions for cost and performance in a data-driven way.

SaleBestseller No. 1
HP 2022 Newest All-in-One Desktop, 21.5" FHD Display, Intel Celeron J4025 Processor, 16GB RAM, 512GB PCIe SSD, Webcam, HDMI, RJ-45, Wired Keyboard&Mouse, WiFi, Windows 11 Home, White
  • 【High Speed RAM And Enormous Space】16GB DDR4...
  • 【Processor】Intel Celeron J4025 processor (2...
  • 【Display】21.5" diagonal FHD VA ZBD anti-glare...
  • 【Tech Specs】2 x SuperSpeed USB Type-A 5Gbps...
  • 【Authorized KKE Mousepad】Include KKE Mousepad
SaleBestseller No. 2
ACEMAGIC Laptop Computer, 16GB DDR4 512GB SSD, 15.6 Inch Windows 11 Laptop with Intel Quad-Core N95(Up to 3.4GHz), Metal Shell, BT5.0, 5G WiFi, USB3.2, Type_C, Webcam, 38Wh Battery, 180° Open Angle
  • 【EFFICIENT PERFORMANCE】ACEMAGIC Laptop...
  • 【16GB RAM & 512GB ROM】Featuring 16GB of DDR4...
  • 【15.6" IMMERSIVE VISUALS】This 15.6 inch laptop...
  • 【NO LATENCY CONNECTION】The laptop computer...
  • 【ACEMAGIC CARE FOR YOU】 This slim laptop will...

As soon as the output of the framework is JSON based, you may consider incorporating it into your continuous integration and continuous delivery (CI/CD) pipelines to automatically adjust Lambda configuration accordingly.

Figure 4. Automated Lambda function optimization for cost and performance

5. Making Lambda functions dependent on less scalable service

Lambda is a scalable solution. It can quickly overwhelm a less scalable service in your architecture. If Lambda can directly access a single EC2 instance or Amazon Relational Database Service (Amazon RDS) database, it can be a potentially dangerous integration.

Here’s how you can prevent potential problems:

  • Minimize and decouple your dependencies on non-serverless services.
  • Use asynchronous processing wherever is possible. This will let you decouple microservices from each other and introduce buffering solutions.
  • Implement a buffering Amazon Simple Queue Service (Amazon SQS) message queue in front of non-scalable or non-serverless services.
  • Implement Amazon RDS proxy in front of Amazon RDS to maintain predictable database performance by controlling the total number of database connections.

Original Post>

Figure 5. Improving RDS DB efficiency and application scalability with RDS Proxy

6. Using a single AWS account to deploy your workloads

Advertisements

There are some hard limits for a single AWS account that can be reached fairly quickly. This includes the amount of elastic network interfaces or AWS CloudFormation stacks you are allowed to have.

There’s a simple solution for avoiding this:

  • Split your workloads and put them into different accounts. AWS Organizations and AWS Control Tower can manage several AWS accounts, even for non-corporate AWS customers.

7. Using Lambda where it is not the best fit

Lambda is ideal for short-lived workloads that can scale horizontally. The common Lambda application types and use cases provide information on where Lambda is an ideal choice. At the same time, using Lambda for uploaded video file conversion and transcoding is not a good idea because those operations might take a significant amount of time and exceed the Lambda timeout.

New
HP Envy Desktop, Intel Core i7-13700, 64GB RAM, 4TB SSD, SD Card Reader, HDMI, VGA, RJ45, Wired Keyboard & Mouse, Wi-Fi 6, Windows 11 Home, Black
  • [High Speed RAM And Enormous Space] 64GB...
  • [Processor] Intel Core i7-13700 (16 Cores, 24...
  • [Tech Specs] 1 x USB 3.2 Type-C, 4 x USB 3.2...
  • [Operating System] Windows 11 Home - Beautiful,...
New
XZKKCD Archangel 3.0 Gaming Computer PC Desktop - Ryzen 5 3600 6-Core 3.6GHz, RTX 3060 12GB, 1TB SSD, 16GB DDR4 3200, RGB Fans, AC WiFi, 600W Gold PSU, Windows 11 Home 64-bit, White
  • AMD Ryzen 5 3600 6-Core 3.6 GHz (4.2 GHz Turbo)...
  • GeForce RTX 3060 12GB GDDR6 Graphics Card (Brand...
  • 802.11AC | No Bloatware | Graphic output options...
  • Heatsink & 3 x RGB Fans | Powered by 80 Plus Gold...
  • 1 Year Warranty on Parts and Labor | Lifetime Free...
New
jumper Laptop, Laptop Computer with 24GB LPDDR4 512GB SSD, Intel Celeron N5095 CPU(Up to 2.9GHz), 17.3" FHD IPS 1920x1200 Display, 38WH Battery, Intel UHD Graphics, USB3.0 * 3, BT5.0, Front 2.0MP.
  • 【Excellent performance】 Laptop is equipped...
  • 【Do Your Tasks Easily】 Laptop computer comes...
  • 【Amazing Visuals】 The 17.3-inch laptop...
  • 【Poweful Cooling System】Laptops are equipped...
  • 【External Ports Design】Notebook computer comes...

A bit of advice:

8. No monitoring for implementation costs

There are many blogs and websites that cover controlling costs. However, many individuals and companies have not implemented measures to protect themselves.

Last advice for today:

  • Set up a daily spending limit in your account settings and an alarm to notify you when your limit is reached. This will allow you not only to save some money, but likely help resolve possible technical issues quicker.

Summary

This article showed you the eight most common serverless anti-patterns to avoid. The solutions I mentioned are applicable to the most common serverless application workloads.

Related information