In the world of Cloud Foundry, buildpacks provide necessary runtime and framework support for your application to run. The artifacts mentioned in the manifest file are used to determine the buildpack and the command necessary to run the application. SAP Cloud Platform supports the concept of BYOL (Bring Your Own Language) where the users can System buildpacks or the community buildpacks that are possible to be run on Cloud Foundry environment.
In this blog, we will have a look at one such development where we will use the Cloud foundry system buildpack. We will develop and deploy a simple Python application using the python buildpack to the SAP Cloud Platform Cloud Foundry environment.
In this blog, you will learn the following:
- Deploying a simple Python application on SAP Cloud Platform
- Consuming the buildpack that is not available in SAP Cloud Platform (BYOL)
- Project structure/files required to deploy the python application
About the project
In this tutorial, we will use the following framework and server module to develop a web application that displays a simple message.
This application uses flask which is a web development framework for Python. This framework also helps to define the port in which you want the application to run. In the main application file, we will use the following lines of code to use the flask library and define the port.
from flask import Flask import os app = Flask(__name__) port = int(os.getenv("PORT", 9009)) if __name__ == '__main__': app.run(host='0.0.0.0', port=port)
Now that we have defined the framework for web development, we need a Web Server Gateway Interface (WSGI) that acts as an interface between the framework and the web server. Gunicorn is a Python WSGI HTTP Server.
Both the dependencies – Gunicorn and Flask must be provided in the requirements.txt file. The project structure looks like this:
- The helloworld.py is the actual Python script file or the application that contains the application logic. This is where the Flask related details are coded. A sample code is as depicted below:
""" This is my first python application that is being deployed on SAP Cloud Platform in the Cloud Foundry environment """ from flask import Flask import os app = Flask(__name__) port = int(os.getenv("PORT", 9009)) @app.route('/') def hello_world(): return 'Hello World! Welcome to SAP Cloud Platform' if __name__ == '__main__': app.run(host='0.0.0.0', port=port)
- As with any Cloud Foundry application, we can deploy the application with the manifest.yml file that contains the information about the application artifacts. Below is a sample of what can be coded for Python deployment.
--- applications: - name: myfirstapp memory: 128M command: python helloworld.py buildpack: https://github.com/cloudfoundry/buildpack-python.git
Note the buildpack reference provided in the manifest file. SAP Cloud Platform takes this buildpack for the runtime container of this application.
- The requirement file contains the information about the dependent libraries.
The requirement file would just simply contain the library name. Example: If Flask is required, the file would have the word “Flask”.
Deploying the application
The application can now be deployed from the Cloud Foundry Command Line Interface using the cf push command. It uses the manifest.yml file for the deployment.
For the sake of understanding the process of deployment, go through the prompts in the window.
- The tool reads the manifest.yml to understand the application artifacts like application name, buildpack, etc.
- The route URL to access the application in SAP Cloud Platform is created
- The application is bound to the route
- The application source code is then uploaded
- The application is started and then the health is monitored
- The application details like the memory consumption, number of instances, URL, etc. are summarized.
The details of the application can be viewed in the SAP Cloud Platform cockpit.
Upon launching the application URL, the Hello World message is displayed successfully.