MongoDB with Spring Boot on AWS: A quick-start guide*

Cover Image for MongoDB with Spring Boot on AWS: A quick-start guide*
Wayne / Devscover
Wayne / Devscover

*Assuming basic knowledge of AWS and Java

While working on** Shout,** I went looking for a quickstart guide to installing MongoDB on AWS, but all I found was a 27 page document from Amazon talking about private clouds, elastic blocks and other madness I’ve never used; so I went it alone and found it surprisingly straightforward! So here’s my real quickstart guide

Provision an AWS Server {#e0c0}

I used Amazon Linux (Essentially CentOS) for this, just because I’m used to it, but a lot of people will use Ubuntu. The specs don’t really matter too much, but do make sure you provision at least enough storage space for your documents. Remember also to save the SSH keys that gets generated as part of the provision wizard.

Initially open up ports {#7777}

To get started, it’s easiest if you open up ports for SSH (22) and Mongo ( 27017) to anywhere. Don’t worry, we’ll lock it down later. Use Security Groups under the EC2 settings page to do this.

SSH Into Server {#52a8}

If you’re on Windows, install Putty. If your on a Linux PC or a Mac, you can just use a terminal. You should use the authentication keys provided by AWS when you provisioned the server (you may need to convert them into an appropriate format using PuttyGEN).

Install Mongo {#1baa}

First you want to create a file so you can install mongo with yum. Run this:

sudo vim /etc/yum.repos.d/mongodb-org-3.6.rep

and in it, you need this:

name=MongoDB Repository

You can now easily run yum to install mongo:

sudo yum install -y mongodb-org

You can then start mongo with this command:

sudo service mongod start

You can confirm it’s running by checking the logs:

sudo cat /var/log/mongodb/mongod.log

You’re looking for a line like

[initandlisten] waiting for connections on port <port>

and I’d really recommend making sure mongo starts up when the server starts (incase of a power outage or a restart)

sudo chkconfig mongod on

Open a mongo session on the command line {#d94e}


(Who’d have guessed!)

Create a database {#5f24}

use mycustom_db

Create a user {#4b7c}

    user: 'steve',
    pwd: 'AsecretPassword',
    roles: [{ role: 'readWrite', db:'mycustom_db'}]

Hopefully I don’t need to tell you to not actually use 'AsecretPassword' as your password…

Open Mongo Access {#4454}

sudo vim /etc/mongod.conf

Then edit the bindIp like so:

# network interfaces
  port: 27017
# bindIp:  <- change this line to:

You also want to scroll down to security and make sure it looks like this, so it’s enabled:

  authorization: 'enabled'

Test Locally {#7c08}

This step is going to be specific to your Spring Boot application, but in most cases, if you already have a local mongo db working, it should be a case of changing either the file or the application.yml file, to something like this:

      uri: mongodb://

You can find your EC2 public DNS address in AWS easily in a number of places:

Run your app, and check you get results.

I personally installed MongoDB Compass, a GUI that allows you to easily connect to your instance and check that results are showing.

Update your security group {#6fdf}

Once you’ve confirmed it’s all working locally, you want to push your changes to your app to the server it runs on, then configure the security groups to only allow access to that mongo server from your web application server…

As you can see, we’ve removed SSH, as we don’t need it anymore and we’ve restricted the inbound rules to a security group.

If you try and connect to this locally, you should get spring failing (that’s a good thing)

However, if you access your web app in a browser, it should connect fine and bring back results.

Done! How easy was that!? Let me know in the comments below if this helped, or if you have any questions!