When doing sam build with AWS Lambda Layers, the file hierarchy changes and an import error occurs.

Overview

sam deploy I couldn't import from Lambda Function just by doing sam build before printing, so I investigated the cause.

Target code

Lambda Layer Template

Resources:
  SystemSharedLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      ContentUri: src/layers/system_shared
      CompatibleRuntimes:
        - python3.8
    Metadata:
      BuildMethod: python3.8

** Lambda Layer Src structure **

src
└── layers
    └── system_shared
        ├── python
        │   └── hogehoge.py
        └── requirements.txt

** Caller **

import hogehoge.py

Expansion path on the Lamnda Function side

** If you didn't sam build **

/opt
└── python/
    └── hogehoge.py

** When sam build **

/opt
└── python/
    └── python/← Extra pass is given
        └── hogehoge.py

solution

Since the specifications are inconsistent, give up and switch the hierarchy specification on the template side

Resources:
  SystemSharedLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      ContentUri: src/layers/system_shared/python
      CompatibleRuntimes:
        - python3.8
    Metadata:
      BuildMethod: python3.8

Recommended Posts

When doing sam build with AWS Lambda Layers, the file hierarchy changes and an import error occurs.
What to do when an error occurs with import _ssl
[Beanstalk] What to do when an error occurs with import uuid
Solution when an error occurs when hiding the console screen with PyInstaller
An error occurs when trying to import scikit-learn after connecting to Oracle with SQLAlchemy
Rollback processing when an error occurs with fabric
A memorandum when an error occurs with pip install
After installing CentOS8 with VirtualBox, an error occurs when restarting
AWS Lambda with PyTorch [Lambda import]
Prepare an environment to use OpenCV and Pillow with AWS Lambda
Follow the file hierarchy with fts
What to do if an error occurs when importing numpy with VScode
[AWS] How to deal with WordPress "An error occurred when cropping an image."