How to deploy your Node.js apps to AWS Elastic Beanstalk(without EB Cli)
Let’s face it — AWS is a convoluted web of so many services and can be really difficult to wrap one’s head around. That’s certainly how I felt, the first time I deployed a Node.js app to the service. PSA I love Clis but definitely not a fan of the Elastic Beanstalk Cli.
Compared to Heroku or DigitalOcean, AWS Elastic Beanstalk(ikr Such an interesting name!) can be a little difficult to wrap your head around but it’s probably one of the easiest AWS services to use. Elastic Beanstalk is a similar service to Heroku and DigitalOcean i.e it provisions a server for you without you having to do any of the complex DevOps work yourself.
Prerequisites
1. An AWS Account
2. A functioning Node.js App in a GitHub repo
3. A GitHub account(duhh!)
Let’s get started!
First things, first, you need an AWS account. It’s pretty complex to setup. I would advice you follow freecodecamp’s AWS Cloud Certified Associate video on setting up an AWS account.
Create an Elastic Beanstalk Application and environment
- Click Services > Elastic Beanstalk > Create application
- Enter an Application name
- For the platform, Select Node.js and leave the other fields as the default.
- Click next and wait for the environment to be done setting up. This may take up to 2minutes.
You’d get a screen like below once it’s done.
Create a Code pipeline to automatically deploy changes
- Your node app should be setup and already running perfectly on localhost. My app holds a list of blogs and is an app I’m building following the fullstack course at fullstackopen.com. I’m using MongoDB Atlas for my database.
Another thing to remember is that for your ports, you need to have them set up as
const PORT = process.env.PORT || 3001
This ensures that whatever port is being used by the server AWS gives us, is picked up by our App. - In your AWS console, click on Services(see the menu) > Code pipeline. We will be hooking up our GitHub to AWS so that each time we push updates, our App is automatically deployed to AWS.
- Click Create Pipeline. Fill in the pipeline name. The role name gets filled out automatically. Then select new service role as your service role. Click next.
- Select GitHub as source provider. Click connect to GitHub to hook up your GitHub account to AWS. Select the repo your code is in and the branch. Skip the build stage and confirm.
- Enter your application name that you created in the Elastic Beanstalk console and select the environment name(automatically generated from your app name) from the list.
6. Click next > Create pipeline.
7. And that’s it. We wait for our App to deploy. Hopefully you get green screens like below.
8. If anything goes wrong, you can check the environment logs.
9. To view your Node.js application: Services > Elastic Beanstalk > Click on the App’s name under environment name. The link will be displayed under the App’s name. In my case under NodejsBlog-env. (See Image in Point 4 of the 1st section)
10. You can preview my app here.
Hope this helps! Leave a clap if it does!
BONUS: Setting environment variables
You probably do not want to use the dotenv npm package in production as I had issues with that. Here’s how to set your env variables in AWS EB.
- Head to Environment > Select your application > Click Configuration
- Beside software, click edit.
- Scroll down to Environment properties.
4. Click Apply