简体   繁体   English

Python)对使用 AWS Lambda 的 AWS 网关调用 Rest API 的任何引用?

[英]Python) Any reference to call Rest API made by AWS gateway using AWS Lambda?

Here, I have created DB REST API using API Gateway, method is POST.在这里,我使用 API Gateway 创建了 DB REST API,方法是 POST。 I want to build a another REST API that calls the DB API, which passes the event to DB API and return the response from it (with some more jobs).我想构建另一个调用 DB API 的 REST API,它将事件传递给 DB API 并从它返回响应(还有更多工作)。 Both languages are Python 3.9两种语言都是 Python 3.9

While I could use requests library from outside of AWS Lambda and call DB API and got the result, using the exact same code and run it at AWS Lambda gives me error message that it cannot connect to DB API .虽然我可以使用来自 AWS Lambda 外部的请求库并调用 DB API 并获得结果,但使用完全相同的代码并在 AWS Lambda 上运行它会给我错误消息,它无法连接到 DB API

I checked both DB API function and API caller function in same VPC, same security code and the role has following:我检查了同一 VPC 中的 DB API 函数和 API 调用函数,相同的安全代码,角色有以下几点: 角色

I know I can just invoke DB API lambda function inside the Lambda but I need that REST API so that other services can access to that DB API我知道我可以在 Lambda 中调用 DB API lambda 函数,但我需要该 REST API 以便其他服务可以访问该 DB API

Can anyone help me on achieving what I want?任何人都可以帮助我实现我想要的吗? Thank you谢谢

ADDED添加

So I want to know what I have missed when people usually calls AWS gateway API by AWS lambda.所以我想知道当人们通常通过 AWS lambda 调用 AWS 网关 API 时我错过了什么。

My expectation was since it worked in other environment, it must work at lambda too but in reality it wasn't even able to connect to the API.我的期望是因为它在其他环境中工作,它也必须在 lambda 上工作,但实际上它甚至无法连接到 API。

I used the following code to call the API:我使用以下代码调用 API:

# DB API caller
def lambda_handler(event, context):

    URL = 'THE_URL_OF_DEPLOYED_API'
    timeout = 10

    # event is in format of dictionary
    response = requests.post(URL, json=event, timeout=timeout)

Followed by this answer .其次是这个答案 I found both my DB API and DB caller API lambda functions were in within same VPC.我发现我的 DB API 和 DB caller API lambda 函数都在同一个 VPC 中。 DB API function needs the VPC setting BUT DB caller API actually didn't needed VPC. DB API 功能需要 VPC 设置,但 DB 调用者 API 实际上不需要 VPC。 So I removed the VPC configuration from DB API caller lambda function and I could successfully got the response.所以我从 DB API caller lambda 函数中删除了 VPC 配置,我可以成功获得响应。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM