我目前正在尝试requests在AWS Lambda函数中使用Python 模块.以下是我到目前为止采取的步骤:
我创建了一个新目录,并使用该命令在其中安装了请求模块 pip3 install requests -t .
然后我test_requests.py在目录中编写了一个简单的Python脚本,如下所示:
import requests
def my_handler(event, context):
r = requests.get("http://google.com")
return r
Run Code Online (Sandbox Code Playgroud)
我使用了压缩整个目录,包括请求模块 zip test_requests.zip *
然后,我使用以下命令将该函数上载到AWS: aws lambda create-function --function-name test_requests --zip-file fileb://test_requests.zip --handler test_requests.my_handler --runtime python3.6 --region us-east-1 --role xxxMY_ROLE_ARNxxx
最后,我用这个命令调用了这个函数: aws lambda invoke --function-name test-requests --payload {} --region us-east-1 lambda_response.txt
当我发出这个命令时,我从Lambda得到了一个未处理的异常.输出文件lambda_response.txt包含:{"errorMessage": "module 'requests' has no attribute 'get'", "errorType": "AttributeError", "stackTrace": [["/var/task/test_requests.py", 3, "my_handler", "r = requests.get('http://google.com')"]]}
我已经看到几个关于AWS lambda的问题,并且无法正确导入模块.这些问题似乎都集中在lambda无法找到模块.在这种情况下,似乎lambda已找到请求,但无法访问其所有属性.
我目前正在尝试了解两个类似的curl 命令之间的区别。
我启动了一个 AWS EC2 Ubuntu 实例,并在该实例上安装了 node.js。然后我创建了一个名为 test-server 的新目录。在这个目录中,我运行了npm init,然后运行了npm install express。然后我编写了一个名为 的简单 Web 侦听器test-server.js,如下所示:
const express = require('express')
const app = express()
app.get('/', function(req, res){
console.log('Got a request!')
res.send('Request received!\n')
})
app.listen(443, function(){
console.log('Server started')
})
Run Code Online (Sandbox Code Playgroud)
最后用命令启动服务器sudo node test-server.js,服务器启动成功。
使用aws ec2 authorize-security-group-ingress,我允许通过端口 443 从本地 IP 地址到实例进行通信。然后,我尝试使用以下 2 个curl 命令测试连接:curl <ip>:443和curl https://<ip>。第一个命令给出了预期的输出Request received!。但是,我从第二个命令收到此错误:curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to <ip>:443。 …