aws-amplify / amplify-cli

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

"amplify mock function <>" pending for a few hours #5085

Closed AugustusLi closed 4 years ago

AugustusLi 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 I have a lambda function owlpostsfunction, I want to test it locally, so I run the following command, it is pending on "Starting execution..." for a few hours. I am using rest, not graphql API.

$ amplify mock function owlpostsfunction

? Provide the path to the event JSON object relative to ********/amplify/backend/f
unction/owlpostsfunction src/event.json
Starting execution...

I don't touch event.json, here is what it is:

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

Amplify CLI Version amplify -v 4.26.0

To Reproduce Run "amplify mock function owlpostsfunction"

Expected behavior I think it shouldn't pending for a few hours. Even if I doesn't configure it correctly, it should throw some errors out.

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

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

edwardfoyle commented 4 years ago

@AugustusLi This would be what happens if your lambda function is awaiting a promise that is never resolved. I would suggest checking your lambda function for that case.

That being said, I agree we could timeout the mock execution after some time (probably 15 mins as that is the max execution time for Lambda functions)

marking this as an enhancement

AugustusLi commented 4 years ago

Thank you, Edward. In this case, what's this promise? Could you tell me how can I find out what promise Amplify sent to Lambda after I ran "amplify mock function owlpostsfunction"?

edwardfoyle commented 4 years ago

Mock doesn't send a promise to the Lambda handler. It will invoke it in the same way as the Lambda service: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html If your handler is async, you need to make sure the promise is eventually resolved. And if you are using the callback, you need to make sure that the callback is eventually called. Otherwise execution will hang forever.

AugustusLi commented 4 years ago

I am still confused how to use "amplify mock function<>". I use exactly the same code as Nader Dabit's tutorial: https://dev.to/aws/10-minute-tutorial-deploy-an-apollo-graphql-server-with-amplify-functions-38p1. And the codes has been "amplify push", and it works successfully on "localhost:3000". But if I use the same code and run "amplify mock function<>", it shows:

$ amplify mock function owlpostsfunction
? Provide the path to the event JSON object relative to ***/owl-web/amplify/backend/function/owlpostsfunction src/event.json
Starting execution...
^Cundefined:1

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at ChildProcess.<anonymous> (/****/.nvm/versions/node/v14.5.0/lib/node_modules/@aws-amplify/cli/node_modules/amplify-nodejs-function-runtime-provider/lib/utils/invoke.js:45:37)
    at ChildProcess.emit (events.js:314:20)
    at ChildProcess.EventEmitter.emit (domain.js:486:12)
    at maybeClose (internal/child_process.js:1051:16)
    at Socket.<anonymous> (internal/child_process.js:442:11)
    at Socket.emit (events.js:314:20)
    at Socket.EventEmitter.emit (domain.js:486:12)

If I follow the tutorial https://docs.amplify.aws/cli/usage/mock#function-mocking-setup, adding the following @function directive, it would be pending for very long time as I mentioned before.

const typeDefs = gql`
  type Query { 
    hello: String @function(name: "owlpostsfunction-${env}")
  }
` 

So why it works after "amplify push", but doesn't work using "amplify mock function <>". Anything I missed to configure? If yes, any tutorial with more details about how to use "amplify mock" for rest api?

Thank you

edwardfoyle commented 4 years ago

@AugustusLi what do you mean by "works sucessfully on localhost:3000"? The unexpected end of JSON input will be fixed by #4978 When running amplify mock function are you passing in the same event as when it works?

AugustusLi commented 4 years ago

@edwardfoyle Thank you. Just figured out how to set a correct event.json for Graphql, and succeed to get the correct response by running "amplify mock function ". My problem is solved.

Btw, if I run "amplify mock", it returns nothing. Is it because "amplify mock" only works on AppSync? Not work for REST?

$ amplify mock
$               <-- returns nothing
edwardfoyle commented 4 years ago

@AugustusLi yes amplify mock only works for AppSync APIs at this time. To simulate a REST request you would need to create an API gateway event as a .json file and pass it in to the function using amplify mock function

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.