question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

503 Error when invoking NextJS API Route from Client

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
felixs8696commented, Mar 10, 2021

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.

1reaction
KrustyCcommented, May 25, 2021

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

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.
Read more >
503 error when fetching an internal NextJS api with query ...
am trying to fetch data from a JSON file through NextJS internal API on the client side. But it is always throwing 503...
Read more >
node.js - 503 Service Unavailable after deploy NextJS app
(on my local node -v show me 14.18.1 , but on cPanel I don't have this version); run npm install with success. then...
Read more >
API Routes: Introduction - Next.js
They are server-side only bundles and won't increase your client-side bundle size. For example, the following API route pages/api/user.js returns a json ...
Read more >
How to Fix 503 Service Unavailable Error: 7 Steps That Work
That being said, improper firewall configuration can detect false positives and block connections from a specific client. For example, your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found