Target the cloud effortlessly with your MEAN application and Stacksmith

Jose Fuentes

Jose Fuentes


Advanced Engineer


Share this article

If you've ever created one or more web applications using Node.js and MongoDB, you've probably considered the benefits of running it in AWS or in Kubernetes, but you may not be familiar enough with these platforms and their core concepts to take the leap.

Bitnami Stacksmith is a tool that can automate packaging and optimize your application for deployment to container and cloud platforms. It can be used in new application development (as I will do here) as well as with legacy applications, to prepare them for replatforming from your datacenter to the cloud.

In this article, I am going to package a common MEAN To-Do application, and deploy it to AWS as well as to a Kubernetes cluster (yes, a single packaging process can deliver the assets needed for deployment to multiple platforms).

The app is a Node.js-Express app that uses MongoDB to maintain state. The application can be found in this GitHub repository. All the necessary dependencies are handled by npm. It requires some configuration to connect to the database. This configuration is in database.js.

How does Stacksmith work?

Let's see the Stacksmith key features to understand better how it works:

  • Stacksmith provides a set of "stack templates", which are tailored to the most common types of applications, with free-form templates also available. Pick the one that fits your application requirements. For this example, I will use the Node.js application with a NoSQL database (MongoDB) stack template.

  • Stacksmith knows which dependencies are required by the different cloud platforms, and the best way to connect and configure applications and dependencies. It brings these into the resulting deployable artifacts during the packaging process.

  • Stacksmith also documents the components of the packaged application, and monitors the components for updates and security patches.

    TIP: Find how to maintain and update your Node.js application in the following blog post.

The image below shows the Stacksmith workflow:

Stacksmith workflow

All that Stacksmith requires is the application source and some scripts that define how the app is built, configured, and run. These are the build, boot, and run scripts, respectively.

  • The build script runs at build time (i.e. changes made by this script will end up in the resulting image). Here's a build script for an application of this type. It just unpacks the application sources to a directory with the proper permissions and runs npm install.

  • The boot script runs in the VM or container that is instantiated from the image. It is intended to do the initial configuration for that instance, and it is run before most of the services have started. The configuration will be typically injected into the instance via environment variables that are accessible via the boot script.

    Every template has its own documentation with the available configuration variables. In the documentation for the Node.js Application with MongoDB template, you'll find the available variables are DATABASE_HOST, DATABASE_PORT, DATABASE_USER, DATABASE_PASSWORD and DATABASE_NAME.

    You can find an example of a boot script crafted specifically for the sample TO-DO app here. It patches the previously mentioned database.js, setting the different parameters with the values that the environment variables have in the script.

  • The run script is intended to start the application. For an application like the one in our example, the run script will just run npm start. You can find it here.

Once you have uploaded these scripts to Stacksmith, hit "Create" to start the packaging process, and you'll get deployable artifacts for the clouds you are targeting. In the current example, that means a Cloud Formation template for AWS or a Helm Chart for Kubernetes.

Migrate your MEAN application to AWS or Kubernetes using Stacksmith

This section walks you through the whole process of migrating the sample MEAN application introduced in the previous section. It is a pretty straightforward three-step operation.

  1. Step 1 shows you how to get a working account on Stacksmith.
  2. Step 2 details how to interact with Stacksmith to package the application and get deployable artifacts.
  3. Step 3 offers a guide for launching those artifacts on both AWS and Kubernetes.

Step 1: Log in to Stacksmith

The first step is to log in to stacksmith.bitnami.com with your Bitnami account. If you don't have an account with Bitnami, you'll be invited to create a new one.

At the time of writing, a 30-day trial is available at stacksmith.bitnami.com. Stacksmith stores all the files you upload as well as the images and artifacts it creates to your cloud account, so, you will have to onboard your AWS credentials.

Stacksmith provides instructions on how to obtain some valid AWS credentials so you can use the service. If you do not have AWS credentials, you can request access to a cloud sandbox account.

Step 2: Package a new app

Once you are logged in, you will be able to package your first app.

Package your application

Stacksmith will ask for some information and several files before packaging your application. Fill in the form as follows:

  1. Introduce a name for your app, and a version tag.
  2. Select the Node.js application with NoSQL DB (MongoDB) stack template.
  3. Select the target platforms you'd like to package the app for (you can select several).
  4. Upload the application code and the previously mentioned scripts.

