Ilia Reingold
08/16/2021, 3:02 AM/* 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:
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:
"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?Frank
Ilia Reingold
08/16/2021, 3:22 AMimport {
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,
});
}
}
Ilia Reingold
08/16/2021, 3:24 AMFrank
sst build
, zip up .build/cdk.out
and DM it to me?Frank
node-fetch
is included in the Lambda zip.Ilia Reingold
08/16/2021, 3:36 AMIlia Reingold
08/16/2021, 3:53 AMFrank
Ilia Reingold
08/16/2021, 4:14 AMsst build
then ran sst deploy
and that time it workedFrank
sst deploy
runs sst build
internally.Frank
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.Ilia Reingold
08/16/2021, 4:19 AMIlia Reingold
08/16/2021, 4:19 AMFrank