1

New to Lambda and trying to figure out something basic. Have a function that is making calls to an external REST server. I can run this Python code on my local test system without issue:

    payload = {'userName': user_name, 'password': password}
    r = requests.post(api_base + "/session",
                      data=json.dumps(payload),
                      headers={'accept': 'application/json',
                               'Content-Type': 'application/json'}
                      )

But when i run it in lambda I get odd errors from the my log message that prints out the exception in CloudWatch (obscured my URL):

No connection adapters were found for '""/session'

If I hard-code the URL then it works fine. Is there some problem with forming the URL in Python that perhaps has different behaviors between the Python versions (I'm using 2.7.14)? The quoting in the exception string that I logged makes me believe that string concatenation is working differently between systems.

I'm was also having a horrible time loading zip files and editing in the browser today but it seems like it perhaps is related to some recent changes effecting Chrome since when i switch to Explorer things work fine.

Here is more of the relevant code:

try:

    # Get session key
    payload = {'userName': user_name, 'password': password}
    if DEBUG:
        print("api_base = ", api_base, ", full url = ", api_base + "/session")
    r = requests.post(api_base + "/session",
                      data=json.dumps(payload),
                      headers={'accept': 'application/json',
                               'Content-Type': 'application/json'}
                      )

    # Check return status
    if r.status_code != 200:
        if DEBUG:
            print("HTTP Request error: ", r.status_code,
                  ", data = ", payload,
                  ", api_base = ", api_base)
        return ""

    session_data = r.json()
    return session_data['token']

except requests.exceptions.RequestException as e:
    print(e)
    return ""

Executing this code on my local machine works just fine. When I run it in lambda I get the following output:

api_base = "https://[redacted].com/rest" , full url = "https://[redacted].com/rest"/session No connection adapters were found for '"https://[redacted].com/rest"/session'

I redacted with '[]' the basename of the URL for security reasons. As you can see, lambda seems to print the string concatenation with strange quoting.

-Wayland

  • 1
    What is the value of `api_base`? Enabling logging might reveal something: https://stackoverflow.com/a/24588289/21945. (not sure if that will work in Lambda). – mhawke Dec 03 '17 at 10:36
  • 1
    Also you don't need to manually create the JSON string, just pass `payload` with the `json` parameter to `requests.post()`. – mhawke Dec 03 '17 at 10:37
  • The value of api_base is just a regular url of the form https://foobar.com/rest. The exact code runs fine on my local system so i don't think it is a problem with the URL itself. I've added some instrumentation and the error shown is the output from a print of the exception caught by the try/except block. The code is constructed this way because i pass in api_base as an environment variable in lambda then append the right url qualifier based upon the intent called. I can just hardwire all of the urls but not how i would like to construct the code. – Wayland Jeong Dec 03 '17 at 17:36
  • You are getting an `InvalidSchema` exception. That means that `requests` thinks that the schema part of the URL is invalid, e.g. "blah://blah.com/session". You have obscured it in your question, but what is the full exception message? I would guess that the problem is related to the use of environment variables - perhaps Lambda modifies them in some way. Please print out exactly what your code is getting for `api_base`. – mhawke Dec 03 '17 at 23:17
  • Thanks a bunch for the assistance. I added more detail to the original question. – Wayland Jeong Dec 04 '17 at 01:00
  • How are you setting `api_base`? Is it set from an environment variable? Check that the quotes were not entered in the environment variables section in AWS console. Then check that the double quotes are not being introduced elsewhere in your code. Worst case fix is to strip off those quotes: `api_base.replace('"', '')` or similar. – mhawke Dec 04 '17 at 11:00
  • The environment vars are being set through the lambda service and takes advantage of KMS for security. I just noticed that when I entered the value for api_base i must have cut and pasted a string that included quotes :-( Anyways, that was the problem. Thanks again for pointing me in the right direction!! – Wayland Jeong Dec 04 '17 at 22:07
  • No worries, had to be something simple! – mhawke Dec 04 '17 at 22:35
  • have you tried str(api_base) ? – sid8491 Dec 05 '17 at 06:58

0 Answers0