我有两个“主”包,需要它们来运行我的两个 lambda 函数:axios 和 @hubspot/api-client。我可以运行仅使用 axios 的运行,没有问题,但使用 Hubspot 客户端的运行,不断返回错误:“
Cannot find module '@hubspot/api-client'
”
这是一些代码...
/* ./serverless.yml */
org: ...
app: website-hubspot-integration
service: hubspot-fetch
frameworkVersion: "3"
provider:
name: aws
runtime: nodejs18.x
functions:
fetchContact:
handler: hubspotFetchContact.hubspotFetchContact
events:
- httpApi:
path: /contact/{contactId}
method: get
fetchContactBatch:
handler: hubspotFetchContactBatch.hubspotFetchContactBatch
events:
- httpApi:
path: /batch
method: get
/* ./hubspotFetchContact.js */
"use strict";
const axios = require("axios");
module.exports.hubspotFetchContact = async (event) => {
const hubspotutk = event?.pathParameters.contactId || false;
/*
Not putting the whole code, but basically, it verifies if hubspotutk
exists, and if it does, an axios call is made to Hubspot like
const contact = await axios.get(`http://api.hubapi.com/contacts/v1/contact/utk/${hubspotutk}/profile?property=${fetchProperties}&propertyMode=value_only&showListMemberships=false`);
Everything in this part works as expected.
*/
return {
statusCode: 200,
body: JSON.stringify({ result }),
};
};
现在这就是问题开始显现的地方:
/* ./hubspotFetchContactBatch.js */
"use strict";
const hubspot = require("@hubspot/api-client");
module.exports.hubspotFetchContactBatch = async (event) => {
/* Not much to see, just testing... */
const hubspotClient = new hubspot.Client({ accessToken: process.env.HS_TOKEN });
return {
statusCode: 200,
body: JSON.stringify({ hubspotClient, event }),
};
};
我从未收到代码 200,因为它之前因内部服务器错误而失败。
我的 Lambda 有一个包含 zip 文件的层:
axiosHubspotLayer.zip
+-- nodejs/
+-- node_modules/
+-- @hubspot/
+-- api-client/
+-- axios/
package.json
/* package.json */
{
"dependencies": {
"@hubspot/api-client": "^11.1.0",
"axios": "^1.6.7"
}
}
一开始,axios 有问题,但是通过包添加 Layer 有所帮助。但由于某种原因,Hubspot 客户端的工作方式不同。
我在其他地方看到了在函数根部安装模块的想法...我想尽可能避免这种情况,因为这样会使包太大而无法在 Lambda 控制台中预览代码,这是一个小加。听起来无论如何,图层应该是解决方案,我想我做错了什么。
谢谢您的帮助!
看来我只需要进一步调查一下......
我还不完全明白这是如何工作的。但显然,按照我的方式定义这两个函数,它在 Lambda 控制台中创建了两个单独的函数:
hubspot-fetch-dev-fetchContact
和 hubspot-fetch-dev-fetchContactBatch
。这很令人困惑,因为每个文件都包含文件 hubspotFetchContact.js
和 hubspotFetchContactBatch.js
。
所以我找到了
hubspot-fetch-dev-fetchContactBatch
函数,并且还用 hubspotAxiosLayer
层来设置它。现在看来有效了。