SAP has been lately very interested and invested in cloud game with the introduction of SAP Cloud Platform - SCP(earlier called as HANA Cloud Platform - HCP). With the likes of Google, Amazon, Microsoft battling in the cloud services/platform arena, it isn’t going to be a cakewalk for anyone stepping into it but SAP is up for it.

SAP is known for their proprietary softwares but over the past few years they are proactively embracing open source technologies and softwares. Once such effort is to invest heavily on building platforms to run applications built in Go, Node.js, Python, Ruby supported by various open source backends like PostgreSQL, MongoDB, Redis.

You might be wondering what is Cloud Foundry and how does it fits into this?

What is Cloud Foundry?

As per the official docs

It is an open source platform that you can deploy to run your apps on your own computing infrastructure, or deploy on an IaaS like AWS, vSphere, or OpenStack. You can also use a PaaS deployed by a commercial CF cloud provider. A broad community contributes to and supports Cloud Foundry.

SAP is one such “commercial CF cloud provider”. Let us take a small history lessons to understand this. Initially all the software companies had their own servers. This means the onus of hardware, storage, virtualization, operating system maintenance, scaling, availability, durability etc was with the internal IT team.

The world started moving towards the cloud and IAAS providers like AWS shared the burden with the companies by taking the responsibility of hardware, storage, virtualization, scaling, availability on them.

Then came IAAS providers like Heroku who told us - we will take care of everything while you can focus on your core application. All dependencies and monitoring can be fulfilled using services and add-ons.

Lets say, you want to provide all the members of your organization with a controlled environment like Heroku. How can do that? Thats where Cloud foundry comes in. Cloud foundry provides you with the platform that can be installed in your local machines, local data centers or even in IAAS providers like AWS and provide access to your employees who can deploy the application they want.

This is what SAP does to provide a platform for open source technologies using Cloud Foundry in SCP.

Enough with all the theory, let us start with SCP and deploy our Node.js application with MongoDB backend on SCP.

Registration, Cockpit and Environments

Register a trial account in SCP

Once registered, you will be taken to the landing page of the cockpit containing the environments. SCP has two different types of environments

Neo Environment

Cloud Foundry Environment

Neo environments are SAP owned infrastructure and is inclined towards SAP technologies. You have access to tonnes of SCP components like SCI, IOT, Hana DB, Machine learning, Streaming services, YAAS etc

Meanwhile, Cloud Foundry environments are hosted in AWS and Azure(N. Virginia and Frankfurt - AWS and California - Azure)

Click on the “Start Cloud Foundry Trail” button. It would take a few minutes to set up the account

Once completed click on the “Go to Space” button

Creating MongoDB service

There would be a space with name dev created with 1GB of memory ready for consumption. Go into the dev space. Now on the left menu bar, go to Services > Service Marketplace. There you can find the services accessible to you and one of them is MongoDB.

Click on MongoDB and and click on “New Instance”

Keep clicking next till the final screen and in the final screen enter a name for the MongoDB instance

Once created, click on the dashboard button(first in the actions tab) to view the dashboard.

The dashboard consists of service, container details along with credentials, endpoints, etc.

Installing Cloud Foundry Command Line Interface (CLI):

Now that your database is created, lets install Cloud Foundry CLI in your machine. Install Cloud Foundry on your respective OS as mentioned in this url. Once installed type cf command to check the installation.

You need to set an API endpoint for the Cloud Foundry CLI. Select your endpoint based on your datacenter.

Once selected, use the following commands to register the endpoint locally and login to your account from the CLI.

cf api <api_endpoint>

cf login

Now that we have logged in, lets check the services and buildpacks that are available for us using the commands

cf buildpacks

cf services

In the services, we can see the mongoDB service that was created by us earlier.

Node.js Application and manifest.yml file

Lets create a very simple Node.js application. Its an express app which logs the process.env details in the log. Following is the code for the Node.js application

Apart from the default index.js and package.json files, you can see a manifest.yml file. This file consists of application configuration like name, memory allocated, instances count and a lot more. You can find more about it here.

Pushing and logging

Once all the files are created, we are ready to deploy the application. Deployment and logging can be performed by the following commands respectively

cf push

cf log <application name mentioned in the manifest file>

As mentioned earlier, cloud foundry lets you focus on just your application. The idea behind cf push is that when you execute the command, the entire code in the current folder is pushed to cloud foundry(internally, the code is stored in git repositories). Once the code is pushed, the platform determines the type of project and installs the necessary buildpack, processes the manifest file for application configuration, services and deploys the application.

I ran my push and log command side by side. Though the application was build successfully, cf failed to start the application. If you check the log file, it prompts “missing script: start”. Cloud foundry expects a start script in the package.json file for node.js applications. Lets add the following to the package.json’s script attribute

"start": "node index.js"

Execute the cf push command in the repository folder again and now you can see the application successfully deployed and we have the environment variables printed out in the logs. Lets take it and beautify it.

MongoDB Integration

Now that our application is running, lets integrate it with the MongoDB instance we created. In order to make the MongoDB instance accessible to the Node.js environment, add the following lines to the manifest.yml file.

services:

- Test_DB

Test_DB is the name that was provided to the MongoDB instance when it was created earlier.

Any services from the list earlier can be created and added here to have it accessible to the Node.js environment.

Check the environment variables to see a new attribute “VCAP_SERVICES” added since the last time. This contains MongoDB credential details that can be used by the Node.js application.

Final Application

To test the integration, I have written a small application which captures each visiting IP address and stores it in the database(using the above credentials) and displays the visitors list to the end user.

Tada! We have successfully deployed a Node.js and MongoDB application in SAP Cloud Platform with Cloud Foundry.