Our great sponsors
-
aws-lambda-power-tuning
AWS Lambda Power Tuning is an open-source tool that can help you visualize and fine-tune the memory/power configuration of Lambda functions. It runs in your own AWS account - powered by AWS Step Functions - and it supports three optimization strategies: cost, speed, and balanced.
-
aws-lambda-java-libs
Official mirror for interface definitions and helper classes for Java code running on the AWS Lambda platform.
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
Since it relies on an external API call, it does take a while to execute (1.285 seconds on average), which is very inefficient, and despite throwing Lambda Power Tuning at the problem, it's purely an external dependency issue, and can't be resolved by resources. You could make it more cost-effective using AWS Step Functions to handle the OpenAI API call asyncrhonously, but I didn't worry for this project.
AWS Lambda handles the call to OpenAI's API. The function itself is quite simple, although the supporting Lambda Layer was significantly larger. Since OpenAI's API costs money, I've leveraged AWS Secrets Manager to keep my API Key secure. To make debugging less nightmarish (we'll see why!), Amazon CloudWatch provides a simple way to store my basic log files, and Amazon API Gateway gives an ability to retrieve them!
The openai-python library makes interacting with the API a breeze. Although at 112MB fully installed, including requests, numpy, and pandas, it's pretty beefy. Consolidating it into a Lambda Layer made it simple and easy to manage. numpy didn't appreciate running in Lambda after being loaded for my Windows machine, so you may need to specify the right package before building the layer.
Just 49 lines of code. That's all of the Python code, including comments, which I had to write for Lambda to make this work, all available on GitHub. Everything else was configuring the existing services. In fact, it can be compacted down into less than 10 lines of code, though substantially harder to read.
Related posts
- Optimizing Costs in the Cloud: Embracing a FinOps Mindset
- How to enhance your Lambda function performance with memory configuration?
- Controlling Cloud Costs: Strategies for keeping on top of your AWS cloud spend
- Best way to decrease latency (API <-> Lambda <-> Dynamodb)
- How to optimize your lambda functions with AWS Lambda power tuning