Deploy to Elastic Beanstalk showed 502 error

Viewed 208

I have tried many times deploying a Pug and NodeJS application to Elastic Beanstalk without success. Every time, I get a 502 error. On checking the error log, I see that error.log shows perhaps EBS might be using a different port to the application but I can confirm that both use port 8081.

I have looked up other similar questions but those answers can't resolve this issue.

What could be the issue here?

Thank you.

-------------------------------------
/var/log/nginx/error.log
-------------------------------------
2020/12/05 06:20:56 [error] 3735#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:21:11 [error] 3735#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:21:26 [error] 3735#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:21:41 [error] 3735#0: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:21:49 [error] 3735#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.84.55, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:21:56 [error] 3735#0: *11 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:22:04 [error] 3735#0: *13 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.84.55, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:22:11 [error] 3735#0: *15 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:22:19 [error] 3735#0: *17 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.84.55, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:22:26 [error] 3735#0: *19 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:22:34 [error] 3735#0: *21 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.84.55, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:22:41 [error] 3735#0: *23 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:22:49 [error] 3735#0: *25 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.84.55, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:22:56 [error] 3735#0: *27 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"
2020/12/05 06:23:03 [error] 3735#0: *29 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "app17-env.eba-wxcw2xyp.us-east-1.elasticbeanstalk.com"
2020/12/05 06:23:03 [error] 3735#0: *29 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET /robots.txt HTTP/1.1", upstream: "http://127.0.0.1:8081/robots.txt", host: "app17-env.eba-wxcw2xyp.us-east-1.elasticbeanstalk.com"
2020/12/05 06:23:04 [error] 3735#0: *29 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.42.155, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "app17-env.eba-wxcw2xyp.us-east-1.elasticbeanstalk.com", referrer: "http://app17-env.eba-wxcw2xyp.us-east-1.elasticbeanstalk.com/"
2020/12/05 06:23:04 [error] 3735#0: *33 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.84.55, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "172.31.84.136"

-------------------------------------
/var/log/eb-activity.log
-------------------------------------
[2020-12-05T06:20:48.267Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/01stop_xray.sh] : Completed activity. Result:
  Executing: if ( initctl status xray | grep start ); then initctl stop xray; fi
  xray start/running, process 2250
  xray stop/waiting
[2020-12-05T06:20:48.267Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/02start_xray.sh] : Starting activity...
[2020-12-05T06:20:48.378Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/02start_xray.sh] : Completed activity.
[2020-12-05T06:20:48.378Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/10stop.sh] : Starting activity...
[2020-12-05T06:20:49.867Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/10stop.sh] : Completed activity. Result:
  + /opt/elasticbeanstalk/containerfiles/ebnode.py --action stop-all
  status: Unknown job: nodejs
  status: Unknown job: nodejs
  status: Unknown job: nginx
  status: Unknown job: nginx
  status: Unknown job: httpd
  status: Unknown job: httpd
  Did not find to find status of init job. Assuming stopped.
  Did not find to find status of init job. Assuming stopped.
  Did not find to find status of init job. Assuming stopped.
  Did not find to find status of init job. Assuming stopped.
  Did not find to find status of init job. Assuming stopped.
  Did not find to find status of init job. Assuming stopped.
[2020-12-05T06:20:49.867Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/20clean.sh] : Starting activity...
[2020-12-05T06:20:49.966Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/20clean.sh] : Completed activity. Result:
  ++ /opt/elasticbeanstalk/bin/get-config container -k app_base_dir
  + EB_APP_BASE_DIR=/var/app
  + rm -rf /var/app
[2020-12-05T06:20:49.966Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/30app_deploy.sh] : Starting activity...
[2020-12-05T06:20:50.410Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/30app_deploy.sh] : Completed activity. Result:
  ++ /opt/elasticbeanstalk/bin/get-config container -k app_base_dir
  + EB_APP_BASE_DIR=/var/app
  ++ /opt/elasticbeanstalk/bin/get-config container -k app_staging_dir
  + EB_APP_STAGING_DIR=/tmp/deployment/application
  ++ /opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir
  + EB_APP_DEPLOY_DIR=/var/app/current
  ++ /opt/elasticbeanstalk/bin/get-config container -k app_user
  + EB_APP_USER=nodejs
  + mkdir /var/app
  + mv /tmp/deployment/application /var/app/current
  + chown -R nodejs:nodejs /var/app/current
