Home > OS >  504 timeout in aws EC2 when calling some external api url
504 timeout in aws EC2 when calling some external api url

Time:11-29

I have a following Next.js api route for testing purpose.

All 4 axois call work perfectly in localhost; But when in production (hosted by AWS EC2); The last 2 calls failed with a reason of 504 gateway time-out.

I had thought of nginx and AWS in-bound/out-bound setup, but if that's the case, the first two mock api shouldn't work as well.

I don't know why it happens. Or it has something to do with api protection from those website? But then why it work in localhost

import axios from "axios";
import { NextApiHandler } from "next";

export default const MockApi: NextApiHandler = async (req, res) => {
  try {
    // mock set
    // work in localhost and production
    const { data: testData } = await axios.get("https://jsonplaceholder.typicode.com/todos/1");
    console.log(testData);

    const { data: mockData } = await axios.get("https://reqres.in/api/users?page=2");
    console.log(mockData);

    // some real life api
    // work in localhost but failed in production with 504 gateway time-out
    const { data: mockData2 } = await axios.get("https://www.target.com.au/ws-api/v1/target/products/search?category=W95362");
    console.log(mockData2);

    const { data } = await axios.get("https://api.nasdaq.com/api/ipo/calendar");
    console.log(data);

    res.status(200).send({});
  } catch (err) {
    res.status(403).json(err);
  }
};

CodePudding user response:

After investigation, it seems like the problem has something to do with AWS internal policies, so that you are not able to use AWS services to abuse particular affiliated companies and some public APIs.

This answer could be incorrect if someone is able to sort the issue out.

  • Related