[英]Can't connect to Lambda or EC2 from my local computer through python requests
I'm new at AWS and I'm trying to deploy a simple lambda service and call it from my local computer.我是 AWS 的新手,我正在尝试部署一个简单的 lambda 服务并从我的本地计算机调用它。
Lambda:拉姆达:
import json
import pandas
def lambda_handler(event, context):
message = 'Hello {} {}!'.format(event['first_name'],
event['last_name'])
return {
'message' : message
}
When I run a test on AWS env it does work, but when I try to do the same kind of test on python I get the 502 error with rest API on API gateway, and error 500 on HTTP API also on API gateway.当我在 AWS env 上运行测试时,它确实有效,但是当我尝试在 python 上进行相同类型的测试时,API 网关上的 rest API 出现 502 错误,API 网关上的 HTTP API 也出现错误 500。
Test AWS: { "first_name": "alooo", "last_name": "arrombado" }测试 AWS:{ "first_name": "alooo", "last_name": "arrombado" }
Local Python Test:本地 Python 测试:
import resquests
r2 = requests.post('https://ia81y8e8ye.execute-api.eu-west-3.amazonaws.com/default/PortAPI',
json = {'first_name':'jose','last_name':'example'})
r2
<Response [502]>
The same kind of problem happen on EC2 instance when try to deploy an python flask API.尝试部署 python Flask API 时,EC2 实例上会发生同样的问题。
With Lambda I didn't use any kind of permission, so I think it has a open traffic.使用 Lambda 我没有使用任何类型的许可,所以我认为它有一个开放的流量。 In other hand, in EC2 I set inbound and outbound, all communications to anywhere.
另一方面,在 EC2 中,我设置了入站和出站,所有通信都到任何地方。
I don't know if more info is needed.我不知道是否需要更多信息。
Thanks for the help.谢谢您的帮助。
If you run the following:如果您运行以下命令:
print(r2.content)
you will see that you get:你会看到你得到:
b'{"message": "Internal server error"}'
This likely means that your lambda most likely failed .这可能意味着您的lambda 很可能失败了。 In that case you have to check CloudWatch Logs and search for any error message.
在这种情况下,您必须检查 CloudWatch Logs 并搜索任何错误消息。
And this can happen because you may be using incorrect event
in your function, and/or returning incorrect response type.这可能是因为您可能在函数中使用了不正确的
event
,和/或返回了不正确的响应类型。 Structure of event object for proxy integration is here for reference.代理集成的事件对象结构在这里供参考。 Other reason could by missing
pandas
in your lambda.其他原因可能是在您的 lambda 中缺少
pandas
。
Assuming lambda proxy integration, the correct function would be:假设 lambda 代理集成,正确的函数是:
import json
def lambda_handler(event, context):
body = json.loads(event['body'])
print(body)
message = 'Hello {} {}!'.format(body['first_name'],
body['last_name'])
return {
"statusCode": 200,
'body' : message
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.