Deploy your Node Js app on Aws Elastic Beanstalk
Tags: node, javascript, aws, cloud
Published: 2022-04-19 09:42:24


Today we will learn how to deploy our simple api on the elastic bean stalk service on AWS.


##### Prerequisite
Register on Aws [Guide here](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-prereqs.html)
Git installed [download](https://git-scm.com/downloads)
EB CLI installed [Installation Guide here](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install-advanced.html)

Check if <kbd>eb cli</kbd> and <kbd>git cli</kbd> is installed on your machine, run:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pth9dk0gsfry9w1dm7ah.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivoerujji1ru1w31ekp4.PNG)
  
---

##### Setup
`npm init -y` to initialize your app.

####
Click [here](https://expressjs.com/en/starter/hello-world.html) for a simple express starter `hello world` app.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tqbskjis8l963dcvc4j.PNG)
 
Install `express` package and dump this code in your `index.js` file
```javascript

const express = require('express')
const app = express()
const port = process.env.PORT|| 3000;

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
```

---

##### Let's write a start script to run our server in `package.json`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umdy7z3v2e06nrciyto1.PNG)

--- 


Create a `.gitignore` file and add this:
```javascript
node_modules/
.gitignore
.elasticbeanstalk/
```

##### Git setup on our project
* git init
* git add .
* git commit -m "initial commit"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtzwk53cpzrfa8nlgrn3.PNG)
 

---

#### Deploy on elastic beanstalk AWS instance
Let's look at how we can initialize our AWS instance.

Run eb init with flags

> eb init --platform node.js --region us-east-2

The above command creates the `.elasticbeanstalk folder`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izudrxmv441qq6wzd2a7.PNG)
 
---

##### .ebextension
After initializing your Eb, create a folder `.ebextensions` and a file `nodecommands.config`

We put our start command in the `nodecommands.config`

```
option_settings:
    aws:elasticbeanstalk:container:nodejs:
        NodeCommand: "npm start"
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6rytjilts7vi74aoskh.PNG)
 
---

#### Create the application environment (Deployment)

> eb create [environment-name]
> The <environment-name> is the name of your application, in this example I used `elasticbean` hence the command is [eb create elasticbean]

- with flags run: eb create --sample [your project folder name]

Note: avoid the use of `_` when naming your project folders

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yw9tutzm8puw507bpof.PNG)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t31yscvbuenbp6v13mko.PNG)
  
---

#### Deploy your application

> eb deploy
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/myfsy94m75hg8pa5chnp.PNG)
 

---

##### To open your eb app

> eb open
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4crw0r0cddahidngk0h.PNG)
 

#### more commands

eb logs
eb --help
eb setenv [VAR_NAME=VALUE] note: do not include the square brackets


##### To delete eb init project

> Go to the directory of your project (the directory where you originally ran the "eb init" command). Delete the . elasticbeanstalk directory. You can now run "eb init" again, and it will prompt you for your configuration information.


---

##### Conclusion
I hope this guide was helpful. Thanks for reading
![thanks](https://media.giphy.com/media/mVJ5xyiYkC3Vm/giphy.gif)


##### Resources
[Deploy Express app on aws](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html)