[2020-12-05T06:20:50.410Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/40config_deploy.sh] : Starting activity...
[2020-12-05T06:20:50.524Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/40config_deploy.sh] : Completed activity. Result:
  ++ /opt/elasticbeanstalk/bin/get-config container -k config_staging_dir
  + EB_CONFIG_STAGING_DIR=/tmp/deployment/config
  ++ ls /tmp/deployment/config
  + for i in '$(ls $EB_CONFIG_STAGING_DIR)'
  ++ sed -e 's/#/\//g'
  ++ echo '#etc#init#nginx.conf'
  + FILE_NAME=/etc/init/nginx.conf
  + /bin/cp /tmp/deployment/config/#etc#init#nginx.conf /etc/init/nginx.conf
  + for i in '$(ls $EB_CONFIG_STAGING_DIR)'
  ++ sed -e 's/#/\//g'
  ++ echo '#etc#init#nodejs.conf'
  + FILE_NAME=/etc/init/nodejs.conf
  + /bin/cp /tmp/deployment/config/#etc#init#nodejs.conf /etc/init/nodejs.conf
  + for i in '$(ls $EB_CONFIG_STAGING_DIR)'
  ++ sed -e 's/#/\//g'
  ++ echo '#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf'
  + FILE_NAME=/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
  + /bin/cp /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
  + for i in '$(ls $EB_CONFIG_STAGING_DIR)'
  ++ sed -e 's/#/\//g'
  ++ echo '#etc#nginx#nginx.conf'
  + FILE_NAME=/etc/nginx/nginx.conf
  + /bin/cp /tmp/deployment/config/#etc#nginx#nginx.conf /etc/nginx/nginx.conf
[2020-12-05T06:20:50.524Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/50start.sh] : Starting activity...
[2020-12-05T06:20:52.017Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/50start.sh] : Completed activity. Result:
  + /opt/elasticbeanstalk/containerfiles/ebnode.py --action start-all
  nodejs start/running, process 3707
  nginx start/running, process 3715
[2020-12-05T06:20:52.017Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/60monitor_pids.sh] : Starting activity...
[2020-12-05T06:20:52.023Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/60monitor_pids.sh] : Completed activity. Result:
  + '[' -d /etc/healthd ']'
[2020-12-05T06:20:52.023Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/70restart_healthd.sh] : Starting activity...
[2020-12-05T06:20:52.025Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook/70restart_healthd.sh] : Completed activity. Result:
  + '[' -d /etc/healthd ']'
[2020-12-05T06:20:52.025Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployEnactHook] : Completed activity. Result:
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/appdeploy/enact.
[2020-12-05T06:20:52.025Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployPostHook] : Starting activity...
[2020-12-05T06:20:52.025Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/AppDeployPostHook] : Completed activity. Result:
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/appdeploy/post.
[2020-12-05T06:20:52.026Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/PostInitHook] : Starting activity...
[2020-12-05T06:20:52.026Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1/PostInitHook] : Completed activity. Result:
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/postinit.
[2020-12-05T06:20:52.026Z] INFO  [3183]  - [Application deployment app17-source@1/StartupStage1] : Completed activity. Result:
  Application deployment - Command CMD-Startup stage 1 completed
[2020-12-05T06:20:52.026Z] INFO  [3183]  - [Application deployment app17-source@1/AddonsAfter] : Starting activity...
[2020-12-05T06:20:52.026Z] INFO  [3183]  - [Application deployment app17-source@1/AddonsAfter/ConfigLogRotation] : Starting activity...
[2020-12-05T06:20:52.026Z] INFO  [3183]  - [Application deployment app17-source@1/AddonsAfter/ConfigLogRotation/10-config.sh] : Starting activity...
[2020-12-05T06:20:52.223Z] INFO  [3183]  - [Application deployment app17-source@1/AddonsAfter/ConfigLogRotation/10-config.sh] : Completed activity. Result:
  Disabled forced hourly log rotation.
[2020-12-05T06:20:52.223Z] INFO  [3183]  - [Application deployment app17-source@1/AddonsAfter/ConfigLogRotation] : Completed activity. Result:
  Successfully execute hooks in directory /opt/elasticbeanstalk/addons/logpublish/hooks/config.
[2020-12-05T06:20:52.223Z] INFO  [3183]  - [Application deployment app17-source@1/AddonsAfter] : Completed activity.
[2020-12-05T06:20:52.224Z] INFO  [3183]  - [Application deployment app17-source@1] : Completed activity. Result:
  Application deployment - Command CMD-Startup succeeded
[2020-12-05T06:23:10.853Z] INFO  [4196]  - [CMD-TailLogs] : Starting activity...
[2020-12-05T06:23:10.853Z] INFO  [4196]  - [CMD-TailLogs/AddonsBefore] : Starting activity...
[2020-12-05T06:23:10.853Z] INFO  [4196]  - [CMD-TailLogs/AddonsBefore] : Completed activity.
[2020-12-05T06:23:10.853Z] INFO  [4196]  - [CMD-TailLogs/TailLogs] : Starting activity...
[2020-12-05T06:23:10.853Z] INFO  [4196]  - [CMD-TailLogs/TailLogs/TailLogs] : Starting activity...

1 Answers

You need to add a configuration for PORT in the BeanStalk environment.

  1. Go to Configuration.
  2. Go to Security.
  3. Add PORT and its value in the configuration.

It is not mentioned everywhere but to run your node application you need to pass PORT as an env variable.

Related