DEV Community

Cover image for Controlling AWS Lambda Costs
Kodsama
Kodsama

Posted on • Edited on

Controlling AWS Lambda Costs

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 to help you keep costs under control.

XKDC economics

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
  2. Limit Lambda Concurrency
  3. Using a Killer Lambda
  4. Closing Thoughts

Setting Up Billing Alarms

XKDC alarm

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

XKDC concurrency

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

Reduce Global AWS Service Quotas for Lambda

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

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

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

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

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.

XKDC to be continued


Closing Thoughts

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. 😴

Top comments (0)