recommended way to install mongodb on elastic beanstalk

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