Overview
Deploy the Premier League API as serverless functions on AWS Lambda. This deployment method provides automatic scaling, pay-per-use pricing, and zero server management. All file exports (CSV, JSON, PDF) are automatically saved to Amazon S3.Architecture
The Lambda deployment includes three separate functions:- PlayersPremierLeagueData: Handles player statistics endpoints (
/player_ranking,/player_csv,/player_json,/player_pdf) - RankingPremierLeagueData: Handles ranking endpoints (
/ranking,/ranking_csv,/ranking_json,/ranking_pdf) - TransferPremierLeagueData: Handles transfer endpoints (
/transfers_in,/transfers_out,/transfers_csv,/transfers_json)
- Runtime: Python 3.11
- Timeout: 30 seconds
- Memory: 1024 MB
- API Gateway: HTTP events for RESTful access
premier_league/lambda_functions/serverless.yml:25-112
Prerequisites
Before deploying, ensure you have:AWS Account
Create an AWS account if you don’t have one: aws.amazon.com
IAM Permissions
Configure IAM user/role with the following permissions:These permissions allow Lambda functions to read and write files to S3.
S3 Bucket
Create an S3 bucket in the AWS Console:
- Go to S3 service in AWS Console
- Click “Create bucket”
- Choose a unique name (e.g.,
premier-league-exports) - Select your preferred region
- Configure settings (default settings work fine)
- Click “Create bucket”
Deployment
Quick Deploy
Deploy with a single command using the built-in deployment script:S3_BUCKET_NAME: Your S3 bucket name (defaults topremier-league-bucketif not specified)--aws-profile: AWS CLI profile name (optional, uses default profile if not specified)--region: AWS region (e.g.,us-east-1,eu-west-1,ca-central-1)
premier_league/lambda_functions/deploy_premier_league.py:13-23
Deployment Examples
Deployment Output
After successful deployment, you’ll receive API endpoint URLs:Save these endpoint URLs - you’ll need them to interact with your deployed API.
Serverless Configuration
The deployment uses a pre-configuredserverless.yml file with the following structure:
- Service Name:
Premier_League_Data_Tool - Default Region:
ca-central-1(can be overridden with--region) - Stage:
dev(can be customized with--stageflag) - IAM Role: Automatically grants S3 read/write permissions
- Environment Variable:
S3_BUCKET_NAMEpassed to all Lambda functions
premier_league/lambda_functions/serverless.yml:1-13
Lambda Function Details
Player Statistics Function
Handler:src/player_lambda.lambda_handler
Endpoints:
GET /player_ranking- Get player statistics as JSONGET /player_csv- Export to CSV (saves to S3)PUT /player_json- Export to JSON (saves to S3)GET /player_pdf- Export to PDF (saves to S3)
season- Target season (e.g., “2023-2024”)stat_type- “G” for goals or “A” for assistsfilename- Required for export endpointslimit- Maximum number of resultsheader- Custom header for exportsleague- Target league name
premier_league/lambda_functions/src/player_lambda.py:63-79
Ranking Function
Handler:src/ranking_lambda.lambda_handler
Endpoints:
GET /ranking- Get league standingsGET /ranking_csv- Export to CSV (saves to S3)PUT /ranking_json- Export to JSON (saves to S3)GET /ranking_pdf- Export to PDF (saves to S3)
Transfer Function
Handler:src/transfers_lambda.lambda_handler
Endpoints:
GET /transfers_in- Get incoming transfersGET /transfers_out- Get outgoing transfersPUT /transfers_csv- Export to CSV (saves to S3)GET /transfers_json- Export to JSON (saves to S3)
premier_league/lambda_functions/serverless.yml:26-78
S3 File Storage
All exported files (CSV, JSON, PDF) are automatically saved to your configured S3 bucket. The Lambda functions handle the upload process:- Files use the
filenameparameter from your request - Extensions are added automatically (
.csv,.json,.pdf) - Files are stored at the root level of your bucket
premier_league/lambda_functions/src/player_lambda.py:12,42-43
Accessing Exported Files
- AWS Console
- AWS CLI
- boto3 (Python)
- Open AWS S3 Console
- Navigate to your bucket
- Find and download the exported file
Testing the Deployment
Test your deployed endpoints using cURL or any HTTP client:Managing the Deployment
Update Deployment
Redeploy after making changes:Remove Deployment
Remove all Lambda functions and API Gateway resources:View Logs
View function logs using AWS CLI:Cost Optimization
Lambda Pricing
Lambda Pricing
AWS Lambda pricing is based on:
- Requests: $0.20 per 1M requests
- Duration: $0.0000166667 per GB-second
- ~$0.0005 per request
- AWS Free Tier: 1M requests/month + 400,000 GB-seconds/month
S3 Storage Costs
S3 Storage Costs
S3 Standard pricing:
- Storage: $0.023 per GB/month
- PUT requests: $0.005 per 1,000 requests
- GET requests: $0.0004 per 1,000 requests
Optimization Tips
Optimization Tips
- Set up S3 lifecycle policies to delete old exports
- Use S3 Intelligent-Tiering for infrequently accessed files
- Monitor CloudWatch metrics to identify unused functions
- Consider reducing memory allocation if functions don’t need 1024 MB
- Implement caching to reduce duplicate API calls
Troubleshooting
Deployment fails with credentials error
Deployment fails with credentials error
Error: Enter your AWS Access Key ID, Secret Access Key, and default region.
AWS credentials not foundSolution: Configure AWS credentials:S3 bucket access denied
S3 bucket access denied
Error:
Access Denied when uploading to S3Solution: Verify your IAM role has the required S3 permissions:- Check the IAM role attached to Lambda
- Ensure it has
s3:PutObjectands3:GetObjectpermissions - Verify the bucket name in the environment variable matches your actual bucket
Function timeout errors
Function timeout errors
Error: Then redeploy.
Task timed out after 30.00 secondsSolution: Increase the timeout in serverless.yml:Package size too large
Package size too large
Error:
Deployment package size exceeds limitSolution: The serverless-python-requirements plugin handles this automatically by:- Using slim versions of packages
- Excluding unnecessary files
- Compressing the deployment package
Advanced Configuration
Custom Stage Deployment
Environment-Specific Buckets
Custom Domain Names
To use a custom domain:- Set up a domain in Route 53 or your DNS provider
- Create an ACM certificate
- Add custom domain configuration to
serverless.yml:
- Install the serverless domain manager plugin:
Next Steps
Local Deployment
Run the API locally for development and testing
API Endpoints
Explore available API endpoints and parameters
AWS Lambda Docs
Learn more about AWS Lambda
Serverless Framework
Deep dive into Serverless Framework