我无法从AWS上托管的、通过无服务器框架管理的基于python的lambda函数进行HTTP调用。
我试过使用botocore.vendored请求模块,但它显示了不推荐警告,并建议使用请求模块本身。

url = V2_URL + '/api/analytics/validate/' + smId
headers = {
    'Content-Type':'application/json',
    'Authorization': token
}

response = requests.get(url, headers=headers)

print('Result: ')
print(response.content)

在Cloudwatch中,我看到这个堆栈跟踪:
[ERROR] UnboundLocalError: local variable 'response' referenced before assignment
Traceback (most recent call last):
  File "/var/task/serverless_sdk/__init__.py", line 97, in wrapped_handler
    return user_handler(event, context)
  File "src/authorize.py", line 21, in validate
    principal_id = verify_token(whole_auth_token, event['pathParameters']['smId'])
  File "src/authorize.py", line 38, in verify_token
    response = requests.get(url, headers=headers)
  File "/var/task/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/var/task/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/var/task/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/var/task/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/var/task/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/var/task/serverless_sdk/__init__.py", line 384, in wrapper
    if response:

最佳答案

正如@blhsing和@Mark A所指出的,@serverless/enterprise-plugin包的3.1.1版本中存在一个bug。升级到3.1.2版本解决了这个问题。我所要做的就是照顾好自己。
详情如下:https://github.com/serverless/serverless/issues/6801

关于python - request.get()在AWS Lambda中引发异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58269903/

10-11 10:34