1

I am trying to create a lambda function to read from a google sheet when triggered by an API call. I had it working on my local machine but when I packaged it an put it as a lambda function it would not initialize the API using the service_account.json file. I know this because I do not see the API being accessed on the GCP console. This is the code used to initialize the API.

try:
    scopes = ["https://www.googleapis.com/auth/drive","https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/spreadsheets.readonly"]
    secrete_file = os.path.join(os.getcwd(), 'service_account.json')

    credentials = service_account.Credentials.from_service_account_file(secrete_file, scopes=scopes)
    service = discovery.build('sheets','v4', credentials=credentials)
    spreadsheet_id = '1W0ow-spreadsheeduidswedZNKW4qE'
    range_name = "Vender_List!A1:A1500"

    results =service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_name).execute()
    ids = results.get('values') 

I believe it has something to do with my use of the os module. FYI the os.getcwd() returns /var/tasks/. When I test the function I get the output:

START RequestId: b10c2b9b-23cb-4168-acbc-b2bf9e1250e8 Version: $LATEST
END RequestId: b10c2b9b-23cb-4168-acbc-b2bf9e1250e8
REPORT RequestId: b10c2b9b-23cb-4168-acbc-b2bf9e1250e8  Duration: 3033.58 ms    Billed     Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 88 MB  Init Duration: 303.20 ms    
2022-05-12T04:13:35.181Z b10c2b9b-23cb-4168-acbc-b2bf9e1250e8 Task timed out after 3.03 seconds

1 Answers1

2

Highly confident that your lambda just times out after 3 seconds while trying to connect to google api. Depending on your VPC setup you might need to make sure it's provisioned inside a private subnet (NAT is used for outbound). Otherwise provision it outside the VPC

b.b3rn4rd
  • 8,494
  • 2
  • 45
  • 57
  • I have updated the code and now it's showing that the `results` variable is not populated. I think this is because it is not awaited. Could that be the case? Should I redefine my functions as asynchronous and await the API calls to google? – chipego kalinda May 12 '22 at 07:45
  • The new error code ` { "errorMessage": "unsupported operand type(s) for +: 'NoneType' and 'int'", "errorType": "TypeError", "requestId": "431bfef8-be38-4aee-86a8-16e6e8800281", "stackTrace": [ " File \"/var/task/lambda_function.py\", line 6, in lambda_handler\n print(get_vendor_object(id))\n", " File \"/var/task/vendor_script.py\", line 74, in get_vendor_object\n vendor_profile_as_list = get_vendor(range_num)\n", " File \"/var/task/vendor_script.py\", line 46, in get_vendor\n index = index+1\n" ]` – chipego kalinda May 12 '22 at 07:47
  • thats the python runtime error, hard to tell what's causing it without seeing the code. Seems like one of your variables is None which means that something somewhere failed. – b.b3rn4rd May 12 '22 at 11:16
  • sorry to respond late. The solution was to make the functions that contained API calls asynchronous (declaring the functions as tasks) and then await them using the `asynchio` library in python. Then run an event loop in the main `lambda_handler()` function to start an event loop to wait for the calls to finish. Thanks for the help. – chipego kalinda May 26 '22 at 10:40