Deployment of node with Elastic Beanstalk (EB)
Setting up EB for the first time
Use the below abbreviated instructions and refer to the AWS docs as needed.
Python 2.7 or 3.4, and pip must be installed on your machine
- Test with
python --version
andpip --version
from CLI if you are not sure what is installed - If either are not installed, download from https://www.python.org/downloads/
- Make sure during install you select the option to add python to your PATH
Install EB CLI
pip install awsebcli
- confirm with
eb --version
Setting up the EB deployment
- In your CLI, go to the working directory for the project.
- Run
eb init
and follow the steps to completion. - Look at the new file
/.elasticbeanstalk/config.yml
and confirm that the value ofprofile
is eithernull
, or a value that will match the correct keys so you deploy into the correct account. If it isnull
then you will need to runaws configure
every time you deploy to EB. See this page for more info on profiles. - There are probably changes to
.gitignore
from the above, commit them now. EB deploys based on your latest git commit. - Run
eb create
and follow the steps, the process at the end will take about 10 minutes - you can watch progress in the CLI or open AWS Web Console and look at the same process on the Elastic Beanstalk page. - If the above fails with an error like "The instance profile aws-elasticbeanstalk-ec2-role associated with the environment does not exist." the easiest way to fix that is to go into the AWS web console, go to Elastic Beanstalk, and Create an Application. You only need to name it, select Web Server for platform, and accept the default Permissions. After that, go ahead and delete the application you just made on Web Console, you don't need it anymore, that is just the easiest way to get the correct permissions set. You can then run
eb create
again and it should complete.
Configuration
Once deployment is complete, on AWS Web Console select Elastic Beanstalk > your app > Configuration.
Instances
Change the Instance Type from t1.micro to m1.small, or whatever makes sense for the deployment. Don't leave it on micro.
Some instances are meant to work in VPC and others without, ask a Systems Engineer if you are not sure what to choose.
Software Configuration
- Turn gzip compression on, unless there is a reason it needs to be left off.
- Change Node Command to
npm start
(assuming that will start your application). - Add the Environment Property PORT=8081 (this is what ngninx is looking for).
- Add the Environment Property NODE_ENV=production (this tells Express and keystone to turn on some caching and not print stack traces).
- Add any custom configuration options like database connection strings. You can load any of these in node using
process.env.PROPERTY_NAME
.
Load Balancer
- To turn on HTTPS, under Load Balancer, turn on a secure port (443) and then pick a cert from the dropdown.
If you are setting your app to redirect HTTP to HTTPS on the box (meaning, in your app code, and not on the load balancer) make sure you are checking against the X-Forwarded-Proto header. In Express you can
app.set( "trust proxy", true )
and it will use that header in place of req.protocol. KeystoneJS works the same since it uses Express, but the option is set inkeystone.init( { "trust proxy": true } )
instead.
Debugging
View the Logs page and review any crashes. A crash is most likely to manifest as seeing an nginx 502 page in the browser.
Deploying updates
- If the profile in your
./elasticbeanstalk/config.yml
is set tonull
, confirm you are using the correct AWS account withaws configure
. - Confirm you have committed all the changes you want to deploy to git.
- Run
eb deploy
.
Shutting down the deployment
You probably will only do this if you were running a test or a project has ended, don't do this with anything you actually want to be available online
- Run
eb terminate
.