Search code examples
node.jsamazon-web-servicesnginxmulteramazon-elastic-beanstalk

AWS Elastic Beanstalk / NodeJS / Nginx Error: Keep getting 502 bad gateway


I have deployed my web application for several times, and it went well.

but since yesterday deployment, I'm keep getting 502 Bad Gateway, whenever I try to get my website.

I terminated my app from AWS elastic beanstalk and recreated, but still getting 502 bad gateway.

here's my nodejs.log:

Error: ENOENT: no such file or directory, open '/var/app/current/config/awsconfig.json'
    at Error (native)
    at Object.fs.openSync (fs.js:642:18)
    at Object.fs.readFileSync (fs.js:510:33)
    at Object.readFileSync (/var/app/current/node_modules/aws-sdk/lib/util.js:97:26)
    at Config.loadFromPath (/var/app/current/node_modules/aws-sdk/lib/config.js:409:39)
    at Object.<anonymous> (/var/app/current/server.js:14:12)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
fs.js:642
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);

and this is the part in my server.js that related to nodejs error log:

const express = require("express");
const path = require("path");
const fs = require("fs");
const template = require("./public/template");
const bodyParser = require("body-parser");
const _ = require("lodash");
const uuidv1 = require("uuid/v1");

const AWS = require("aws-sdk");
const multer = require("multer");
const multerS3 = require("multer-s3");

// Image Upload
AWS.config.loadFromPath(path.join(__dirname, "/config/awsconfig.json"));
const s3 = new AWS.S3();
const upload = multer({
  storage: multerS3({
    s3,
    bucket: "hackingdeal",
    key: (req, file, cb) => {
      cb(null, new Date().valueOf() + path.extname(file.originalname));
    },
    acl: "public-read-write"
  })
});

FYI, "server.js" file is located in root dir("./server.js") and awsconfig.json is located in "./config/awsconfig.json"

I made a .gitignore to ignore "config/awsconfig.json". I also created a .ebignore but I didn't contained "config/awsconfig.json" so my elastic beanstalk could find and read "config/awsconfig.json" file.

This is my nginx/error.log:

2018/10/20 23:32:05 [error] 8103#0: *96 connect() failed (111: Connection refused) while connecting to upstream, client: 110.12.50.169, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "hackingdeal-env.4wgwkb9tdv.ap-northeast-2.elasticbeanstalk.com"
2018/10/20 23:32:05 [error] 8103#0: *96 connect() failed (111: Connection refused) while connecting to upstream, client: 110.12.50.169, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "hackingdeal-env.4wgwkb9tdv.ap-northeast-2.elasticbeanstalk.com", referrer: "http://hackingdeal-env.4wgwkb9tdv.ap-northeast-2.elasticbeanstalk.com/"
2018/10/20 23:48:00 [error] 12936#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 110.12.50.169, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "hackingdeal.com"
2018/10/20 23:48:01 [error] 12936#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 110.12.50.169, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "hackingdeal.com", referrer: "http://hackingdeal.com/"

hackingdeal.com is my website. I connected that domain to my Elastic Beanstalk by using Route53 service, making alias.

The thing is, My app runs successfully on localhost.

My app run successfully right after when I deployed "server.js" with Image upload statements(using AWS S3 and multer/multerS3 thing: See upper code block"

But from yesterday, it keep returns 502 bad gateway.

I'm totally lost, I don't know where to start, or where to find my problem. please help..


Solution

  • I'd have to read the logs to understand better your problem, but it seems that you try to load your configuration from the file /config/awsconfig.json in here

    // Image Upload
    AWS.config.loadFromPath(path.join(__dirname, "/config/awsconfig.json"));
    const s3 = new AWS.S3();
    const upload = multer({
      storage: multerS3({
        s3,
        bucket: "hackingdeal",
        key: (req, file, cb) => {
          cb(null, new Date().valueOf() + path.extname(file.originalname));
        },
        acl: "public-read-write"
      })
    });
    

    So, you must upload this file to your ELB. In fact, that's the problem logged in nodejs.log