[英]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.