When creating an AWS Lambda using Python:
- Can the Lambda access local imports if the modules are included in the Lambda handler zip; and
- What are the implications of including the
__pycache__
directories in the zip?
Question 1: Can the runtime access local imports?
The AWS documentation focuses on the Lambda handler Python file containing the handler function itself. This is obviously must be included in the deployment zip. But we don't want one big function, or even several functions or classes in one big file.
If using the usual Python approach of creating sub-directories, containing modules and packages, included in the directory containing the handler itself, and included in the zip that is uploaded to the AWS Lambda handler, will that code be accessible at run-time, and therefore be importable by the Lambda handler?
I'm not referring to the AWS Lambda support for "layers", which is normally used for providing access to packages that are installed in a virtual environment with pip
etc. I (think I) understand that support and am not asking about that.
I specifically just want to clarify: can the Lambda handler import from local files, for instance, an adjacent definitions.py
being referenced by from definitions import *
(please no judgements about don't import star :-) as long as it's also in the zip?
Question 2: Is it good practice to include the __pycache__
directories?
In the AWS Python Lambda deployment documentation the output of a zip command shows the packages included with the Lambda handler Python file, also including a __pycache__
directory. Additional libraries are also shown but it seems intended that these are collected from the layers.
The AWS documentation shows the inclusion of __pycache__
but no mention is made at all.
I believe AWS Lambda run-times are certain specific versions of Python running on AWS Linux images. I'm currently forced to develop on Windows :-(. Will this mismatch cause issues for the run-time? Would other considerations come into play such as ensuring included bytecode is the same version of Python as the runtime?
Doesn't Python expect bytecode for particular packages in a particular relative location? Presumably the top-level (in the zip) __pycache__
directory should only contain handler routine bytecode?
Does the Lambda runtime even use the __pycache__
directories? If this is workable and working, given the Lambda may only run once before being destroyed, does that imply that developers should put effort in to providing the bytecode in the Lambda zip to improve Lambda performance? In this case, is it necessary to run sufficient tests across the code before zipping it for Lambda, to ensure all the bytecode is generated?
Context
I have reviewed various articles on creating an AWS Lambda zip using Python, including the AWS documentation, but the content is shallow and simplistic, failing to clarify the precise "reach" of the runtime. It's not even in the AWS Lambda Handler Cookbook.
I do not (yet) have access to a live AWS environment to test this out, and given this broad omission in the on-line documentation and community commentaries (most articles just parrot the AWS documentation anyway, albeit with worse grammar), I thought it would be good to get a clarification on SO.