Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I safely run a Node.js server on port 80 with Amazon Elastic Beanstalk?

The following error is common for people trying to run a Node.js server on port 80.

Error: listen EACCES 0.0.0.0:80

I used to solve this on my Amazon EC2 server simply by using

sudo node app.js

Now I've learned not to use that method for security concerns. A good solution as explained in this answer is to use:

sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``

However I'm not sure how to implement either solution on an AWS Elastic Beanstalk instance, where I don't seem to have SSH access the way I did for the AWS EC2 server, and the only way I seem to have for running anything is in my package.json file like this:

{
  "scripts": {
    "start": "node init"
  }
}

So I have no idea how I would run other types of commands. How is this done?

like image 221
curiosity5678 Avatar asked Oct 16 '16 03:10

curiosity5678


People also ask

What port does Elastic Beanstalk use?

By default, Elastic Beanstalk configures the proxy to forward requests to your application on port 5000. You can override the default port by setting the PORT environment property to the port that your main application listens on.

Does Elastic Beanstalk do an NPM install?

By default, Elastic Beanstalk installs dependencies in production mode ( npm install --production ).

Why use Elastic Beanstalk instead of EC2?

Elastic Beanstalk is one layer of abstraction away from the EC2 layer. Elastic Beanstalk will setup an "environment" for you that can contain a number of EC2 instances, an optional database, as well as a few other AWS components such as a Elastic Load Balancer, Auto-Scaling Group, Security Group.


2 Answers

Beanstalk has a proxy build into it listening on port 80. Your Node.js app should only listen on process.env.PORT. Once it's done that, you'll be good to go.

like image 191
Brad Avatar answered Oct 24 '22 12:10

Brad


Elastic beanstalk will route requests to your instances (at port 80 by default) from its load balancer. So in case you want to expose a particular port, you can add an entry to the load balancer listeners with the load balancer port as your desired port and instance port as 80.
Hope this helps. Can elaborate further if needed.

like image 37
Shubhankar S Avatar answered Oct 24 '22 12:10

Shubhankar S