I am working with AWS SAM (Serverless Application Model) to build Python 3.6 lambda code in an API Gateway setup.
As such, I have a single template.yaml
file that creates several Lambda functions. They are organized with the lambda functions each in their own sub-directory within the project. The lambda also share several common files which I keep in a shared folder.
project-home
-lambda_a_dir
-lambda_a.py
-lambda_b_dir
-lambda_b.py
-shared_dir
-shared.py
The problem is that while Pycharm can clearly see the shared.py
, SAM cannot and refuses to recognize the shared files, with the following error: Unable to import module 'lambdaA': No module named 'shared'
If I move a copy of the shared.py
file into each lambda directory, both Pycharm and SAM are happy and I can build/deploy to AWS.
My question: how can I build the SAM template with the shared files living in the shared directory?
So far, I have tried:
- Symbolic link and MacOS alias.
- Various combinations of
CodeUri
alternatives - Local package with
__init__
andsetup.py
. (I can't use a public package because the code is private and cannot not be put on a public repository.)
Here is my template file:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
lambdaA:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./lambda_a_dir/
Handler: lambda_a.lambda_handler
Runtime: python3.6
lambdaB:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./lambda_b_dir/
Handler: lambda_b.lambda_handler
Runtime: python3.6
shared_dir
content in a separate layer should do the trick. – Helioaws cloudformation package
command should follow symlinks. – Athelstan