Building apps has never been faster, thanks to the improvements in virtualization. Lately I have been looking for better ways to build and implement apps without having to worry about ongoing front-end work, such as patching Linux machines, replacing them when they fail, or even SSH in systems.
In this two-part tutorial I will tell you how to build a Node.js app with MongoDB Atlas and use it easily with Amazon EC2 Container Service (ECS).
In the first part we deal with:
- Using Docker for app implementation
- Building a MongoDB Atlas Cluster
- Connect to a Node.js-based app that enables full CRUD operations
- Local start-up in a development environment.
In part two we will run our app in a Linux container on AWS by installing some tools, configuring some environment variables and launching a Docker cluster.
At the end of this two-part tutorial, you'll see how you can work with Node.js to start an application, how to build a MongoDB cluster for the app, and finally how AWS should be used for that app to implement. in containers.
A fast intro for docker
(Do you already know about Containers & Docker? Go ahead to the tutorial.)
Docker makes it easy for developers to build, send and execute distributed applications in different environments. It does this by helping you to "contain" your application.
What is a container? The Docker website defines a container as "a way to package software in a format that can be run in isolation on a shared operating system". More generally, containers are a form of system virtualization that allows you to run an application and its dependencies on processes isolated from sources. With containers you can easily package application code, configurations and dependencies into user-friendly building blocks that have been specially built to carry out your code. This ensures that you can deploy quickly, reliably and consistently, regardless of the deployment environment, and allow you to focus more on building your app. In addition, containers make it easier to manage microservices and support CI / CD workflows, isolating, testing and release incremental changes with little or no effect on production systems.
In the example that we will go through in this post, we are going to build a simple record system with MongoDB, Express.js, React.js and Node.js called mern-crud. We combine various general services on the AWS cloud platform to build a fully functional, containerized application with auto-scaling and load balancing. The core of our front-end will be implemented on Amazon EC2 Container Service (ECS).
In part one I will set up our environment and build the application on my local workstation. This will show you how to use MongoDB Atlas together with Node.js. can use. In the second part, we will pack everything by implementing it in ECS with coldbrew-client, a simple ECS command-line tool.
Below you will find the basis for building an application that allows you to perform CRUD operations in MongoDB.
With Amazon EC2 Container Service (ECS) you can deploy and manage your Docker instances using your AWS dashboard. For a complete overview of how it works, I recommend looking at Abby Fuller's "Getting Started with ECS" demo, which includes details on the role of each component of the underlying Docker deployment and how they work together to make your easily repeatable and deployable applications.
Configures AWS and coldbrew-client
AWS environment variables
Get started by signing up for your AWS account and creating your Identity and Access Management (IAM) keys. You can view the required permissions of the coldbrew-client documents. The IAM keys must be verified against the AWS API; we can export these as environment variables in the command shell that we use to execute coldbrew-client assignments:
export AWS_ACCESS_KEY_ID = "PROVIDEDBYAWS export AWS_SECRET_ACCESS_KEY = "PROVIDEDBYAWS
This allows coldbrew clients to use our user rights within AWS to create the right underlying infrastructure.
Getting started with coldbrew-client
We then work with coldbrew clients, an open source automation application for the deployment of AWS Docker containers. It removes many of the steps associated with creating our Docker images, creating the environment and automatically scaling the AWS groups associated with our Node.js web servers on AWS. The best part of coldbrew is that it is very portable and configurable.
The coldbrew documentation states the following:
coldbrew-client works on two simple concepts: applications (apps) and clusters.
- An app is the minimum betting unit.
- One or more apps can run in a cluster and share the computer resources.
We will use this tool to simplify many of the usual tasks that you should do when creating a production ECS cluster. It will do the following on our behalf:
Manually, all the above mentioned tasks would require a non-trivial effort.
To install coldbrew clients, we can download CLI executable file (coldbrew or coldbrew.exe) and place $ PATH in our
$ which coldbrew & # 39; Usr / local / bin / Coldbrew
Preparing our app and MongoDB atlas
ECS will enable us to perform a stateless front of our application. All our data is stored in MongoDB Atlas, the best way to run MongoDB on AWS. In this section we build our database cluster and connect our application. We can rely on coldbrew orders to manage future implementations of new versions of our application.
Your Atlas Cluster
Building a cluster in Atlas is simple and free. If you need help, check out this tutorial video that will guide you through the process. Once our free M0 cluster is built, it is easy to whitelist the IP addresses of our ECS containers so that we can store data in MongoDB.
I called my cluster
Download and configure the app
Let's start by downloading the required repository:
$ git clone firstname.lastname@example.org: cefjoeii / mern-crud.git Cloning in & # 39; mern-crud & # 39; ... remote: Counting objects: 303, done. remote: Total 303 (delta 0), reused 0 (delta 0), pack-reused 303 Objects received: 100% (303/303), 3.25 MiB 0 bytes / s, ready. Solving delta & # 39; s: 100% (128/128), done.
Let's go to the repository we cloned and look at the code:
server.js app depends on the database configuration stored in the
config / db.js file. We will use this to specify our M0 cluster in MongoDB Atlas, but we want to make sure that we do this using user credentials that comply with the principle of least privileges.
Protect our app
Let's use a base database to ensure that we do not give our application all rights to manage the database. All these basic users need access to reading and writing.
In the MongoDB Atlas user interface, click on the "Security" option at the top of the screen and then click on "MongoDB users"
Now click the "Add new user" button and create a user with read and write access to the namespace to which the cluster is written.
- Enter the username (I select mernuser)
- Click on the "Show Advanced Options" link
- Select the role "readWrite" in the "Database" section "merndb" and leave "collection" empty
- Create a password and save it somewhere to use in your connection string
- Click on "Add User"
We now have a user who is ready to access the MongoDB Atlas database cluster of our application. It is time to get our connection sequence and prepare the application.
Link our managed cluster to our app
You can open our connection string by going to the "Clusters" section in Atlas and clicking the "Connect" button.
This section provides an interface to whitelist IP addresses and methods to connect to your cluster. Let's choose the "Connect Your Application" option and copy our connection string. Let's copy the connection sequence and take a look at what we have to change:
The standard connection string gives us a number of parts for which our changes are needed, marked in red below:
MongoDB: // mernuser: demopass @ mern-demo-shard-00-00-x8fks.mongodb.net: 27017, mern-demo-shard-00-01-x8fks.mongodb.net : 27017, mern-demo-shard-00-02-x8fks.mongodb.net: 27017 / merndb ssl = true & replicaSet = Mern-demo-scherf-0 & authSource = admin
The username and password must be replaced with the login details we have previously created. We also have to specify the
merndb database in which our data is stored.
Let's add our connection string to the file
config / db.js and then compile our application.
Save the file and then import the required dependencies of the application from the root of the repository with the command "
npm install ":
Let's put our local IP address on the white list so that we can start our app and test that it works locally before we deploy it in Docker. We can click on "CONNECT" in the MongoDB Atlas user interface and then select "ADD NEW IP ADDRESS" to whitelist our local workstation:
We can now test the app and see that it starts as expected:
$ npm start > email@example.com start / Users / jaygordon / work / mern-crud > button server Listening on port 3000 Socket mYwXzo4aovoTJhbRAAAA connected. Online: 1 Connected to the database.
Let test http: // localhost: 3000 in a browser:
The hardest part is over, the next part is stakes with
coldbrew-cli which are just a few commands. In part two of this series we discuss how we can start with
If you have not previously been managed with MongoDB services, we recommend that you start with our free tier. For existing customers of external service providers, view our migration offer and discover how you can get 3 months of free service.