Hey folks, I'm using a postConfirmation lamdbajs f...
# help
i
Hey folks, I'm using a postConfirmation lamdbajs function, and it seems like when I deploy, imports/require statements don't work and cause an error. Code looks like this:
Copy code
/* eslint-disable no-console */
const fetch = require('node-fetch');

export async function main(event, context, callback) {
  try {
    // Post user data to elastic

    console.log('posting data to elastic...', process.env.API_URL);

    await fetch(
      `${process.env.API_URL}/users`, {
        method: 'POST',
        headers: {
          'content-type': 'application/json',
        },
        body: JSON.stringify(event),
      },
    )
      .then((res) => res.json()) // expecting a json response
      .then(() => callback(null, event));
  } catch (error) {
    console.log('there was an error', error);
    callback(null, event);
  }
}
It works fine when testing locally, but after running
sst deploy
and trying out the workflow, I noticed this in CloudWatch:
Copy code
2021-08-16T02:58:40.094Z	51cc72c5-0e44-4b15-8360-66170252a532	INFO	there was an error TypeError: fetch is not a function
    at Runtime.main [as handler] (/var/task/postConfirmation.js:1080:11)
    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)
So the one module I import is not working. It's definitely in my package.json:
Copy code
"dependencies": {
    "@aws-cdk/core": "1.111.0",
    "@serverless-stack/cli": "0.38.0",
    "@serverless-stack/resources": "0.38.0",
    "node-fetch": "^2.6.1"
  },
Any thoughts?
f
Hey @Ilia Reingold, can I see how you are creating the postConfirmation lambda?
i
Hey @Frank Full code :
Copy code
import {
  Auth,
  Stack,
} from '@serverless-stack/resources';

export default class MyStack extends Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    const auth = new Auth(this, 'Auth', {
      cognito: {
        userPool: {
          userPoolName: process.env.POOL_NAME,

          selfSignUpEnabled: true,

          userVerification: {
            emailSubject: 'Verify your email for Condobeaver!',
            emailBody: 'Thanks for signing up to Condobeaver! Your verification code is {####}',
            // emailStyle: cognito.VerificationEmailStyle.CODE,
            smsMessage: 'Thanks for signing up to Condobeaver! Your verification code is {####}',
          },

          standardAttributes: {
            fullname: {
              required: true,
              mutable: false,
            },
          },

          signInAliases: {
            email: true,
          },
        },

        triggers: {
          postConfirmation: 'src/postConfirmation.main',
        },
      },
    });

    // Show the endpoint in the output
    this.addOutputs({
      UserPoolId: auth.cognitoUserPool.userPoolId,
      IdentityPoolId: auth.cognitoCfnIdentityPool.ref,
      UserPoolClientId: auth.cognitoUserPoolClient.userPoolClientId,
    });
  }
}
Maybe I need to import Function or something? 😮
f
hmm.. can you run
sst build
, zip up
.build/cdk.out
and DM it to me?
I can take a look and see if
node-fetch
is included in the Lambda zip.
i
Sure!
Hmm, looks like it worked after using build before deploy
f
oh what do you by build before deploy?
i
I ran
sst build
then ran
sst deploy
and that time it worked
f
Hmm..
sst deploy
runs
sst build
internally.
I was going to say maybe
node-fetch
hasn’t been npm installed into
node_modules
, but u mentioned that it worked for `sst start`… that means
node-fetch
must have been isntalled.
i
I also figured that sst deploys runs sst build
Well, no need to lose sleep over it. I'll keep an eye and let you know if anything else pops up 🤷
f
Sounds good! Thanks Ilia.