Install MEAN Stack Using CloudFormation

AWS CloudFormation is a service that helps you model, setup and replicate your AWS resources. It uses a template file to bring up a collection of resources together as single stack.

To create templates we use a JSON file or AWS CloudFormation Designer. For this tutorial I opted the first solution.

Note: The template is available on my Github 😎.

We start with a basic template that defines a single EC2 instance with a security group that allows SSH traffic on port 22, MongoDB traffic on port 27017, and the NodeJS app on port 3000 from anywhere, as shown below:

In addition to that, we create two input parameters that specify the instance type and a Key Pair for SSH access. Then, we use UserData property to provide a set of shell commands to install MongoDB, NodeJS and bootstrap a simple MEAN application. Finally the output section print the public URL of the MEAN application.

Now we defined our template. Go to AWS Management Console then navigate to CloudFormation Dashboard and click on “Create Stack“:

Upload the JSON file and click on “Next“:

Assign a name to the stack, and choose your instance type and key pair you will use to ssh to the instance. Then, click on “Next“:

Left all fields unchanged and click on “Next“, then “Create

Once launched, you will get the following screen with launching process events:

After a while, you will get the CREATE_COMPLETE message in the status tab.

If you point your browser to the URL shown in the Outputs tab, you should see: a simple HTML message:

If we change the endpoint we should see a JSON response:

Congratulation ! ✨🎉 you have deployed your MEAN Stack application.

Go to EC2 Dashboard, you should see your instance there:

Verify the security group is setup as configured in the template:

To verify all packages and dependencies has been installed correctly, we can connect to the server via SSH:

To terminate the instance we can delete the stack from the CloudFormation Wizard, Deleting the stack will terminate all the instances launched by the stack:


Build RESTful API in Go and MongoDB

In this tutorial I will illustrate how you can build your own RESTful API in Go and MongoDB. All the code used in this demo can be found on my Github. 😊

1 – API Specification

The REST API service will expose endpoints to manage a store of movies. The operations that our endpoints will allow are:

2 – Fetching Dependencies

Before we begin, we need to grap the packages we need to setup the API:

  • toml :  Parse the configuration file (MongoDB server & credentials)
  • mux : Request router and dispatcher for matching incoming requests to their respective handler
  • mgo : MongoDB driver

3 – API structure

Once the dependencies are installed, we create a file called “app.go“, with the following content:

The code above creates a controller for each endpoint, then expose an HTTP server on port 3000.

Note: We are using GET, POST, PUT, and DELETE where appropriate.  We are also defining parameters that can be passed in

To run the server in local, type the following command:

If you point your browser to http://localhost:3000/movies, you should see:

4 – Model

Now that we have a minimal application, it’s time to create a basic Movie model. In Go, we use struct keyword to create a model:

Next, we will create the Data Access Object to manage database operations.

5 – Data Access Object

5.1 – Establish Connection

The connect() method as its name implies, establish a connection to MongoDB database.

5.2 – Database Queries

The implementation is relatively straighforward and just includes issuing right method using db.C(COLLECTION) object and returning the results. These methods can be implemented as follows:

6 – Setup API Endpoints

6.1 – Create a Movie

Update the CreateMovieEndpoint method as follows:

It decodes the request body into a movie object, assign it an ID, and uses the DAO Insert method to create a movie in database.

Let’s test it out:

With Postman:

With cURL

6.2 – List of Movies

The code below is self explanatory:

It uses FindAll method of DAO to fetch list of movies from database.

Let’s test it out:

With Postman:

With cURL:

6.3 – Find a Movie

We will use the mux library to get the parameters that the users passed in with the request:

Let’s test it out:

With Postman:

With cURL:

6.4 – Update an existing Movie

Update the UpdateMovieEndPoint method as follows:

Let’s test it out:

With Postman:

With cURL:

6.5 – Delete an existing Movie

Update the DeleteMovieEndPoint method as follows:

Let’s test it out:

With Postman:

With cURL:

7 – Taking this further

On my upcoming posts, I will show you how :

  • Write Unit Tests  in Go for each Endpoint
  • Build a UI in Angular 4
  • Setup a CI/CD with CircleCI
  • Deploy the stack on AWS and much more …

So stay tuned 😇