0

I have two "main" packages that I need to run my two lambda functions : axios and @hubspot/api-client. I can run the run that only uses axios without a problem, but the one that uses the Hubspot client, keeps returning the error : "Cannot find module '@hubspot/api-client'"

Here is some code...

/* ./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 }),
    };
};

Now this is where the problem starts manifesting :

/* ./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 }),
    };
};

I never get to receive the code 200, because it fails with an Internal server error before.

My Lambda has a Layer which contains a zip file :

axiosHubspotLayer.zip
+-- nodejs/
    +-- node_modules/
        +-- @hubspot/
            +-- api-client/
        +-- axios/
        package.json
/* package.json */

{
    "dependencies": {
        "@hubspot/api-client": "^11.1.0",
        "axios": "^1.6.7"
    }
}

In the beginning, axios was problematic, but adding the Layer with the package helped. But for some reason the Hubspot Client doesn't work the same way.

I saw elsewhere the idea of installing the module at the root of the function... I'd like to avoid that if possible, because then it makes the package too big to be able to preview code in the Lambda console which is a small plus. And it sounds like Layers should be the solution anyways, I guess there's just something I'm doing wrong.

Thank you for your help !

1 Answer 1

0

Looks like I just had to investigate a bit further...

I don't completely understand how this works yet. But apparently, having both functions defined the way I did, it created two separate functions in the Lambda console : hubspot-fetch-dev-fetchContact and hubspot-fetch-dev-fetchContactBatch. It's confusing because each contains both the files hubspotFetchContact.js and hubspotFetchContactBatch.js.

So I found the hubspot-fetch-dev-fetchContactBatch function and also set it up with the hubspotAxiosLayer layer. Now it seems to be working.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.