AWS Python的lambda函数内服务帐户初始化失败



我试图创建一个lambda函数从谷歌表读取时,由API调用触发。我让它在我的本地机器上工作,但是当我将它打包并将其作为lambda函数时,它不会使用service_account.json文件初始化API。我知道这一点是因为我没有看到在GCP控制台上访问API。这是用来初始化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') 

我相信这与我对os模块的使用有关。仅供参考,os.getcwd()返回/var/tasks/。当我测试这个函数时,我得到了输出:

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

非常有信心,你的lambda只是超时后3秒,而试图连接到谷歌api。根据您的VPC设置,您可能需要确保在私有子网内提供它(出站使用NAT)。否则在VPC外发放