Lambda timeouts when requesting internet resource

Hey everyone !

I’m doing a small app using serverless. I’m simply writing some webscraping in an aws lambda in order to retrieve some data with an API Gateway
The problem is, my lambda returns a timeout (changed the timeout to 30s just to be sure) and I have no clue how to debug this in the context of serverless.

Here is my lambda :

import json
from lambda_decorators import cors_headers
import requests

def handler(event, context):
    print("Was called with params : " + str(event))
    teamId = event["queryStringParameters"]['team_id']
    body = callNbaStatsApi(teamId)
    return {
        'statusCode': 200,
        'body': body

def callNbaStatsApi(teamId):
    print("Start callNbaStatsApi")
    nbaStatsUrl = '{0}&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&Split=general&TeamID={1}&VsConference=&VsDivision='
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0',
        'x-nba-stats-origin': 'stats',
        'Referer': '',
    url = nbaStatsUrl.format('2019-20', teamId)
    r = requests.get(url, headers=headers)
    print("End callNbaStatsApi")
    return r.json()

if __name__ == "__main__":

I notice two things :

  • When calling the python script on my own laptop the request works and print the nba data.
  • When the request goes to it does work inside of the aws lambda environment.

With all this, I suppose there is something that makes unavailable but I don’t know what.

Also, since it can be a problem, I have no VPC configuration on my lambda

The real question here is simply : how would you work to debug such a behavior ?

Thank you for your time :slight_smile:

@david-sauvage Did you have any luck with this, I am facing a similar issue