aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.82k stars 821 forks source link

authPreSignup default function fails #3979

Closed kjetilge closed 4 years ago

kjetilge commented 4 years ago

Note: If your issue/bug is regarding the AWS Amplify Console service, please log it in the Amplify Console GitHub Issue Tracker

Describe the bug After adding the auth authPreSignup signup failes with error: CustomMessage failed with error index.handler is undefined or not exported. (In broeser)

When running amplify mock authPreSignup it failes with error:

(node:4238) UnhandledPromiseRejectionWarning: Error: Could not load lambda handler function due to Error: Cannot find module '/..../amplify/backend/function/authPreSignup/src/index'

CustomMessage failed with error index.handler is undefined or not exported.

Amplify CLI Version 4.18.0

To Reproduce

  1. Add authPreSignup
  2. Run amplify mock function authPreSignup -> missing src/index.js
  3. Log in from browser: -> index.handler is undefined or not exported

Expected behavior Both mocking and actual backend should just work

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context If I add src/index.js, mockin works (using the code below). If I change authPreSignup/index.js to:

exports.handler = (event, context, callback) => {
  callback(null, event);
};

Then it works. I've also succeded with exporting "custom" in env, but don't remember exactly how.

In total this is not a good developer experience. It feels like a bug.

edwardfoyle commented 4 years ago

This is fixed by #3928. We are planning to do a release that includes this change by the end of the week. Until the release is out, you can work around the issue by moving the index.js file that's in amplify/backend/function/<functionName> to the src folder.

djkmiles commented 4 years ago

It's not enough to just move that file because the whole package/zip/tree isn't being made properly, the dependencies are unavailable. This issue is not closed, this is a critical issue that prevents release. It affects other cognito trigger functions as well, such as post-confirmation

It's not cool to be closing issues until they are handled

Version 4.18.0 does not fix this

edwardfoyle commented 4 years ago

@djkmiles Sorry, you're right, you also need to move the package.json file into the src folder. Everything in the src folder will be zipped and uploaded to Lambda. #3928 above will fix the issue, but we ran into some other issues getting a release out last week so it's not available yet. I'll reopen until the release is complete

edwardfoyle commented 4 years ago

@djkmiles This should be fixed in the latest release (4.18.1). Feel free to reopen if you're still running into issues.

kjetilge commented 4 years ago

So I tried the same as above again today using amplify --version 4.19.0

amplify mock function funcnameAuthPreSignup
Provide the path.... src/event.json
Could not find ref for "modules". Using unsubstituted value.
Could not find ref for "DOMAINWHITELIST". Using unsubstituted value.
Could not find ref for "DOMAINBLACKLIST". Using unsubstituted value.
Starting execution...
ffba7AuthPreSignup failed with the following error:
{ code: 'MODULE_NOT_FOUND' }
Finished execution.

So It still does not work out-of-the-box.

edwardfoyle commented 4 years ago

Hi @kjetilge I believe that is a problem with mock and not with the lambda function itself. We are tracking the issue where environment variables are not loaded into mock correctly in #4085. Does the function work as expected when uploaded to Lambda via amplify push?

kjetilge commented 4 years ago

Using amplify --version 4.20.0 it works when uploaded via Lambda push but not with mock.

borisilic commented 4 years ago

I'm having a similar issue with PostConfirmation:

-> amplify mock function frontend3a437cf5PostConfirmation

Could not find ref for "modules". Using unsubstituted value.
Could not find ref for "GROUP". Using unsubstituted value.

frontend3a437cf5PostConfirmation failed with the following error:
{
  type: 'Lambda:Unhandled',
  message: "Cannot find module './modules'\n" +
    'Require stack:\n' +
    '- /home/boris/Documents/development/frontend/amplify/backend/function/frontend3a437cf5PostConfirmation/src/index.js\n' +
    '- /home/boris/.nvm/versions/node/v12.14.0/lib/node_modules/@aws-amplify/cli/node_modules/amplify-nodejs-function-runtime-provider/lib/utils/execute.js'
}
amirmishani commented 3 years ago

Just tested this with Amplify 4.32.1 and seeing the same issue.

ThierDev commented 3 years ago

Tested in Amplify 4.40.0 and getting the same issue too

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.