My CloudWatch bill was more expensive than 2.8 million Lambda compute seconds

How unchecked Python print statements can lead to significant costs in AWS.

My CloudWatch bill was more expensive than 2.8 million Lambda compute seconds

Table of contents

No heading

No headings in the article.

Introduction

In this post, I'll talk about my recent encounters with AWS, specifically the unsuspected expenses tied to Python logging, and share the valuable lessons I learned.


Background

Our platform is designed for intense data analysis. The front end boasts a React UI, while the back end employs an S3 deployment, CloudFront distribution, and more. We chose DynamoDB for storing data and used the API gateway to integrate powerful Python Docker Lambda functions. These functions, requiring around 4GB memory and 30-240 seconds of compute time, manage the crux of our data analysis.


Sneaky AWS Costs

AWS is renowned for its generous free tier, and I aimed to leverage it to its fullest during the platform's construction. Initially, my focus was on the Lambda compute costs, foreseeing them as the major component of my bill.

In the initial phase, my predictions were spot on. The Lambda expenses overshadowed other costs, racking up about 15 million compute seconds monthly. Dedication and tweaking allowed me to reduce this by half. However, a new adversary was on the horizon: logs.

Utilizing the Cloud Development Kit (CDK) meant that, by default, Lambda functions were set to retain logs indefinitely. My oversight was assuming that storage size would gradually increase, possibly exceeding the free tier threshold. To my astonishment, this wasn't the prime suspect behind the rising costs.

After a meticulous examination of my bill, I discovered the real culprit: the PutLogEvents. My excessive use of print statements in Python was causing a cost explosion. In layman's terms, I overdid the print statements, unintentionally shooting myself in the foot. Or, as it turned out, both feet.


Key Takeaways:

  1. Understand Every AWS Service: Every service, especially when automated using tools like the CDK, has default configurations that might not always align with your budget or requirements.

  2. Monitor and Regularly Check Your Bills: AWS provides detailed billing insights. A routine check can help in spotting anomalies before they burn a hole in your pocket.

  3. Optimize Logging in Python: Resorting to print statements for logging in Python, especially in a cloud environment, can be counterproductive. Adopt more structured logging methods.


Conclusion

AWS is the objectively best cloud provider, but like all great things, it requires understanding and respect. My journey taught me the importance of being vigilant about the services I use, monitoring costs, and optimizing wherever possible. And yes, print statements might seem harmless, but in the cloud, they can rain on your parade.


If you have any unexpected AWS costs or difficult projects you could use some help with, reach out! lets chat!