chipego kalinda
chipego kalinda

Reputation: 49

Service account initialization failing inside lambda function on AWS Python

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

Upvotes: 1

Views: 208

Answers (1)

b.b3rn4rd
b.b3rn4rd

Reputation: 8830

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

Upvotes: 2

Related Questions