2

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

Mercury
  • 594
  • 1
  • 12
  • 28

1 Answers1

0

In the end I managed to trace the issue back to the CodeBuild image used in the pipeline. Due to an oversight during the creation of the pipeline I used a managed image which used the CodeBuild standard 1 which does not support the building of nested stacks/templates. Since the stack mentioned above was being build as nested stack of a larger template it was not build and so with cause the error with the layer.

After changing to the CodeBuild standard 3 the stack build and packaged as expected.

Mercury
  • 594
  • 1
  • 12
  • 28
  • I am sorry for not including the detail that this stack was nested in the question as I did not think it was relevant the problem, I will make sure to be more thorough with future questions. – Mercury Jun 17 '21 at 05:32