Deploying a Node.js and MongoDB Application in SAP Cloud Platform
SAP has, of late, shown considerable interest and has invested in the cloud game beginning with the introduction of SAP Cloud Platform – SCP. SCP was earlier known as HANA Cloud Platform or HCP. Given that tech giants like Amazon, Google and Microsoft are all vying for top place in the cloud platform and services area, it doesn’t seem to be a very simple task for a relatively unknown player to step into. However, SAP has so far proven to be up to the fight.
SAP is well for its proprietary software, however, over the last few years, SAP has started to proactively embrace open source software and technologies. An example of this is the investment the company made on putting together platforms that ran applications built in Go, Node.js, Ruby and, Python and supported by a number of open source backends like MongoDB, Redis, and PostgreSQL.
This brings us to the question about Cloud Foundry.
What is Cloud Foundry?
Quoting official documents, Cloud Foundry “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 an example of this ‘commercial CF cloud provider. Going back in time, software companies initially had dedicated servers. The responsibility of the storage, hardware, operating system maintenance, virtualization, durability etc was the organization’s own, internal IT team.
As the technology evolved and the world started moving towards the cloud, IAAS providers such as AWS shared part of the companies that was earlier shouldered by companies. Providers like AWS took over the responsibility of storage, hardware, scaling and virtualization.
At the next level of evolution came IAAS providers such as Heruko who took over responsibility of the overall process while organizations were free to concentrate on their key applications. Each dependency and monitoring could now be fulfilled via services and their numerous add-ons.
Now let us assume that we need to provide each member of an organization with a controlled and monitored environment similar to Heroku. This is where Cloud Foundry gets involved. Cloud Foundry enables users to install the required onto their machines, local data centers or IAAS providers such as AWS. Access can now be provided to the organization’s employees who can now execute the application they require.
Registration, Cockpit, and Environments
To start, you can register a trial account in SCP. Once done, you will now be moved to the landing page of the cockpit which contains the environments. SCP has two main environment types –
- Neo Environment
- Cloud Foundry Environment
Neo environments are SAP environments that own the infrastructure. These are inclined to lean toward SAP technologies. As users, you would have access to vast amounts of SAP components including IOT, Hana DB, SCI, Machine Learning, YAAS, Streaming services, etc.
Cloud Foundry environments, on the other hand, are those that are hosted within Azure in California and AWS in Frankfurt and N. Virginia.
Once you click the button called ‘Start Cloud Foundry Trail’, it would take a few minutes to get the account ready for use.
Once ready click, “Go to Space”.
Creating MongoDB service
You would likely notice a space with the name ‘dev’ created with a GB of memory which is ready for use. Once in this dev space use the left menu bar, navigate to Services → Service Marketplace. Here you will find a list of services that you can access. MongoDB should be one of them.
Now you would need to click on MongoDB and followed by “New Instance”. Continue to click next till you get to the final screen. Once in the final screen, you can input a name for the MongoDB instance.
Next, you need to click on the dashboard button to view the dashboard. You can spot this as the first in the list under the Actions tab.
As you can see, the dashboard contains details on the service, containers as well as endpoints, credentials, etc.
Installing Cloud Foundry Command Line Interface (CLI):
Once your database has been created, It is time to install Cloud Foundry CLI on your machine. You can follow the guidelines on this URL for installing Cloud Foundry based on your OS.
Once done, type the command ‘cf’ to verify the installation.
You would be required to set an endpoint for the API for the Cloud Foundry CLI. The endpoint you select needs to be based on your data center.
After selecting an API endpoint, the commands below will register the endpoint locally and login to your account via the CLI
cf api <api_endpoint> cf login
Once you have successfully logged in, it is time to check the services and build the available packs using the following commands –
cf buildpacks cf services
Under servers, we should be able to view the MongoDB service that we had created earlier.
Node.js Application and manifest.yml file
Let us now try and create a straightforward Node.js based application. This is an express app and logs details for process.env. The code below is for the Node.js application-
Except for the default index.js and the package.json files, you will also notice a manifest.yml file. This holds the configuration of the application like memory allocated, name, instance counts, etc. More details can be found here.
Pushing and logging
After the files have been created, it is now time to deploy the application. Application deployment and logging can be completed using the following commands –
cf push cf log <application name mentioned in the manifest file>
As stated above, cloud foundry allows users to focus on their application. The goal behind cf push is that when the command is executed, the complete code in the selected folder is pushed to the foundry.
After the code has been pushed, it is up to the platform to determine the project type after which it installs the buildpack as needed, processes the manifest file to configure the application, and services and deploys the application.
In this example, the push and log command was run simultaneously. While the application was built successfully, the cf failed to start the application. On the checking the log file, you will find the prompt: ‘missing script : start’. Cloud foundry expected a start script for the node.js application in the package.json file. You can add the following command to the package.json script attribute:
"start": "node index.js"
Once done, you can attempt to execute the ‘cf push’ command within the repository folder once again. This time you will see that the application is deployed successfully and the corresponding environment variables are printed in the logs.
Now that the application is running successfully, the next phase is to integrate it with the MongoDB instance created earlier. For the MongoDB instance to be accessible to the Node.js environment, we would need to add the following lines of code to the manifest.yml file.
services: - Test_DB
The name provided to the MongoDB earlier was ‘Test_DB’ at the time of creation. You can create any service from the earlier list and add it at this stage so it can be accessed by the Node.js environment.
If you have a look at the environment variables, you will notice a new attribute ‘VCAP_Services that has been added. VCAP_SERVICES contains the MongoDB credentials that the Node.js application will need to use.
This is just the tip of the iceberg with regards to the power and usefulness of Cloud Foundry. It has a vast array of features and capabilities that can be put to use in a number of different ways. The best way to understand what all Cloud Foundry is capable of is to continue to explore more and more.