Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Python Layer Run Locally

How does one run locally a AWS Lambda Function with layers?

My environment:

  • Pycharm project for an AWS Lambda Function with Python 3.6 runtime.
  • AWS Toolkit
  • similar file/folder structure to create a Lambda Layer: https://aws.amazon.com/blogs/compute/working-with-aws-lambda-and-lambda-layers-in-aws-sam/ as follows:
    +---.aws-sam
        ....
    +---test
    |       app.py
    |       requirements.txt
    |       
    +---dependencies
    |   \---python
    |           constants.py
    |           requirements.txt
    |           sql.py
    |           utils.py
  • and deployment template like:
testFunc:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: teest/
      Handler: app.test
      Runtime: python3.6
      FunctionName: testFunc
      Events:
        test:
          Type: Api
          Properties:
            Path: /test
            Method: ANY
      Layers:
        - !Ref TempConversionDepLayer

  TempConversionDepLayer:
        Type: AWS::Serverless::LayerVersion
        Properties:
            LayerName: Layer1
            Description: Dependencies
            ContentUri: dependencies/
            CompatibleRuntimes:
              - python3.6
              - python3.7
            LicenseInfo: 'MIT'
            RetentionPolicy: Retain

I can deploy the function correctly and running it on AWS works well, whenever i try to run the function locally, it fails with the error message:

`Unable to import module 'app': No module named 'sql'`

I've tried to read all possible resources about Layers and Pycharm but nothing really helped.

Can anybody give a hand please?

Thank you,

like image 581
devplayer Avatar asked Oct 15 '22 11:10

devplayer


1 Answers

I was able to get around this issue in PyCharm by adding a symbolic link to another directory which contained code for the layer

like image 80
Oleg Kozlov Avatar answered Oct 20 '22 17:10

Oleg Kozlov