DEV Community

Cover image for Deploy your Node Js app on Aws Elastic Beanstalk
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

Deploy your Node Js app on Aws Elastic Beanstalk

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

Prerequisite

Register on Aws Guide here
Git installed download
EB CLI installed Installation Guide here

Check if eb cli and git cli is installed on your machine, run:
Image description

Image description


Setup

npm init -y to initialize your app.

Click here for a simple express starter hello world app.

Image description

Install express package and dump this code in your index.js file


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}`)
})
Enter fullscreen mode Exit fullscreen mode

Let's write a start script to run our server in package.json

Image description


Create a .gitignore file and add this:

node_modules/
.gitignore
.elasticbeanstalk/
Enter fullscreen mode Exit fullscreen mode
Git setup on our project
  • git init
  • git add .
  • git commit -m "initial commit"

Image description


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


.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"
Enter fullscreen mode Exit fullscreen mode

Image description


Create the application environment (Deployment)

eb create [environment-name]
The 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
Image description


Deploy your application

eb deploy
Image description


To open your eb app

eb open
Image description

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

Resources

Deploy Express app on aws

Top comments (5)

Collapse
 
radandevist profile image
Andrianarisoa Daniel

Hey that's a nice gif you put on at the end of the post, I like it. It made me laugh.
Nice post too by the way.

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

Thanks @radandevist

Collapse
 
rahuldipu profile image
Rahul Kumar

what to do if I make changes in my code and want to deploy the changes?

Waiting for your response.

Thanks

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

You can create a code pipeline, once you update from your code source like GitHub it will sync

Collapse
 
rahuldipu profile image
Rahul Kumar

Thanks. I have followed your blog and everything is working fine but I am getting one warning. can you please help me with it?

Unable to assume role "arn:aws:iam::************:role/aws-elasticbeanstalk-service-role". Verify that the role exists and is configured correctly.