3

I'm not able to make HTTP calls from python based lambda function hosted on AWS and managed through Serverless framework.

I've tried using botocore.vendored requests module but it shows deprecation warning and suggested to use the requests module itself.

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)

In Cloudwatch, I see this stack trace:

[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
  • 91,368
  • 6
  • 71
  • 106
Anil Kumar
  • 459
  • 6
  • 16
  • Assign to `response` instead of `result`: `response = requests.get(url, headers=headers)`. – blhsing Oct 07 '19 at 13:15
  • That was a typo, check my comment on first answer. – Anil Kumar Oct 07 '19 at 13:17
  • Your traceback shows that the execution stops at ` if response:` and yet it is not part of your posted code. Please post your actual code if you want us to help. – blhsing Oct 07 '19 at 13:21
  • Well if you noticed, that part of code is not part of my function and is auto-generated by serverless sdk. – Anil Kumar Oct 07 '19 at 13:23
  • I see. This is indeed interesting and likely a bug of the SDK then. – blhsing Oct 07 '19 at 13:29
  • I agree this looks like a bug in the SDK and the SDK bug is masking your real issue. (It looks like the SDK doesn't handle exceptions properly). You might try to put a try/except around your code and print out the exception that your code is raising. – Mark A Oct 07 '19 at 16:32
  • Fixed in the latest release of serverless SDK. Thank you, everyone, for the valuable inputs. – Anil Kumar Oct 09 '19 at 14:26

1 Answers1

1

As @blhsing and @Mark A pointed out, there was a bug in version 3.1.1 of @serverless/enterprise-plugin package. Upgrading it to version 3.1.2 solved the issue for me. All I had to do was npm i -g serverless and it took care of itself.

Details of issue here: https://github.com/serverless/serverless/issues/6801

Anil Kumar
  • 459
  • 6
  • 16