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:
Setup
npm init -y
to initialize your app.
Click here for a simple express starter hello world
app.
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}`)
})
Let's write a start script to run our server in package.json
Create a .gitignore
file and add this:
node_modules/
.gitignore
.elasticbeanstalk/
Git setup on our project
- git init
- git add .
- git commit -m "initial commit"
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
.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"
Create the application environment (Deployment)
eb create [environment-name]
The is the name of your application, in this example I usedelasticbean
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
Deploy your application
To open your eb app
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
Top comments (5)
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.
Thanks @radandevist
what to do if I make changes in my code and want to deploy the changes?
Waiting for your response.
Thanks
You can create a code pipeline, once you update from your code source like GitHub it will sync
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.