Learn the 3 AWS Lambda states today (the single purpose function, the fat lambda and the lambda-lith). 
Tags: aws, cdk, beginners, tutorial
Published: 2020-04-02 12:27:47

# The Lambda Trilogy

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k24a31hjg180dognvdkh.png)

You can clone this pattern from [cdkpatterns.com](https://github.com/cdk-patterns/serverless/tree/master/the-lambda-trilogy) today through one command:

```npx cdkp init the-lambda-trilogy```

## Deconstructing The Lambda Trilogy

{% youtube tHD3i06Z6gU %} 

## What is it?

The three states of AWS Lambda are something that has been discussed by many serverless heroes since their invention. This is probably the most controversial subject in all of serverless so I am not going to tell you which of the three is the best because like everything you need to adapt the right implementation to fit your context!

Some examples from [Paul Swail](https://twitter.com/paulswail), [Yan Cui](https://twitter.com/theburningmonk), [Jeremy Daly](https://twitter.com/jeremy_daly) and others:
- [Jeremy Daly & Paul Swail Serverless Chats #41](https://www.serverlesschats.com/41)
- [Jeremy Daly Off By None](https://www.jeremydaly.com/newsletter-issue-63/)
- [Yan Cui Presentation (slide 41 on)](https://www.slideshare.net/theburningmonk/beware-the-potholes-on-the-road-to-serverless-224107000)
- [Yan Cui re:Invent 2019](https://d1.awsstatic.com/events/reinvent/2019/REPEAT_1_How_to_refactor_a_monolith_to_serverless_in_8_steps_API310-R1.pdf)
- [Yan Cui Hackernoon - monolithic or single purpose functions?](https://hackernoon.com/aws-lambda-should-you-have-few-monolithic-functions-or-many-single-purposed-functions-8c3872d4338f)
- [Yan Cui monoliths vs cold starts](https://theburningmonk.com/2018/02/aws-lambda-monolithic-functions-wont-help-you-with-cold-starts/)
- [J D Hollis medium.com](https://medium.com/statics-and-dynamics/should-i-use-a-single-monolithic-lambda-function-or-multiple-lambda-functions-with-api-gateway-d99b0230f1e7)
- [Ryanne Dolan medium.com](https://medium.com/@ryannedolan/aws-lambda-and-the-monolith-a0eb2d1516ef)

The three states are:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fmhq2mneur7f5sfhs75s.png)

### Description
This is the purest of all the serverless patterns. Each lambda does one unique function and the code is in its own file.

### Pros
- Maximum code reusability
- Forces you to write more testable code
- Introduces lowest cognitive burden for developers making changes to that individual function
- Easier to optimize your lambda execution times and by extension costs

### Cons
- Only works for fully event driven architectures
- Seeing the bigger picture, congnitive burden increases as system wide changes are talked about
- Maintenance as it grows (how do you make sure 7000 lambdas have no code vulnerabilities?)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rrtrg9yxm9y8k6ddvqmd.png)

### Description
This is a compromise option where we can still have individual lambdas but we group the actual code together in one (or more) files. You would decide what goes into a file based on low coupling, high cohesion arguments like in traditional development.

### Pros
- Related logic is grouped together making your code easier to see the bigger picture
- Code can easily be shared between lambda functions without needing things like layers
- Security footprint reduced as updating one file can update many lambda functions

### Cons
- How big is too big? Every extra byte of code added slows your lambda cold start times.
- Increased blast radius of changes. Now one line of code being changed could bring down a section of your infrastructure instead of one lambda.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/16i6owsm2yjys5pt7kmg.png)

### Description
This is using the lambda runtime container like a docker container. You use a web framework like Flask or Express and put them inside the lambda, then have your api gateway pass all requests through to the lambda and have that framework process the request.

### Pros
- You can have an identical local development experience to deployed since you are using no AWS specific features
- The code could be moved to Fargate later if it got too big for lambda with minimal changes (or another cloud)
- Developers already know these frameworks

### Cons
- Is this really what Lambda excels at? The larger project sizes will increase cold start times and there will be restrictions on incoming/outgoing payload sizes
- Higher exposure to cold starts as the lambda will spend longer processing events
- Lower levels of code reuse as probably still building the traditional ball of mud
- Adapters required to make existing frameworks work with lambda. These are in various states of maturity and are another potential vulnerability in your app.

## CDKPatterns Implementation

You can clone this pattern from [cdkpatterns.com](https://github.com/cdk-patterns/serverless/tree/master/the-lambda-trilogy) today through one command:

```npx cdkp init the-lambda-trilogy```

Both TypeScript and Python versions of all 3 lambda states are available. 

The lambda-lith in Python uses Flask and in TypeScript uses Express.js