Six Costly Findings in AWS Serverless Computing
Tags: aws, cloud, serverless, architecture
Published: 2024-01-23 16:00:00


I've worked in serverless computing for over five years and wanted to share some lessons learned.

By the term "costly," I don't mean necessarily dollars or how expensive it was. "Costly" could mean difficulties in troubleshooting, difficulties in architecture, and just pain points I've discovered over the years.

There are many more, but this post focuses on the costly findings that have stood out.

## An overview of serverless

Read the following post for an overview of serverless.

[https://medium.com/serverless-is-cool/a-super-quick-overview-of-serverless-52b1e5cf9e2f](https://medium.com/serverless-is-cool/a-super-quick-overview-of-serverless-52b1e5cf9e2f)

## Input injection

Until just recently, OWASP Top Ten had input injection as the top risk. It is still the top risk in the Serverless Top Ten.

The OWASP Top Ten API Security Risks has broken object-level authorization as the highest risk. This means that if I ask an API for data,  it  will give it to me. Then, if I ask it for another user's data, it'll give me that, too, even if I'm still logged in with my account. Since I'm not meant to get someone else's data,  it seems appropriate that it would be a higher risk than input injection.

Input injection is still high on the list, and it would be important to address that first.
 
![Input injection lesson](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xup8xbe916cm7fxdiyy2.png)

In the scenario above, we designed a Lambda function to get an update from the database to get an event from a DynamoDB table whenever the table data changes.

The Lambda function then sends a clear cache command to the API Gateway.

This workflow allowed the API Gateway to cache data and its cache to be cleared when there was new data. The DynamoDB table data stayed the same most of the time, and caching would reduce the number of read transactions on the table.

Unfortunately, this resulted in an accidental input injection attack. Somehow, the DynamoDB stream would resend old events. Some of these events were old. Furthermore, multiple old events were sent in a short period.

The Lambda function was designed to clear the API Gateway cache when it received an event. The numerous events resulted in multiple commands to the API. Shortly after, the API reached its rate limit and stopped responding to requests.

We experienced a self-induced denial of service attack!

![Input injection remediation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rr9y2mizo14pvk2oio0k.png)

We reworked our Lambda function when we realized our users could not get data because of this DoS event.

The Lambda function would validate each input rather than assuming it was "safe" to process. It would check the timestamp and only process events created within a few minutes. Furthermore, it would keep track of the last time it cleared the API cache. It would only clear the cache no sooner than five minutes than the previous time.

![Review of input injection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4oq9ajha1vt3xwywmnnz.png)

The image above shows some input injection risks and mitigations to consider.

## Over-privileged policies

I joined an existing serverless project. I discovered all the Lambda functions within a Serverless Framework service used the default IAM role instead of having one least privileged role per function.

![Over-privileged policies lesson](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfjhkcpr8bsc1d78ve0f.png)

The above shows the example of a role with an over-privileged IAM policy.

I emulated an injection attack where I would send a malicious command to the AWS CLI. (The Lambda function code allowed me to send Linux commands to the operating system with the AWS CLI installed.) I was able to modify other parts of the AWS infrastructure.

An attacker could have issued a malicious command to IAM and started an account takeover.

![Over-privileged policies remediation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdwtfedfo55h7umymh3a.png)

We created an IAM role for each function and assigned a unique IAM policy that had the least privileges to allow the function to execute successfully.

I attempted the same malicious exploit, and the function threw an error.

![Review of over-provisioning](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6yusfc9fmfyvya775ky.png)

The image above shows some over-provisioning risks and mitigations to consider.

## Failures and service outages

It is easy to assume that AWS services will never go down or experience degradation. Designing for the "happy path" is the most logical approach. But, it is essential to consider the "negative path" where things go wrong.

![Failures and service outages lesson](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0l0r6kehuxfv4gm29ny.png)

One day, we experienced degradation in CloudWatch rules. (Since then, it was rebranded to EventBridge.) Some of our functions failed to work for hours.

Some functions were designed for the scheduled events to get third-party data to store it in our DynamoDB tables. The third-party data source only provided new data every six minutes. If we failed to save the data within that time, we would never have access to it again.

We lost data for hours. That affected our user experience and billing.

![Failures and service outages remediation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojh8focplnm8b9y092b3.png)

To remediate this, we started monitoring the health of the AWS services. We would be notified when there was an outage.

We made sure all functions exited gracefully by using try-catch blocks. We could log errors that could be monitored. If our monitoring noticed an increase in logged errors, that would be an indicator to investigate.

We explored having multi-region scheduled events. For example, if the North Virginia region was down, we could still get scheduled events from the Ohio region.

![Review of failures and service outages](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1y0y8kdgq12ms2czyx1c.png)

The image above shows some failure and service outage risks and mitigations to consider.

## Cost engineering

We found our AWS bill was costly. For almost a year, we accepted it as the "cost of moving" to the cloud.

![Cost engineering lessons](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dgxaifl0j7tavv9fr2p5.png)

We decided to investigate whether we were making the best use of the AWS cloud and being as efficient as possible.

![Cost engineering remediations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amjs9bc1uake0gm1cb1k.png)

We observed our DynamoDB traffic patterns and realized they were irregular. We realized we would save money by moving to on-demand capacity even though it was more expensive than the provisioned capacity.

We realized we had old log and S3 files that were no longer needed. We took advantage of lifecycle rules to delete old, unnecessary data.

An initial version of an app used Kinesis, but we found it was more cost-effective to use SQS.

![Review of cost engineering](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5ix9eoc8ljprljc6l5c.png)

The image above shows some cost risks and mitigations to consider.

## Encryption

We wanted to be super secure and enabled customer-managed KMS keys for all our data. We found our AWS bill increased considerably.

![Encryption lessons](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/muvfik85id9ij18w7oxj.png)

We discovered the AWS-managed keys were less expensive than the customer-managed keys (CMK). We did not have security requirements that required us to use CMKs. We moved all our encryption to AWS-managed keys where possible.


![Encryption remediations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7e13rtd1qcsjsk8yrulg.png)

Better security did not provide business value in this case.

![Lessons on encryption](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49cb8evg6az77n9aiira.png)

The image above shows some encryption risks and mitigations to consider.

## Spaghetti

We sometimes created "serverless spaghetti" designs.

![Serverless spaghetti lesson](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbvuxjwi1k0v9x5vyibo.png)

Much like "spaghetti code" that is difficult to trace and has complex dependencies, the same can happen in a serverless design.

The image above shows an unnecessarily complex design in an attempt to implement a "pure" and "clean" microservice design.

![Serverless spaghetti remediations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0oycss67y6wtoq6ix8h.png)

We reviewed our design and attempted to simplify it. The above shows how we simplified the previous design example. Getting the data directly from the table was cleaner than going through a microservice.

![Review of serverless spaghetti](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pwhgr30y9nyiflrlmf3.png)

The image above shows some serverless spaghetti risks and mitigations to consider.

## Watch the LayerOne presentation

{% embed https://youtu.be/wRJ9m2EODOs %}

## Before you go

Here are other posts you might enjoy.

[AWS CDK Serverless Cookbook: A Step-by-step Guide to Build a Serverless App in the Cloud](https://medium.com/chapter-by-chapter/aws-cdk-serverless-cookbook-ebook-1d4d4e0488c)

[Using Docker for AWS CDK development: Have a consistent development environment for your team on any OS
](https://miguelacallesmba.medium.com/using-docker-for-aws-cdk-development-7054086deb3d)

[Speed up AWS CDK deploys up to 80%: The ts-node package needed the speed boost](https://aws.plainenglish.io/speed-up-aws-cdk-deploys-up-to-80-c47afad1c18c)

[Build Robust Cloud Architectures: Applying Military Design Principles to Cloud Applications](https://medium.com/illumination/build-robust-cloud-architectures-d512fb9eae1a)

[Introduction to Cloud Computing Security: An excerpt from the Serverless Security book](https://medium.com/serverless-is-cool/introduction-to-cloud-computing-security-a37b6c4bc4f1)