CDK Patterns at 20! Let's Walk Through all 20 Serverless Patterns for AWS
Tags: aws, cdk, serverless, tutorial
Published: 2020-07-14 12:27:59

There are now 20 fully deployable serverless architecture patterns at [cdkpatterns.com](https://www.cdkpatterns.com), all built with AWS CDK TypeScript/Python and all including the vanilla CloudFormation Template.

This seemed like a good time to stop and reflect on what has been built showcasing the use cases for each pattern in one article. I will simply walk through them alphabetically but if you want to view them by AWS Component used you can use [the component filter](https://cdkpatterns.com/patterns/) or if you want to view them by the AWS Well Architected pillar they relate to you can use [the well architected pattern matcher](https://cdkpatterns.com/patterns/well-architected/)

For the patterns that exist in Jeremy Dalys new [Serverless Reference Architecture](https://www.jeremydaly.com/serverless-reference-architectures/) collection I will also include a link to there for users of other frameworks than AWS CDK (SAM, Serverless, Pulumi etc).


## Polly

![polly arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/polly/img/overview.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/polly)

This is a pattern that integrates the Amazon Polly service into an AWS Lambda Function so that you can synthesise text into speech using a serverless stack. It also integrates with Amazon Translate to allow you to choose the language for the spoken text.

### When To Use
If you are publishing content you could use this to support international readers/listeners in their native language

### Alternatives
I am sure external vendors exist, this is the only serverless AWS native solution I know.

<br /><br /><br />
<hr />

## S3 Deploy

![s3 deploy arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/s3-angular-website/img/architecture.PNG)

- [Angular Pattern Link](https://github.com/cdk-patterns/serverless/blob/master/s3-angular-website)
- [React Pattern Link](https://github.com/cdk-patterns/serverless/tree/master/s3-react-website)

This is a simple pattern for taking a website built in Angular or React and deploying it to an S3 bucket for a super cheap and scalable hosting solution. The construct bundled with the pattern is flexible and allows you to expand on the bundled pattern by adding CloudFront, a custom domain name and SSL.

### Deconstructing S3 Deploy
{% youtube tUUNiF0q7rk %}

### When To Use
You need to deploy a website on AWS through CDK

### Alternatives
AWS Amplify

<br /><br /><br />
<hr />


## The Big Fan

![the big fan architecture](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-big-fan/img/the-big-fan-arch-small.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/tree/master/the-big-fan)

This is a pattern from Heitor Lessa that allows you to fan out your messages to multiple different consumers. The beauty is that you are filtering the messages based on properties in the message so consumers only receive messages relevant to them saving compute time / cost.

Heitor Lessa at re:Invent
{% youtube 9IYpGTS7Jy0 %}

[re:Invent slides](https://d1.awsstatic.com/events/reinvent/2019/REPEAT_3_Serverless_architectural_patterns_and_best_practices_ARC307-R3.pdf)

### When To Use
If you are building an asynchronous event driven flow that needs to fan out to multiple consumers based on different message types

### Alternatives
Amazon EventBridge - look at The EventBridge ATM

<br /><br /><br />
<hr />

## The CloudWatch Dashboard

![cloudwatch dashboard arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-cloudwatch-dashboard/img/arch_small.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/tree/master/the-cloudwatch-dashboard)

When you start building serverless applications the big piece that feels missing compared to a traditional application is observability. You have all these small pieces running asynchronously and when something goes wrong it can feel like logs are your only friend. This pattern shows you have to generate the metrics needed to build a custom dashboard and add Alerts publishing to SNS for a serverless stack containing API Gateway HTTP API, Lambda and DynamoDB

### When To Use
Anyone building production serverless applications should be using CloudWatch dashboards and alerts to understand their application state

### Alternatives
You can use other third party tools but this is the only AWS option

<br /><br /><br />
<hr />

## The Destined Lambda

![destined lambda arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-destined-lambda/img/arch.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/tree/master/the-destined-lambda)

This pattern combines [Lambda Destinations](https://aws.amazon.com/blogs/compute/introducing-aws-lambda-destinations/) with [Amazon EventBridge](https://aws.amazon.com/eventbridge/) to show you that with EventBridge rules you can decouple your components in an event driven architecture and by combining it with lambda destinations you can strip out EventBridge specific code from your lambda functions themselves and decouple further.

Deconstructing The Destined Lambda
{% youtube DQgq_p6Q03M %}

### When To Use
Lambda destinations are a brilliant way of removing boilerplate from your orchestration logic but they only work when executed asynchronously so if your flow is synchronous then this is not for you yet.

They also forward the full context of an event which means that when you send an error to a Dead Letter Queue through destinations you get everything including the event that caused the error so you have everything you need to implement automatic retry or error hospital functionality.

### Alternatives
If you don't want to use destinations, you can still use Amazon EventBridge just with more logic in your Lambda Function or you could use [AWS Step Functions](https://aws.amazon.com/step-functions/).

<br /><br /><br />
<hr />

## The Dynamo Streamer

![dynamo streamer arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-dynamo-streamer/img/arch-small.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/tree/master/the-dynamo-streamer)

This is a variation on The Simple Webservice pattern that has been created by [Eric Johnson](https://twitter.com/edjgeek) only instead of a lambda being connected to the apigateway, the dynamodb is connected directly and the api gateway uses templates to transform the incoming message to insert the data. A lambda then listens for events coming from dynamodb streams and can be used to do data transforms after insertion meaning if an error occurs you lose no data.

"When thinking about #Serverless architectures, consider how much of your processing can happen AFTER the data is saved. Thinking asynchronously can lead to greater resiliency and often, less code."

### When To Use
I would use a direct API Gateway integration with DynamoDB as much as you can. Unfortunately you cannot use VTL yet with the new HTTP API so you need to use the more expensive REST API but removing a Lambda Function from the flow reduces latency and execution costs. If cold starts are an issue for your use case this eliminates them from the flow.

### Alternatives
The simple webservice pattern

<br /><br /><br />
<hr />

## The EFS Lambda

![efs lambda arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-efs-lambda/img/arch2.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/tree/master/the-efs-lambda)

This is a pattern from [Danilo Poccia](https://aws.amazon.com/developer/community/evangelists/danilo-poccia/) that attaches an EFS file system to your lambda function to give it expandable, persistent storage. Having this level of storage in a Lambda Function opens the door to many new possibilities (multiple functions can even use the same file system).

### When To Use
If you need to share state between multiple Lambda Functions or if you need the Lambda to be able to access something bigger than the max in memory allows

### Alternatives
Convert from using a Lambda Function to using an [AWS Fargate](https://aws.amazon.com/fargate/) container of you could go all the way to [EC2](https://aws.amazon.com/ec2/)

<br /><br /><br />
<hr />

## The EventBridge ATM

![eventbridge atm arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-eventbridge-atm/img/arch-small.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/tree/master/the-eventbridge-atm)

This is an example CDK stack to deploy the code from this [blogpost](https://aws.amazon.com/blogs/compute/integrating-amazon-eventbridge-into-your-serverless-applications/) by [James Beswick](https://twitter.com/jbesw)

In this example, a banking application for automated teller machine (ATM) produces events about transactions. It sends the events to EventBridge, which then uses rules defined by the application to route accordingly. There are three downstream services consuming a subset of these events.

### When To Use
If you need to build an asynchronous flow where the contents of the message dictate which consumer(s) should receive the message 

### Alternatives
You could use The Big Fan pattern with SNS

<br /><br /><br />
<hr />

## The EventBridge Circuit Breaker

![the eventbridge circuit breaker arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-eventbridge-circuit-breaker/img/arch2-small.PNG)

[cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/the-eventbridge-circuit-breaker)

This is an example CDK stack to deploy my own interpretation of The Circuit Breaker stack described by [Jeremy Daly](https://twitter.com/jeremy_daly) in this [article](https://www.jeremydaly.com/serverless-microservice-patterns-for-aws/#circuitbreaker) and by [Martin Fowler](https://twitter.com/martinfowler) all the way back in 2014 [here](https://martinfowler.com/bliki/CircuitBreaker.html)

In this example, we have a lambda behind an API gateway that is supposed to integrate with an external webservice (www.google.com). The problem is that Google is down and it takes 10 seconds for your lambda to return that error. You pay for every ms of execution with Lambda so this is bad if lots of consumers hit your service.

Don't worry, we have integrated a circuit breaker into this system. When a call to google fails an error event is pushed to EventBridge where it is routed to a lambda that inserts a record into DynamoDB with a 60 second lifespan.

When a consumer calls our lambda we check if there have been 3 failure events in the last 60 seconds and if so we fail immediately, this saves over 9 seconds of execution costs. As the error events expire after 60 seconds our failure events should gradually drop below 3 where we call the service again and check status.

### When To Use
If you integrate with external webservices you should probably integrate a circuit breaker so that your system is more resilient in the event of that service going down

### Alternatives
AWS ElastiCache could be used to cache successful calls/responses but this won't help if you haven't called before and the service is down.

<br /><br /><br />
<hr />

## The EventBridge ETL

![EventBridge ETL Arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-eventbridge-etl/img/arch-small.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/the-eventbridge-etl)

This is an example stack showing how you can use EventBridge to orchestrate events through an ETL process. This pattern was insired by [Vyas Sarangapani](https://twitter.com/madladvyas) and [Hervé Nivon](https://twitter.com/hervenivon). The CDK Patterns README details their implementations.

Note - This is a learning pattern, if I was implementing this in a production system I would make a couple of changes:

- Use batching of events where possible to reduce costs
- KMS Encryption of sensitive data in events
- SQS between EventBridge and the Lambdas for resiliency
- Add in error events to EventBridge and error event rules

Deconstructing The EventBridge ETL
{% youtube 8kg5bYsdem4 %}

### When To Use
This pattern is more a demonstration of what is possible with EventBridge in terms of building complex decoupled flows. I would use this to learn EventBridge.

### Alternatives
AWS Glue for complex flows or you could use a Lambda Function streaming larger files from S3.

<br /><br /><br />
<hr />

## The Lambda Power Tuner

![Lambda Power Tuner Arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-lambda-power-tuner/img/arch-small.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/the-lambda-power-tuner)

This is an AWS CDK project that deploys the awesome [AWS Lambda Power Tuning](https://github.com/alexcasalboni/aws-lambda-power-tuning) project by [Alex Casalboni](https://aws.amazon.com/developer/community/evangelists/alex-casalboni/).

AWS Lambda Power Tuning is an AWS Step Functions state machine that helps you optimize your Lambda functions in a data-driven way.

### When To Use
If you are using Lambda Functions in your application you will want to use the Lambda Power Tuning step function to optimize performance vs cost. 

### Alternatives
You could tune it by hand with enough effort

<br /><br /><br />
<hr />

## The Lambda Trilogy

![Lambda Trilogy Arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-lambda-trilogy/img/arch2-small.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/the-lambda-trilogy)

This is three patterns in one representing the three states of AWS Lambda.

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!

This pattern includes examples from [Paul Swail](https://twitter.com/paulswail), [Yan Cui](https://twitter.com/theburningmonk), [Jeremy Daly](https://twitter.com/jeremy_daly) and others

Deconstructing The Lambda Trilogy
{% youtube tHD3i06Z6gU %}

### When To Use
If you are using Lambda Functions in your application you will want to use one of the three states

<br /><br /><br />
<hr />

## The RDS Proxy

![rds proxy arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-rds-proxy/img/arch_overview.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/the-rds-proxy)

This is a project that has been configured with a MySQL RDS DB, an RDS Proxy, a Lambda Function to run queries and an API Gateway HTTP API to trigger the lambda function. The RDS Proxy is designed to bridge the gap between the massively scalable, serverless Lambda function and the less scalable RDS Instance

### When To Use
If you need to have a synchronous flow where a serverless component communicates with a non-serverless RDS Instance

### Alternatives
If you can go asynchronous you can use the scalable webhook

<br /><br /><br />
<hr />

## The SAGA Step Function

![saga step function arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-saga-stepfunction/img/arch-small.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/the-saga-stepfunction)

This is a pattern that demonstrates how use handle distributed transactions within a serverless system. It also demonstrates single table DynamoDB design inspired by [Alex Debrie](https://twitter.com/alexbdebrie)

Deconstructing The Saga Step Function
{% youtube 1nJNx6_KSRU %}

### When To Use
If you need to make multiple smaller transactions pass or fail in one atomic unit like in the example where you are booking a holiday and need flights and hotel booked together.

### Alternatives
This is a SAGA design pattern implemented with Step Functions, you could try implementing it with other components but it will probably be less efficient. 

<br /><br /><br />
<hr />

## The Scalable Webhook

![scalable webhook arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-scalable-webhook/img/architecture-small.png)

- [cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/the-scalable-webhook)
- [sra link](https://www.jeremydaly.com/the-scalable-webhook/)

This is a pattern from [Jeremy Daly](https://twitter.com/jeremy_daly) for integrating serverless resources like Lambda with non serverless ones like RDS in an asynchronous flow.

Deconstructing The Scalable Webhook
{% youtube kRI7QJfGBI8 %}

### When To Use
If you cannot predict your volume of traffic and you can use an asynchronous process to process the events

### Alternatives
The RDS Proxy but the scalable webhook can be applied to any resource, not just RDS.

<br /><br /><br />
<hr />

## The Simple GraphQL Service

![simple graphql arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-simple-graphql-service/img/architecture-small.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/the-simple-graphql-service/README.md)

This is a simple MVP implementation of AWS AppSync to use GraphQL to query a Lambda resolver and a DynamoDB table.

### When To Use
GraphQL allows your consumer to specify what pieces of information they need from your API which is really powerful. It also is helpful as an extra unifying wrapper on top of disparate APIs

### Alternatives
You could deploy Apollo in a Lambda Function

<br /><br /><br />
<hr />

## The Simple Webservice

![simple webservice arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-simple-webservice/img/architecture-small.png)

- [cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/the-simple-webservice)
- [sra link](https://www.jeremydaly.com/simple-web-service/)

The most basic pattern on cdkpatterns, the start of most peoples serverless journey. This is an API Gateway HTTP API, connected to a Lambda Function that queries DynamoDB

### When To Use
If you need to expose your DynamoDB table (or other serverless component) with a RESTful endpoint.

### Alternatives
The Simple GraphQL service and The Dynamo Streamer

<br /><br /><br />
<hr />

## The State Machine

![state machine arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-state-machine/img/arch-small.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/the-state-machine)

This is a simple State Machine implementation using Step Functions to implement the pattern of the same name described by [Jeremy Daly](https://twitter.com/jeremy_daly)

### When To Use
If you have complex orchestration logic between multiple Lambda Functions

### Alternatives
It is possible to also build event driven flows with EventBridge and Rules. You get a lot for free with Step Functions though like complete insights into every step executed

<br /><br /><br />
<hr />

## The XRay Tracer

![XRay Tracer Arch](https://raw.githubusercontent.com/cdk-patterns/serverless/master/the-xray-tracer/img/arch_notitle-small.png)

[cdk patterns link](https://github.com/cdk-patterns/serverless/blob/master/the-xray-tracer)

Learn about using AWS X-Ray for tracing events through your system. This pattern has X-Ray enabled on API Gateway, Lambda, DynamoDB, External HTTP calls, SNS and SQS

### When To Use
If you use serverless components in your system you should be using a tracing system to understand its behaviour

### Alternatives
You can instrument some third party alternatives like [Thundra](https://www.thundra.io/)

<br /><br /><br />
<hr />