AWS Lambda Python Layer with yfinance
This repository contains an AWS Lambda Layer that has the yfinance
package (and all it's dependencies), optimized for AWS Lambda use. It allows AWS Lambda functions (that use the Python 3.10 runtime) to use yfinance
without bundling it with the function deployment package.
yfinance version: 0.2.36
To use this layer in your AWS Lambda function:
- Navigate to AWS Lambda in the AWS Management Console.
- Create or select an existing function.
- Under the
Layers
section, chooseAdd a Layer
. - Select
Provide a layer version ARN
and input the ARN provided below. - Save and deploy your function.
The ARN for the layer is:
Region | ARN |
---|---|
us-west-2 | arn:aws:lambda:us-west-2:100957474272:layer:yfinance_layer:7 |
See Deployment section with the details on how to deploy it in another region
Once you've added the layer to your Lambda function, you can import and use yfinance
just like you would in any Python script:
ad.user_cache_dir = lambda *args: "/tmp" # Lambda
import yfinance as yf
def lambda_handler(event, context):
ticker = yf.Ticker("AAPL")
hist = ticker.history(period="5d")
return hist.to_dict()
This AWS Lambda Layer is designed for users aiming to optimize the use of the yfinance
library within the AWS Lambda environment. Leveraging Lambdas can help distribute requests, providing a more efficient approach to handle rate limits imposed by Yahoo on their Finance API.
1. YFinance:
yfinance
facilitates fetching financial data from Yahoo Finance easily and in a Pythoniac way.
2. pysqlite3:
The Python environment in AWS Lambda bundles an older version of sqlite3
. This poses a challenge as yfinance
capitalizes on window functions, which are unsupported by this legacy version. To circumvent this and fully harness the capabilities of yfinance
, we've incorporated pysqlite3 into this Layer. This package provides an updated version of sqlite3
that aligns seamlessly with the requirements of yfinance
.
3. Exclusion of urllib3:
The rationale behind this decision is twofold:
-
yfinance
's version ofurllib3
was clashing with the version thatboto3
(which comes bundled by default with Lambda's Python environment) uses. This incompatibility could lead to unforeseen issues during execution. -
AWS Lambda's Python environment naturally includes its own variant of
urllib3
. By steering clear of incorporating an overlapping and potentially conflicting version, we've chosen to trust Lambda's inherenturllib3
.
If the provided Layer ARN isn't available in your AWS region, or you want to have your own private deployment, you can deploy the Layer yourself. Here's a step-by-step guide:
-
Clone the repo.
-
ZIP the
python
folder. -
Log in to AWS Management Console: Open the AWS Management Console and navigate to the AWS Lambda service.
-
- Upload the zip file to S3.
- Copy the S3 path of the uploaded file.
- Click on "Layers" in the left navigation panel.
- Click on "Create layer".
- Provide a name for the layer.
- Type the previously copied S3 path.
- Select the appropriate runtime (Python 3.10).
- Add a description (optional) and click on "Create".
-
Once the Layer is created, AWS will provide an ARN for the Layer. Note this down and go to Quick Start.
If you deploy this Layer in a new region and wish to contribute the ARN to help others:
-
Fork the Repository: Click on the "Fork" button on the top-right corner of this repository page.
-
Clone Your Forked Repository:
git clone [URL of your forked repository]
-
Update the ARN Table in README.md: Navigate to the
README.md
file in your cloned repository. Add the ARN for the new region to the existing table. -
Commit and Push Your Changes:
git add README.md git commit -m "Added ARN for [region-name]" git push origin main
-
Send a Merge Request (MR): Go back to the main page of your forked repository on GitHub. Click on "New Pull Request". Ensure the base repository is set to the original repository (not your fork) and the base branch is
main
. Fill out the necessary details and submit the pull request.
Once your MR is reviewed and merged, the new ARN will be available in the main repository's README.md, benefiting all users looking for that specific region.