【发布时间】:2021-05-27 20:46:46
【问题描述】:
我想使用 pycurl 来获得 TTFB 和 TTLB,但无法在 AWS lambda 中调用 pycurl。
为了专注于这个问题,假设我称这个简单的 lambda 函数为:
import json
import pycurl
import certifi
def lambda_handler(event, context):
client_curl = pycurl.Curl()
client_curl.setopt(pycurl.CAINFO, certifi.where())
client_curl.setopt(pycurl.URL, "https://www.arolla.fr/blog/author/edouard-gomez-vaez/") #set url
client_curl.setopt(pycurl.FOLLOWLOCATION, 1)
client_curl.setopt(pycurl.WRITEFUNCTION, lambda x: None)
content = client_curl.perform()
dns_time = client_curl.getinfo(pycurl.NAMELOOKUP_TIME) #DNS time
conn_time = client_curl.getinfo(pycurl.CONNECT_TIME) #TCP/IP 3-way handshaking time
starttransfer_time = client_curl.getinfo(pycurl.STARTTRANSFER_TIME) #time-to-first-byte time
total_time = client_curl.getinfo(pycurl.TOTAL_TIME) #last requst time
client_curl.close()
data = json.dumps({'dns_time':dns_time,
'conn_time':conn_time,
'starttransfer_time':starttransfer_time,
'total_time':total_time,
})
return {
'statusCode': 200,
'body': data
}
我有以下错误,可以理解:
Unable to import module 'lambda_function': No module named 'pycurl'
我按照 tuto https://aws.amazon.com/fr/premiumsupport/knowledge-center/lambda-layer-simulated-docker/ 来创建一个图层,但是在使用 docker 生成图层时出现以下错误(我提取了有趣的部分):
Could not run curl-config: [Errno 2] No such file or directory: 'curl-config': 'curl-config'
我什至尝试生成刚刚在我自己的机器上启动的层:
pip install -r requirements.txt -t python/lib/python3.6/site-packages/
zip -r mypythonlibs.zip python > /dev/null
然后在 aws 中将 zip 作为图层上传,但是在启动 lambda 时出现另一个错误:
Unable to import module 'lambda_function': libssl.so.1.0.0: cannot open shared object file: No such file or directory
似乎该层必须建立在某种扩展目标环境上。
【问题讨论】:
-
为什么不直接使用
urllib。这不需要任何层或依赖项,因为它是 Python 标准库的一部分。即使您不想使用urllib,也可以使用requests,这可能是比您的Lambda 中的curl更好的选择。 -
谢谢@Jens,这可能是一种解决方法。然而,据我所知,只有
pycurl能够提供诸如 TTLB、TTFB 和 DNS 查找时间等指标。 -
好的。从您提供的代码中并不清楚。在这种情况下,
urllib或requests可能没有帮助。 -
你说得对,我把代码改成明确的。
标签: amazon-web-services aws-lambda layer pycurl