简体   繁体   English

无法通过 python 请求从我的本地计算机连接到 Lambda 或 EC2

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

Example of correct lambda function正确的 lambda 函数示例

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.

相关问题 从本地机器将 Python 连接到 AWS EC2 上的 kafka - Connect Python to kafka on AWS EC2 from local machine 通过(python)lambda在EC2上运行作业 - Run a Job on EC2 from a (python) lambda 无法从我的本地 PC 和 AWS EC2 实例的 Twitter Web 获得相同的请求结果 - Unable to get the same requests results from Twitter Web from my local PC and AWS EC2 instance 使用Python从EC2实例连接到RDS - Connect to RDS from EC2 instance with Python 在本地计算机上运行Shell脚本,并在AWS EC2实例上运行脚本-如何跟踪ongion输出? - running a shell script on my local computer that runs a script on aws ec2 instance - How can I follow the ongion output? 有什么方法可以将照片从我的电脑上传到 ec2? - Is there any way to upload photos from my computer to ec2? 如何在我的AWS EC2实例上处理多个Python请求? - How can I handle multiple Python requests on my AWS EC2 instance? 无法在 ec2 上运行 python sanic - Can't run python sanic on ec2 AWS Lambda函数无法与EC2实例通信 - AWS Lambda function can't communicate with EC2 Instance 如何在 AWS EC2 上运行 Python 代码并将 csv 文件从服务器写入我的本地计算机? - How to run a Python code on AWS EC2 and write a csv file from the server to my local machine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM