Node js and MongoDB app on AWS - node.js

What are the steps for deploying a Node js app, with express.js framework and MongoDB as database on AWS?
This is my first such activity and I am not able to find any reliable source over the net.

Deploy nodejs-express-mongoDB backend on AWS EC2 Ubuntu(16.04)
Back-end is a private repo on Gitlab.
Steps:
1: Create EC2 Ubuntu server on AWS,
create EC2 instance
2: Connect personal computer with AWS server - use to control cloud Ubuntu server on personal computer,
Connecting Linux Instance from Windows Using PuTTY
3: Clone repo from Gitlab to Ubuntu server,
Configure ssh key on linux server
Clone gitlab repo to linux server
4: Install Nodejs and MongoDB on Ubuntu server,
Install Nodejs
Install MongoDB
Install MongoDB
5: Launch MongoDB and run Nodejs to start node server,
6: Test back-end server using Postman or Browser.
Other useful linkYouTube

AWS has a full documentation for this on their website. These documentations are extremely thorough and you should read these first.
MongoDB on AWS
Node.js on AWS
Express.js on AWS
Here's a tutorial from 2015.

Related

how to re deploy a node app on Amazon EC2

I deployed a node js app on AWS EC2 amazon linux 2 so now I'm trying to update my project how can I push new changes of node app to EC2
The simplest way I've found for EC2 is to SSH to the machine, then do a git pull from your repo, onto the machine.

How to connect local Postgres database not synchronizing to Heroku production database

I have an application deployed on Heroku but for some reason when I create the database locally it does not get applied to the actual database on heroku and I'm not sure why this is happening.
I've install Postgres locally via the web app and I also installed using Brew Install Postgres.

something confused with docker

I'm trying to use docker for my project these days, but I'm confused with some problem with it.
My project created by nodejs + mongodb + nginx, now I want to publish my project using docker. I don't know whether I should use a node image + a mongodb image + a nginx image and then link them together or I should use a basic image like centos and install node,mongo,nginx then run my server.
And another question, If I try the second way, in my dockerfile I run node server, it need to connect the mongodb use a correct user, but the mongodb doesn't have user authorization config yet when node server runs, so the node server can't run, so how to create a user in Docker file?

Nodejs and Meteor on OpenShift

