Skip to content

Instantly share code, notes, and snippets.

@fdnklg
Last active August 14, 2019 14:05
Show Gist options
  • Save fdnklg/ee3f80cd7ebe0d4037e5ae0449a03e26 to your computer and use it in GitHub Desktop.
Save fdnklg/ee3f80cd7ebe0d4037e5ae0449a03e26 to your computer and use it in GitHub Desktop.
Required steps and resources to setup a working restful api for a mongodb which is running on an aws ec2 instance.

How to setup a serverless rest api and connect it it with a mongodb on ec2 instance.

1. Setup an EC2 instance with terraform

Clone the Terraform example repo and follow the explained steps to get it running.

Be sure to open all necessary ports in your security groups used by the instance to make it available to the public.

1. Setup serverless on an aws with node.js

Follow the Tutorial to config serverless on your machine.

2. Install Mongodb on your EC2 instance

Follow this Tutorial to get the db on your instance runnning.

  • Pay attention to the right ' when copying the commands, otherwise you might get errors during your workthrough

Fix the permissions of the mongodb before starting it as a service with the following code:

# default path /data/db
cd /path/to/your/db
sudo chown -R mongodb:mongodb *
# or this
sudo chown -R $USER /data/db

Before starting the db as a service edit the mongo.conf and update dbPath, and network interfaces

# mongod.conf

# Where and how to store data.
storage:
  dbPath: /data/db

# network interfaces
net:
  port: 27017
  bindIpAll: true

security:
  authorization: enabled  

Now you should be able to login to your db and fill it with data.

3. Create a Restful API to you mongoBD with serverless

Check out the following Tutorial and find out how to get serverless provinding an API for you database. Furthermore test your routes offline with serverless offline.

Some important code snippets of the tutorial are broken and you have to look here for the snippets: Tutorial.

With this information you should be able to make your database accessible via a restful API. Good luck..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment