serverless-next.js: 503 Error when invoking NextJS API Route from Client

Describe the bug

I am using Amplify’s API functionality to invoke an AppSync GraphQL endpoint to resolve my API calls. But, I’m getting a GraphQL error message in my Lambda@Edge CloudWatch logs when trying to invoke my API in my application from the client side.

Error: No credentials
    at GraphQLAPIClass.<anonymous> (/var/task/pages/api/users/[username].js:163397:35)
    at step (/var/task/pages/api/users/[username].js:163243:23)
    at Object.next (/var/task/pages/api/users/[username].js:163224:53)
    at fulfilled (/var/task/pages/api/users/[username].js:163215:58)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

The weird thing is that when I try to execute the API’s internal logic on the server side, everything works properly. So, this correctly retrieves and display all data in my application.

# pages/index.js
export const getServerSideProps = async ({ req, res }) => {
  const response = await API.graphql(graphqlOperation(UserQueries.GetUser, <username>))
  
  return {
    props: {
      user: response.data,
    },
  };
};

However, if if I invoke this api route, by going to <app_id>.cloudfront.net/api/users/[username], I get the result under “Actual behavior”.

# pages/api/users/[username]/index.js

const handler = async (req, res) => {
  const { username } = req.query.username;
  const response = await API.graphql(graphqlOperation(UserQueries.GetUser, <username>))
  res.status(200).json(response.data);
};

This tells me that I have set up my Cognito and identity pools correctly. For now I am just developing using an API Key, so I expect there to be no permissions barriers.

Actual behavior

If I go to <app_id>.cloudfront.net/api/users/[username] I receive the following error page:

503 Error Message from Application

Expected behavior

API query response should be returned when invoked client side.

Steps to reproduce

  1. Deploy a GraphQL API using CDK (separately, not using Amplify)
  2. Create. NextJS application (https://nextjs.org/docs/getting-started)
  3. Put the AppSync Endpoint URL in the config and set up a custom “_app.js” file using Amplify.configure(config);
  4. Add an API Route to the NextJS Application using the instructions here: https://nextjs.org/docs/api-routes/introduction.
  5. Handle the API route by using Amplify’s API client: const response = await API.graphql(graphqlOperation(operation, params))
  6. Add a serverless.yaml at the root of the project with the following content:
# serverless.yml
App:
  component: "@sls-next/serverless-component@1.18.0"
  1. Deploy the application by running the command serverless. See: https://github.com/serverless-nextjs/serverless-next.js
  2. Go to https://<app_id>.cloudfront.net/api/<path_to_your_api_route>.

This will result in the 503 error above.

Screenshots/Code/Logs

CloudWatch Log Stack Trace

# serverless.yml
App:
  component: "@sls-next/serverless-component@1.18.0"

Versions

Additional context

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 24

Most upvoted comments

This may be related to these issues, but none of these solutions work for me:

https://github.com/serverless-nextjs/serverless-next.js/issues/819 https://github.com/serverless-nextjs/serverless-next.js/issues/935 https://github.com/serverless-nextjs/serverless-next.js/issues/181 https://github.com/serverless-nextjs/serverless-next.js/issues/222

I’ve tried increasing the lambda timeout, deleting node_modules and package-lock.json and reinstalling, upgrading all npm packages, using the latest serverless nextjs component.

@dphang thanks for coming back to me. After some more debugging, I had to remove webpack5, and that sorted out the issue. I may come back to test it again with webpack 5 and update you here in case I have more issues

Thanks Charles, hopefully we can get some visibility on this issue from the devs

So, Felix I’m experiencing the same issue and opened up another report. Your serverless.yml is super small so my guess is that you’re suffering from a misconfiguration error. But I have no advice beyond that since I’m suffering from the same.