502 Bad Gateway when hosting a Meteor app on AWS Elastic Beanstalk with Meteor-up

690 views Asked by At

I have a Meteor app I've been trying to deploy to AWS with mup-aws-beanstalk.

Here is my repository This is a basic "Hello world" equivalent of MongoDB, Meteor, React, Node.js app. How to install

This works perfectly fine localy, and runs on http://localhost:3000/. When I try to deploy to AWS with the meteor-up mup-aws-beanstalk plugin, it deploys, but I get a 502 Bad Gateway error.

I'm pretty new to this but I did some research and checked the logs.

Checking the logs, I see that the start script isnt working properly

> [email protected] start /var/app/current
> bash ./start.sh


┌──────────────────────────────────────────────────┐
│             npm update check failed              │
│       Try running with sudo or get access        │
│       to the local update config store via       │
│ sudo chown -R $USER:$(id -gn $USER) /tmp/.config │
└──────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────┐
│             npm update check failed              │
│       Try running with sudo or get access        │
│       to the local update config store via       │
│ sudo chown -R $USER:$(id -gn $USER) /tmp/.config │
└──────────────────────────────────────────────────┘
Node version
v12.16.1
Npm version
6.14.0
=> Starting health check server
=> Starting App
/var/app/current/programs/server/node_modules/fibers/fibers.js:90
                    return fn.apply(this, arguments);
                              ^

Error: $ROOT_URL, if specified, must be an URL
    at packages/meteor.js:1328:13
    at packages/meteor.js:1343:4
    at packages/meteor.js:1508:3
    at /var/app/current/programs/server/boot.js:401:38
    at Array.forEach (<anonymous>)
    at /var/app/current/programs/server/boot.js:226:21
    at /var/app/current/programs/server/boot.js:464:7
    at Function.run (/var/app/current/programs/server/profile.js:280:14)
    at /var/app/current/programs/server/boot.js:463:13
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `bash ./start.sh`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

I checked the above problem and other StackOverflow questions mention to make the URL start with http or https, which my does.

Then there is also an error from nginx, there are these lines that keep repeating. I am not sure if these two are related. This post describes the problem might be because the app isn't running on the server/port combination

HOWEVER, this post states that it might be because Elastic Beanstalk is reading the wrong file first, and therefore not opening up the ports in the first place?

I am not sure how or where to change the port numbers, or if this is a problem with npm.

-------------------------------------
/var/log/nginx/error.log
-------------------------------------
2020/07/13 04:02:02 [error] 4632#0: *148508 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.21.16, server: , request: "GET /aws-health-check-3984729847289743128904723 HTTP/1.1", upstream: "http://127.0.0.1:8039/aws-health-check-3984729847289743128904723", host: "172.31.46.135"
2020/07/13 04:02:06 [error] 4632#0: *148510 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.46.174, server: , request: "GET /aws-health-check-3984729847289743128904723 HTTP/1.1", upstream: "http://127.0.0.1:8039/aws-health-check-3984729847289743128904723", host: "172.31.46.135"
2020/07/13 04:02:17 [error] 4632#0: *148512 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.21.16, server: , request: "GET /aws-health-check-3984729847289743128904723 HTTP/1.1", upstream: "http://127.0.0.1:8039/aws-health-check-3984729847289743128904723", host: "172.31.46.135"

This is my deployment output

PS G:\GitFolder\meteor-example-deploy-aws\.deploy> mup deploy
=> Setting up
=> Ensuring IAM Roles and Instance Profiles are setup
Building App Bundle Locally

WARNING: The output directory is under your source tree.
         Your generated files may get interpreted as source code!
         Consider building into a different directory instead
         meteor build ../output

app/node_modules/semantic-ui-css/semantic.css: warn: There are some @import rules in the middle of a file. This might be a bug, as imports are only valid at the beginning of a file.
Browserslist: caniuse-lite is outdated. Please run next command `npm update`

Unable to resolve some modules:

  "@babel/runtime/helpers/createSuper" in /G/GitFolder/meteor-example-deploy-aws/app/imports/ui/layouts/App.jsx (web.browser.legacy)

If you notice problems related to these missing modules, consider running:

  meteor npm install --save @babel/runtime

=> Archiving Bundle
  10% Archived
  20% Archived
  30% Archived
  40% Archived
  50% Archived
  60% Archived
  70% Archived
  80% Archived
  90% Archived
  100% Archived
=> Uploading bundle
  Uploaded 11%
  Uploaded 23%
  Uploaded 35%
  Uploaded 46%
  Uploaded 58%
  Uploaded 70%
  Uploaded 81%
  Uploaded 93%
  Uploaded 100%
  Finishing upload. This could take a couple minutes
=> Creating Version
=> Configuring Beanstalk Environment
  Updated Environment
