Bman2386
Bman2386

Reputation: 21

How to create an AWS app that uses MongoDb for database?

I'm trying to move an existing node.js express app from Heroku to AWS. The app uses MongoDB for the database. The problem is aws EB (Elastic Beanstalk) doesn't natively support no-sql databases (I understand that I need to use a VPC and am able to connect a VPC to MongoDB). I'm a little confused with setup both on the app's code (how the config files should be written/if any backend changes that might be needed), and how it should be setup in AWS. Is there a better option for this? Right now eb deploy fails and the logs don't seem helpful. Any advice is greatly appreciated.

config.yml

branch-defaults:
  Master:
    environment: Testproject-env
environment-defaults:
  Testproject-env:
    branch: null
    repository: null
global:
  application_name: test-project
  default_ec2_keyname: null
  default_platform: Node.js
  default_region: us-east-2
  include_git_submodules: true
  instance_profile: null
  platform_name: null
  platform_version: null
  profile: eb-cli
  sc: git
  workspace_type: Application

nodecommand.config

option_settings:
  aws:elasticbeanstalk:container:nodejs:
    NodeCommand: "npm start"

app.js

const express = require("express");
const mongoose = require('mongoose');
const users = require('./routes/api/users');
const bodyParser = require('body-parser');
const passport = require('passport');
const path = require('path')

const app = express();

if (process.env.NODE_ENV === 'production') {
    app.use(express.static('frontend/build'));
    app.get('/', (req, res) => {
        res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'));
    })
}

const db = require('./config/keys_dev').mongoURI;
mongoose
    .connect(db, { useNewUrlParser: true })
    .then(() => console.log("Connected to MongoDB successfully"))
    .catch(err => console.log(err));

app.use(passport.initialize());
require('./config/passport')(passport);

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use('/api/users', users);

const port = process.env.PORT || 5000;

app.listen(port, () => console.log(`Server is running on port ${port}`));

Upvotes: 2

Views: 364

Answers (1)

Allan Chua
Allan Chua

Reputation: 10175

Would suggest to consider using Amazon Document DB instead for this use-case. Amazon Document DB offers a MongoDB compatible engine so we don't have to make any code-changes when migrating MongoDB applications in AWS except for connection string updates.

What makes using Amazon Document DB really interesting for this migration use-case is that you could take advantage of AWS Data Migration Service (DMS) to pull your existing dataset from the old MongoDB to Amazon Document DB with little effort. Here is a starter guide to migrating data from MongoDB to Document DB.

Upvotes: 1

Related Questions