【问题标题】:AWS Python Layer Run LocallyAWS Python 层在本地运行
【发布时间】:2019-09-16 11:35:06
【问题描述】:

如何在本地运行带有层的 AWS Lambda 函数?

我的环境:

    +---.aws-sam
        ....
    +---test
    |       app.py
    |       requirements.txt
    |       
    +---dependencies
    |   \---python
    |           constants.py
    |           requirements.txt
    |           sql.py
    |           utils.py
  • 和部署模板如:
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

我可以正确部署该功能并且在 AWS 上运行它运行良好, 每当我尝试在本地运行该函数时,它都会失败并显示错误消息:

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

我已尝试阅读有关 Layers 和 Pycharm 的所有可能资源,但没有任何帮助。

有人可以帮忙吗?

谢谢,

【问题讨论】:

  • app.py 中的import 语句是什么样的?
  • @PeterHalverson,例如:from utils import * utils 是在 AWS 中 /opt 下扩展的依赖项/python/utils.py,并正确导入。错误是:>(昨天我有一个类似的导入,但我希望从 sql.py 文件中导入。)

标签: python amazon-web-services aws-lambda layer


【解决方案1】:

我可以在 PyCharm 中通过将符号链接添加到另一个包含图层代码的目录来解决这个问题

【讨论】:

  • 我正在尝试使用 pyodbc 并对其进行符号链接,但仍然遇到问题。我将 /pyodbc 符号链接到 /path/to/extracted/layer_archive 我现在导入了一个名为 pyodbc 的东西,但它没有任何预期的方法。
猜你喜欢
  • 2022-06-10
  • 2013-12-03
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
  • 2020-08-20
  • 2020-10-20
相关资源
最近更新 更多