Controlling AWS Lambda Costs
Tags: aws, cost, lambda, devops
Published: 2024-06-28 13:36:26

Managing your Amazon Web Services (AWS) costs can be challenging. AWS provides tools to limit usage and notify you when costs rise unexpectedly. This guide focuses on scalable AWS services like [AWS Lambda](https://aws.amazon.com/lambda/) to help you keep costs under control.

![XKDC economics](https://what-if.xkcd.com/imgs/a/111/summon.png)

Manually tearing down services when your budget is high is cumbersome. Instead, spend time setting up automated limits to prevent overspending.

## Steps to Control Costs
1. [Set Up Billing Alarms](#budget)
2. [Limit Lambda Concurrency](#concurrency)
   - [Reduce Global Service Quotas](#quota)
   - [Set Concurrency Limits for Individual Lambda Functions](#limit)
   - [Set Rate Limits on API Gateway](#api)
   - [Use AWS WAF to Avoid HTTP Flooding](#waf)
3. [Using a Killer Lambda](#killer)
4. [Closing Thoughts](#closing)

---

## Setting Up Billing Alarms <a name="budget"></a>

![XKDC alarm](https://imgs.xkcd.com/comics/phone_alarm_2x.png)

The first step is to set up budget alerts for your AWS costs. This way, you’ll get notified if your monthly AWS bill is estimated to cross a set threshold.

1. **Create a Budget**:
   - Go to the AWS Management Console.
   - Navigate to the AWS Budgets dashboard.
   - Click on "Create a budget."
   - Follow the steps to set your budget amount.

2. **Configure Alerts**:
   - Set up alert notifications for when your budget threshold is reached. You can receive alerts via email or SNS (Simple Notification Service).
   - To use SNS, create an SNS topic if you don't have one, and add subscribers (e.g., your email address).

With these alerts, you’ll know when you’re approaching your budget and can take action.

---

## Limiting Lambda Concurrency <a name="concurrency"></a>

![XKDC concurrency](https://hackaday.com/wp-content/uploads/2014/06/virus-aquarium.png?w=500)

Balancing scalability and cost is crucial. Limit Lambda invocations to prevent unexpectedly large bills.

### Reduce Global AWS Service Quotas for Lambda <a name="quota"></a>

Lower the total concurrency of all Lambda invocations (default is 1000):

1. Go to the AWS Management Console.
2. Search for "Service Quotas."
3. Filter for "Lambda."
4. Click on "Concurrent executions."
5. Click the "Request quota increase" button.
6. Fill in the new limit and submit the request.

### Set Concurrency Limits for Individual Lambda Functions <a name="limit"></a>

Limit concurrent invocations of specific Lambda functions:

1. Navigate to the desired Lambda function in the AWS Console.
2. Go to Configuration > Concurrency.
3. Click "Edit" in the Concurrency section.
4. Select "Reserve concurrency" and set the limit.
5. Click "Save."

### Set Rate Limits on API Gateway <a name="api"></a>

Control the number of requests sent to your backend:

1. Go to the AWS Management Console.
2. Search for "API Gateway."
3. Select your API.
4. Go to "Stages" and select the stage (e.g., `prod`, `dev`).
5. Scroll to the "Throttle" section in the "Stage Editor" tab.
6. Set the Rate Limit and Burst Limit.
7. Click "Save Changes."

### Use AWS WAF to Avoid HTTP Flooding <a name="waf"></a>

Protect against DDoS attacks by setting rate limits and blocking excessive requests:

1. Go to the AWS Management Console.
2. Search for "WAF."
3. Click on "Create web ACL."
4. Add a rate-based rule with your desired rate limit.
5. Set the action to "Block" when the rate limit is exceeded.
6. Select the resources you want to protect.
7. Click "Create web ACL."

---

## Using a Killer Lambda <a name="killer"></a>

To prevent AWS API Gateway and AWS Lambda from being invoked when a specific budget is reached, we can combine multiple AWS tools. For the sake of readability, I moved this part to a new article: [AWS cost control last resort, the killer lambda](https://dev.to/kodsama/aws-cost-control-last-resort-the-killer-lambda-5mk).

![XKDC to be continued](https://f.hypotheses.org/wp-content/blogs.dir/1807/files/2015/08/keep-calm-to-be-continued-5.png)

---

## Closing Thoughts <a name="closing"></a>

By following these steps, you can ensure that your AWS API Gateway and Lambda functions are managed effectively, preventing further costs and limiting possible cost overruns. This approach ensures your services are controlled and automatically restored at the beginning of the next period, keeping costs in check.

Managing AWS costs can be tough, but with the right setup, you can avoid unexpected expenses. Start with billing alarms and budgets, then use concurrency limits and API Gateway rate limits to control usage. AWS WAF can also protect against unexpected spikes.

Implementing these steps takes effort but will save you from manual intervention and unexpected costs in the long run. Focus on development, knowing your AWS costs are under control.

Address cost explosions in the development stage, but at least you can sleep peacefully until then. 😴