To obtain the files for this example, you can either clone the repository and create the tarball by yourself by typing:

git clone https://github.com/bitnami-labs/stacksmith-examples
cd stacksmith-examples/nodejs-with-nosql/todo
tar czf app.blue.tar.gz app

Or use the following direct links:

Create a new application

Click "Create" and wait for the build to complete. If you’re impatient, you can see the logs of the build live!

Build logs

Once the process finishes, you will see that Stacksmith has created a Cloud Formation template or a Helm chart.

  • The Cloud Formation template describes the infrastructure that AWS is going to create for serving your application, and how to configure it.

    In this case, it will contain two groups (one for the Node.js application and another for the MongoDB database) with one instance each. The group for the Node.js application can be horizontally scaled by incrementing the number of instances in the next screen. Those instances are instantiated from the AMI referenced by the image id that Stacksmith gives you. The template also sets things like the load balancer, some data volumes and all the necessary networking around all the components.

  • The Helm chart describes the equivalent set of resources for Kubernetes. It contains all the manifests that tells Kubernetes which pods, services, volumes, and other resources it should create, and how to interconnect them.

    The containers that serve the Node.js application are instantiated from the Docker image referenced by the image id that Stacksmith provides.

Step 3: Launch the app

So let's launch the app. Instructions for both AWS and Kubernetes are provided below.

Deploy your application to AWS

  • On the view of your AWS build, click the "Launch in AWS" button.

Launch in AWS

You will be redirected to the AWS Cloud Console "Create Stack -> Select Template" section. As you can see in the image below, the "Amazon S3 template URL" is already preselected and it is the one resulting from the build process done in Stacksmith.

  • Click "Next" to proceed.
  • Specify some values for the instance, such as the SSH key or ports.
  • Set the SSH key name to your key and application source to 0.0.0.0/0. The latter is important if you want the application to be publicly accessible.
  • Select the number of instances for the machine that will run the app. This template includes a load balancer that will balance the load between them.
  • Click "Next" to continue.

Fill configuration parameters

  • In the resulting screen, check that all the values are correct and click "Create" to deploy your stack.

After the creation process finishes, you will see the "PublicDnsName" output. That is a link to the public URL where the application is available.

  • Click on the link to navigate to the public URL where the app is now available to the world. You should see something like this:

Application running

Deploy your application to Kubernetes

NOTE: Before performing the following steps, make sure you have a Kubernetes cluster running, and that Helm and Tiller are installed correctly. For detailed instructions, refer to our Kubernetes starter tutorial. These steps also require your cluster to access the ACR registry that stores the images generated for you by Stacksmith. See Stacksmith documentation to configure ACR access.

  • On the view of your Kubernetes build, click on the "Download Helm chart" button. This will download the custom Helm chart for your application as an archive.

Download chart

  • Make sure that you can to connect to your Kubernetes cluster by executing the command below:

    $ kubectl cluster-info
    
  • Use helm to install the chart:

    $ helm install my-to-do-application-1-docker-719364341.tgz --name my-todo-app
    

This will create all the necessary resources in the cluster to run your application.

You will be able to monitor the status of the deployment using the helm status command with the deployment name, as shown below:

  $ helm status my-todo-app

Once the deployment is ready, you will be able to see the application running as in the AWS example. Pay special attention to the NOTES section of the output.

Helm status output

Run the following commands and go to http://127.0.0.1:3000 to access the application:

$ export POD_NAME=$(kubectl get pods --namespace default -l "app=my-todo-app-my-to-do-application" -o jsonpath="{.items[0].metadata.name}")
$ echo "Visit http://127.0.0.1:3000 to use your application"
$ kubectl port-forward $POD_NAME 3000:3000

So far I just gave you an overview of Stacksmith: what it is, how it works, how to migrate and deploy your MEAN application to AWS and Kubernetes. which components it's constructed from and how it really works.

The second part of this post will show you how Stacksmith monitors the components of your packaged application to keep it always updated. Let's move on!

Go to part II: How do I update my MEAN application with Stacksmith