=> Waiting for Beanstalk Environment to finish updating
  Env Event: Updating environment mup-env-meteor-example-deploy-aws's configuration settings.
  Env Event: Rolling with Additional Batch deployment policy enabled. Launching  a new batch of 1 additional instance(s).
  Env Event: Batch 1: 1 EC2 instance(s) [i-0b017a1b7f1c7151a] launched. Deploying application version.
  Env Event: Environment health has transitioned from Severe to Degraded. 100.0 % of the requests are failing with HTTP 5xx. ELB processes are not healthy on 1 out of 2 instances. Configuration update in progress on 1 instance. 0 out of 2 instances completed (running for 2 minutes). ELB health is failing or not available for 1 out of 2 instances. Impaired services on 1 out of 2 instances.
  Env Event: Added instance [i-0b017a1b7f1c7151a] to your environment.
  Env Event: Failed to run npm install. Snapshot logs for more details.
  Env Event: Retrieving logs prior to instance(s) termination. Logs will be available for an hour in the environment management console and at elasticbeanstalk-us-east-1-966889535256/resources/environments/logs/bundle/e-kvnxyajrem.
  Env Event: During an aborted deployment, some instances may have deployed the new application version. To ensure all instances are running the same version, re-deploy the appropriate application version.
  Env Event: Failed to deploy configuration.
  Env Event: Terminating excess instance(s): [i-0b017a1b7f1c7151a].
  Env Event: Command execution completed on all instances successfully.
  Env Event: [Instance: i-0b017a1b7f1c7151a] Successfully finished bundling 16 log(s)
=> Deploying new version
=> Waiting for Beanstalk Environment to finish updating
  Env Event: Environment health has transitioned from Degraded to Severe. 100.0 % of the requests are failing with HTTP 5xx. Command failed on 1 out of 2 instances. Incorrect application version found on 1 out of 2 instances. Expected version "5" (deployment 10). ELB processes are not healthy on all instances. Application update in progress (running for 42 seconds). ELB health is failing or not available for all instances. Impaired services on 1 out of 2 instances.
  Env Event: Rolling with Additional Batch deployment policy enabled. Launching  a new batch of 1 additional instance(s).
  Env Event: Removed instance [i-0b017a1b7f1c7151a] from your environment.
  Env Event: Added instance [i-0733205831e115a28] to your environment.
  Env Event: Batch 1: 1 EC2 instance(s) [i-0733205831e115a28] launched. Deploying application version '5'.
  Env Event: Unsuccessful command execution on instance id(s) 'i-0733205831e115a28'. Aborting the operation.
  Env Event: Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].
  Env Event: [Instance: i-0733205831e115a28] Command failed on instance. Return code: 127 Output: (TRUNCATED)...
/opt/elasticbeanstalk/hooks/appdeploy/pre/45node.sh: line 12: nvm: command not found
/opt/elasticbeanstalk/hooks/appdeploy/pre/45node.sh: line 13: nvm: command not found
/opt/elasticbeanstalk/hooks/appdeploy/pre/45node.sh: line 14: npm: command not found.
Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/45node.sh failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.
  Env Event: Retrieving logs prior to instance(s) termination. Logs will be available for an hour in the environment management console and at elasticbeanstalk-us-east-1-966889535256/resources/environments/logs/bundle/e-kvnxyajrem.
  Env Event: Excess instance(s) terminated.
  Env Event: Terminating excess instance(s): [i-0733205831e115a28].
  Env Event: Command execution completed on all instances successfully.
  Env Event: [Instance: i-0733205831e115a28] Successfully finished bundling 15 log(s)
  Env Event: Environment health has transitioned from Severe to Degraded. 100.0 % of the requests are failing with HTTP 5xx. Command failed on 1 out of 2 instances. Incorrect application version found on 1 out of 2 instances. Expected version "1" (deployment 1). ELB processes are not healthy on 1 out of 2 instances. Application update is aborting. 1 out of 2 instances completed (running for 4 minutes). ELB health is failing or not available for 1 out of 2 instances. Impaired services on 1 out of 2 instances.
App is running at mup-env-meteor-example-deploy-aws.eba-cah6ppkm.us-east-1.elasticbeanstalk.com
=> Finding old versions
=> Removing old versions
=> Updating Beanstalk SSL Config

and this everything in my mup.js file

  app: {
    // Tells mup that the AWS Beanstalk plugin will manage the app
    type: 'aws-beanstalk',
    name: 'meteor-example-deploy-aws',
    path: 'G:/GitFolder/meteor-example-deploy-aws/app',
    env: {
      ROOT_URL: 'http://mup-env-meteor-example-deploy-aws.eba-cah6ppkm.us-east-1.elasticbeanstalk.com/',
      MONGO_URL: 'mongodb://MYUSERNAME:MYPASSREDACTED@docdb-2020-07-06-07-57-38.cluster-c9vs8fwnppko.us-east-1.docdb.amazonaws.com:27017/?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'
    },
    auth: {
      id: 'AKIA6C...',
      secret: 'xCBpL....'
    },
    minInstances: 1
  },
  plugins: ['mup-aws-beanstalk']
};
1

There are 1 answers

0
mcnk On

If you are still facing this issue because of browser-policy try this.

1- Modify you mup.js with this

 proxy: {
    domains: 'domain.com',
    shared: {
      nginxConfig: './nginx.conf',
    },
    ssl: {
      // Enable Let's Encrypt
      forceSSL: true,
      letsEncryptEmail: '[email protected]',
    },
  },

2- in ngninx.conf place this

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_buffer_size          128k;
    proxy_buffers              4 256k;
    proxy_busy_buffers_size    256k;

3- run mup setup then mup proxy reconfig-shared then mup deploy