尝试在 AWS 上的 lambda 中调用 pycurl 时出错

问题描述

我想使用 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'

我按照教程 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

然后将 zip 作为层上传到 aws 中,但是在启动 lambda 时出现另一个错误:

    Unable to import module 'lambda_function': libssl.so.1.0.0: cannot open shared object file: No such file or directory

似乎该层必须建立在某种扩展目标环境上。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)