I have a Python Lambda and since I started using AWS X-Ray the package size has ballooned from 445KB to 9.5MB.
To address this and speed up deployments of my code, I have packaged my requirements separately and added a layer to my template. The documentation suggests that this approach should work.
Packaging dependencies in a layer reduces the size of the deployment package that you upload when you modify your code.
pip install --target ../package/python -r requirements.txt
Resources:
...
ProxyFunction:
Type: AWS::Serverless::Function
Properties:
Architectures:
- x86_64
CodeUri: proxy/
Handler: app.lambda_handler
Layers:
- !Ref ProxyFunctionLibraries
Role: !GetAtt ProxyFunctionRole.Arn
Runtime: python3.8
Tracing: Active
ProxyFunctionLibraries:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: proxy-function-lib
Description: Dependencies for the ProxyFunction.
ContentUri: package/.
CompatibleRuntimes:
- python3.8
However, this doesn't seem to have prevented the Lambda from still packaging everything in the top layer, and every time I deploy the package is still 9.5MB. The new layer for some reason is 11MB in size, but that is only being deployed when a change is made.
How can I reduce the size of the Lambda function package?

