The project I am currently working on creates a lambda layer which contains a file called app.py
, within this file is a function named lambda_handler
which is interest to be used as Handler
for whatever lambda function includes the layer. The sam template I use to do this looks as follow:
Resources:
LamLayer:
Type: AWS::Serverless::LayerVersion
LayerName: !Join
- ''
- - 'LamLayer'
- - !Ref AWS::StackName
Properties:
ContentUri: ./lam_layer
CompatibleRuntimes:
- python3.8
Metadata:
BuildMethod: python3.8
LamFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./lam_function
Runtime: python3.8
Handler: app.lambda_handler
Layers:
- !Ref LamLayer
Timeout: 60
AutoPublishAlias: live
Now although the Handler: app.lambda_handler
is not present in the lambda function itself, it is present in the included layer.
Now after creating this setup I tested it by calling sam build; sam deploy
and it successfully deployed and worked. When I called the LamFunction
it successfully found the Handler and ran it.
The problem arises when I push my changes to the CodePipeline we have setup. The build and deploy succeeded but when I now call the LamFunction
it throws the following error:
Unable to import module 'app': No module named 'app'
After debugging this for a while I seem to have narrowed down the problem to the difference in the way I was building vs. how the pipeline is building the project.
I called: sam build; sam deploy
Whereas the pipeline calls: sam build; sam package --s3-bucket codepipeline-eu-central-1-XXXXXXXXXX --output-template-file packaged-template.yml
and then uses the standard pipeline deploy stage to deploy from the S3 bucket.
But although I think I know that this difference is causing the problem I am not sure what the underlying reason is and what I need to change to fix it ?
---- EDIT ----
Here is the buildspec.yml
in case this is the culprit:
version: 0.2
phases:
install:
runtime-versions:
python: 3.8
build:
commands:
- sam build
- sam package --s3-bucket codepipeline-eu-central-1-XXXXXXXXXX --output-template-file packaged-template.yml
artifacts:
files:
- packaged-template.yml