I have a mobile (Ionic 2) chat application with the following implementation, that uses nodejs-0.10 and MongoDB 3.2.7 and Meteor 1.4.1.1. It works perfectly on my localhost.
Now I need to deploy it to an OpenShift server. I have followed the following steps and created a server on OpenShift with Meteor. It is connecting with Git, and I can push my code to the server.
I am pretty sure the Meteor server is running on OpenShift, because I saw something to that effect on the startup logs (via ssh). However, I am not sure how to connect to the Meteor server to test it.
The domain is nodejs-easyjobs.rhcloud.com (54.208.77.250) on the OpenShift server, I can ping this successfully.
I am using Ionic 2 to build the mobile app. I am currently just running on Android, but plan to add iOS and Windows.
I am following this tutorial, and it runs on localhost. However, I am not sure where this is configured. I guess localhost is default, and you need to change it to a different host if needs be.
If I check my OpenShift server (nodejs-easyjobs.rhcloud.com) via ssh, I can see that the contents of the Meteor bundle directory is on the server. So Git is pushing the code.
The part I don't understand is:
Do I need to configure Meteor differently for being on the OpenShift
Server? Or do I just leave it as is?
Where do I configure the client
(Ionic 2) to point to the OpenShift server?
Question:
My question is, how do I configure my mobile app (Ionic 2) to connect to the OpenShift Meteor Server?
UPDATE:
My dir structure:
openshift-appname
.../ionic-apps/nodejs/bundle/server/...
meteorapp
.../ionic-aps/myIonicApp/api/.meteor/...
.../ionic-aps/myIonicApp/api/server/...
The contents of the .../ionic-apps/nodejs is cloned on the OpenShift server with Git.
I followed this tutorial to deploy the Meteor bundle to OpenShift. `
So I used the following to build my Meteor app:
> cd .../ionic-aps/myIonicApp/
> meteor build .../ionic-apps/nodejs --directory --server-only
As far as I can see, I may be wrong, but the Meteor Server is all correct and running on OpenShift. I need to know how I can get my Ionic2 app to access it.
Step1: Find the access point to the Meteor server using the OpenShift
IP. i.e. nodejs-easyjobs.rhcloud.com (54.208.77.250).
Step2: Configure my Ionic2 client to use this.

recommended way to install mongodb on elastic beanstalk

I have already taken a look at How to install mongodb in Elastic Beanstalk? dated 2014, which no longer works. as well as https://docs.mongodb.org/ecosystem/platforms/amazon-ec2/#manually-deploy-mongodb-on-ec2
I have set up a new elastic beanstalk environment running on node.js with 1 ec2 micro instance '64bit Amazon Linux 2016.03 v2.1.0 running Node.js'
I have already tried using ssh to connect into my instance and install the mongodb packages using yum command:
$ sudo yum install -y mongodb-org-server mongodb-org-shell mongodb-org-tools
and received this call back:
Loaded plugins: priorities, update-motd, upgrade-helper
No package mongodb-org-server available.
No package mongodb-org-shell available.
No package mongodb-org-tools available.
Error: Nothing to do
When I first ssh 'd into my instance, I received this error warning:
This EC2 instance is managed by AWS Elastic Beanstalk. Changes made via SSH
WILL BE LOST if the instance is replaced by auto-scaling. For more information
on customizing your Elastic Beanstalk environment, see our documentation here:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html
Currently my environment is set up as a single instance environment, to save on costs. However, in the future I will upgrade to an auto-scaling environment.
Because of this, I am asking is it recommendable to make any changes via ssh in ec2, or should I only be using EB CLI?
I have both EC2 and EB CLI installed locally, however I have never used EB CLI before. If I should be using EB, does anyone have a recommended way to install mongodb?
In case anyone is looking for an answer, here is the advice I received from aws business support.
All code deployed to Elastic Beanstalk needs to be "stateless" I.E. Never make changes directly to a running beanstalk instance using SSH or FTP.... As this will cause inconsistencies and or data lose!
- Elastic Beanstalk is not designed for application that are not stateless.
The environment is designed to scale up and down pending on your Network / CPU load and build new instances from a base AMI. If an instance has issues or the underlying hardware, Elastic Beanstalk will terminate these running instances and replace with new instances. Hence, why no code modification must be applied or done "directly" to an existing instance as new instances will not be aware of these direct changes. ALL changes / code needs to be either uploaded to Elastic Beanstalk console or the CLI tools and the pushed to all the running instances.
More information on Elastic Beanstalk design concepts can be read at the following link
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/concepts.concepts.design.html
Suggested Solution:
With the above in mind, if using MongoDB to store application data our recommendation would be to DE-couple the MongoDB environment from your Node.js application.
I.E Create a MongoDB Server outside of Elastic Beanstalk, example launching MongoDB directly on a EC2 instance and have your Elastic Beanstalk Node.js application connect to MongoDB Server using connection settings in your app.
-Creating MongoDB
Below is some example links that may be of use for your scenario for creating a MongoDB Server.
Deploy MongoDB on EC2,
https://docs.mongodb.org/ecosystem/platforms/amazon-ec2/
MongoDB node client
https://docs.mongodb.org/getting-started/node/client/
MongoDB on the AWS Cloud quick start guide
http://docs.aws.amazon.com/quickstart/latest/mongodb/architecture.html
-Adding environment variables to Elastic Beanstalk to reference your MongoDB server
Once you have created your MongoDB Server you can pass the needed connection settings to your Elastic Beanstalk environment using environment variables.
Example using .ebextensions .config which you can add Mongo URL / ports / users etc..
option_settings:
- option_name: MONGO_DB_URL
value: "Your MongoDB EC2 internal IP address"
Information on how to use environment properties and read them from within your application can be seen below.
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs.container.html#create_deploy_nodejs_custom_container-envprop
And information using .ebextensions .config can be found at the following link
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions.html
Alternatively you can also set environment variable using the cli or via the AWS Console
eb cli set environment variables can be read per the below link.
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-setenv.html
Using AWS Console
To set system properties (AWS Management Console)
Open the Elastic Beanstalk console.
Navigate to the management console for your environment.
Choose Configuration.
In the Software Configuration section, choose Edit.
Under Environment Properties, create your name / values ...
Accessing Environment Configuration Settings
Inside the Node.js environment running in AWS Elastic Beanstalk, you can access the environment variables using process.env.ENV_VARIABLE similar to the following example.
process.env.MONGO_DB_URL
process.env.PARAM2
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs.container.html#create_deploy_nodejs_custom_container-envprop
Summary:
In summary I would recommend the following steps to integrate MongoDB with Elastic Beanstalk environments.
Step 1) Create a MongoDB Server outside of Elastic Beanstalk
Step 2) Create your Node.js application in Elastic Beanstalk that connect to your MongoDB server
3 options:
1) SSH into an eb instance and install the mongo CLI manually:
sudo yum-config-manager --add-repo https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/4.0/x86_64/
sudo yum install --nogpgcheck -y mongodb-org-shell
Disadvantage is that if EB scales down its number of instances and the instance you are currently on gets terminated, you get kicked out of the SSH session:
The system is going down for halt NOW!
Connection to 1.2.3.4 closed by remote host.
Connection to 1.2.3.4 closed.
ERROR: CommandError - An error occurred while running: ssh.
You then need to start all over again: connect to instance, install mongo CLI...
2) Pre-install mongo CLI on instances by using a .config file:
container_commands:
01-mongocli:
command: "sudo yum-config-manager --add-repo https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/4.0/x86_64/;sudo yum install --nogpgcheck -y mongodb-org-shell"
ignoreErrors: true //use this the ensure instance deployment even if mongo CLI installation fails
Again, if the instance gets terminated by auto-scaler you'd have to connect again, but you don't have to the install mongo CLI manually.
3) Create a separate instance that hosts your mongo CLI, as described in #amyloula's answer. If your mongodb is within an VPC you need to create that separate instance also within the VPC. You will then need to create a Gateway to access the instance publicly, as you cannot connect directly to an instance in a VPC.

Resources