简体   繁体   English

AWS Sam 调用本地 lambda 的问题

[英]Issue with AWS Sam invoking local lambda

I have a python lambda which is just the sample hello_world which you can create using sam init .我有一个 python lambda 这只是 hello_world 示例,您可以使用sam init创建它。

I have modified it slightly by adding sub-folder in the lambda folder.我通过在 lambda 文件夹中添加子文件夹对其稍作修改。

So inside the hello_world lambda folder I have:所以在 hello_world lambda 文件夹中我有:

app.py # this is the lambda handler
requirements.txt
my_code_folder # I added this and I want to be able to import it and use it in the lambda. It contains a tonne of custom modules.

However when I run sam local invoke I get:但是,当我运行sam local invoke时,我得到:

[ERROR] Runtime.ImportModuleError: Unable to import module 'app': No module named 'hello_world'

If I take out the import it works fine.如果我取出导入它工作正常。

Perhaps I have imported incorrectly in my lambda?也许我在 lambda 中导入错误?

import hello_world.my_code_folder.MyModule as my_module

My SAM template has this:我的 SAM 模板有这个:

Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

Sample app created using sam init command and added a custom module named hello使用sam init命令创建的示例应用程序并添加了一个名为hello的自定义模块

❯❯ cat hello_world/hello/hello_world.py
# custom module
def hello():
    print("hello")
❯❯ cat hello_world/app.py
import json
import hello.hello_world as h


def lambda_handler(event, context):
...
    h.hello()

    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "hello world",
            # "location": ip.text.replace("\n", "")

directory structure目录结构

/tmp/foo/samapp via 🐍 v3.8.2 ((samapp))
❯❯ tree
.
..
├── __init__.py
├── events
│   └── event.json
├── hello_world
│   ├── __init__.py
│   ├── app.py
│   ├── hello. <--- my custom module
│   │   └── hello_world.py
│   └── requirements.txt
├── samconfig.toml
├── template.yaml

Uploaded code上传代码

在此处输入图像描述

Logs日志

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 SAM 本地中的另一个 lambda 调用 AWS lambda? - How to invoke AWS lambda from another lambda within SAM local? 本地调用 AWS SAM(lambda) python 代码时出现“没有名为“请求”的模块”错误 - 'No module named 'requests'' error when invoking AWS SAM(lambda) python code locally AWS SAM 本地调试是否收费? - AWS SAM local debugging is chargeable? 在 Jenkins 中使用 AWS SAM 部署 AWS Lambda - Deploying AWS Lambda using AWS SAM in Jenkins AWS SAM Local和docker-lambda:继续无法导入模块'lambda_function':没有名为'lambda_function'的模块 - AWS SAM Local and docker-lambda: keep getting Unable to import module 'lambda_function': No module named 'lambda_function' 从另一个 SAM 本地 function 调用 AWS SAM 本地 function - Invoke AWS SAM local function from another SAM local function AWS SAM lambda 授权方 Internet 访问 - AWS SAM lambda authorizer internet access 运行本地 sam invoke 命令时,AWS lambda 函数找不到主应用程序。 如何让 sam invoke 找到 app.py 应用程序? - AWS lambda function does not find the main app when running a local sam invoke command. How do I make sam invoke find the app.py application? 在AWS Lambda导入错误中调用python - Invoking python in AWS lambda import error 在 python 中从 Lambda 调用 AWS Step 函数 - Invoking AWS Step function from Lambda in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM