简体   繁体   English

从另一个 SAM 本地 function 调用 AWS SAM 本地 function

[英]Invoke AWS SAM local function from another SAM local function

I am trying to create an AWS SAM app with multiple AWS Serverless functions.我正在尝试创建一个具有多个 AWS 无服务器功能的 AWS SAM 应用程序。

The app has 1 template.yaml file which have resource of 2 different serverless lambda functions, for instance "Consumer Lambda" and "Worker Lambda".该应用程序有 1 个模板。yaml 文件具有 2 个不同的无服务器 lambda 函数的资源,例如“Consumer Lambda”和“Worker Lambda”。 Consumer gets triggered at a rate of 5 minutes.消费者以 5 分钟的速度被触发。 The consumer uses boto3 library to trigger the worker lambda function.消费者使用boto3库触发worker lambda function。 This code works when the worker lambda is deployed on AWS.当工作程序 lambda 部署在 AWS 上时,此代码有效。

But I want to test both the functions locally with Sam local invoke "Consumer" which invokes "Worker" also locally.但是我想用 Sam local invoke "Consumer" 在本地测试这两个函数,它也在本地调用 "Worker"。

Here's a screenshot of the YAML file:这是 YAML 文件的屏幕截图:

I am using Pycharm to run the project.我正在使用 Pycharm 来运行该项目。 There is an option to run only 1 function at a time which then creates only one folder in the build folder.有一个选项可以一次只运行 1 个 function,然后在构建文件夹中只创建一个文件夹。

I have to test if Consumer is able to invoke worker locally in pycharm before deployment.我必须在部署之前测试消费者是否能够在 pycharm 本地调用工作人员。 I think there is some way to do it but not sure how to.我认为有一些方法可以做到这一点,但不知道该怎么做。 I did some extensive search but didn't yield anything.我做了一些广泛的搜索,但没有产生任何结果。

Any help is appreciated.任何帮助表示赞赏。 Thanks in advance提前致谢

You can start the lambda invoke endpoint in the following way (official docs ):您可以通过以下方式启动 lambda 调用端点(官方文档):

sam local start-lambda

Now you can point your AWS resource client to port 3001 and trigger the functions locally.现在,您可以将 AWS 资源客户端指向端口 3001 并在本地触发函数。

For eg.例如。 If you are doing this on Python, it can be acheived in the following way with boto3:如果您在 Python 上执行此操作,则可以使用 boto3 通过以下方式实现:

boto3

# Create a lambda client
lambda_client = boto3.client('lambda',
                             region_name="<localhost>",
                             endpoint_url="<http://127.0.0.1:3001>",
                             use_ssl=False,
                             verify=False)

# Invoke the function
lambda_client.invoke(FunctionName=<function_name>,
                     Payload=<lambda_payload>)

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

相关问题 如何从 SAM 本地中的另一个 lambda 调用 AWS lambda? - How to invoke AWS lambda from another lambda within SAM local? 运行本地 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 SAM 本地调试是否收费? - AWS SAM local debugging is chargeable? AWS Sam 调用本地 lambda 的问题 - Issue with AWS Sam invoking local lambda 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' 从 localstack 访问 S3 时,sam local invoke 获取 connectionRefused - sam local invoke gets connectionRefused when accessing S3 from localstack 如何在 Visual Studio Code 上使用 Python 设置 AWS SAM 本地调试? - How to setup AWS SAM local debugging with Python on Visual Studio Code? 使用 AWS SAM CLI 安装 PyODBC 以进行本地调试时出现问题 - Problem installing PyODBC for local debugging with AWS SAM CLI 无法在 AWS SAM 本地开发中导入 python package - Unable to import python package in AWS SAM local development 如何在 sam 本地环境中使用依赖项? - How to use dependencies in sam local environment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM