Skip to Content
Technical Articles
Author's profile photo Yohei Fukuhara

Create simple Flask REST API using Cloud Foundry

Hi All,

I am writing this blog to describe some easy steps to deploy a Python Flask based REST API application on the SAP cloud foundry environment.

Python is very useful to deal with many requirements, since it has so many powerful libraries. In my case, I recently used it to connect web application and TensorFlow serving for object detection. The python application resizes image file and converts data format.

For Flask logging, please see another article “Logging from Python Flask application deployed on Cloud Foundry”.

Environment

Local PC

  • Windows 10 Professional
  • Python 3.6.6  on Anaconda
  • Flask 1.0.2
  • cf CLI 6.37.0

Cloud Foundry

  • Python Build pack 1.6.20
  • CF Trial (Europe – Frankfurt)

Prerequisites

  • your space is created on Cloud Foundry environment
  • cf CLI is installed on Local PC(see the official page for the installation)

Procedure

1. Prepare for python application

The python application is on my Github repository, so just clone the repository is also OK here.

1.1. Application directory

Create an application directory on local PC. I created the one named “cloudfoundry-python-flask-sample”.

1.2. Flask application(hello.py)

The application is quite simple, because it just return “Hello World”.  It works also on local window PC.

from flask import Flask
import os

app = Flask(__name__)
# Port number is required to fetch from env variable
# http://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#PORT

cf_port = os.getenv("PORT")

# Only get method by default
@app.route('/')
def hello():
    return 'Hello World'

if __name__ == '__main__':
	if cf_port is None:
		app.run(host='0.0.0.0', port=5000, debug=True)
	else:
		app.run(host='0.0.0.0', port=int(cf_port), debug=True)

1.3. Add Flask library to file “requirements.txt”

Add Flask library to file “requirements.txt”.  If you want other libraries, just add them.

Flask

1.4. Python runtime version(runtime.txt)

Just write python runtime version. The version should be listed here.  As of 2018/12/12 I used python Buildpack 1.6.20, which was default on SAP Cloud Platform CF.

python-3.6.6

1.5. Manifest(manifest.yml)

This is so simple application that it does not need much resources.  Please make sure don’t allocate much resources.

---
applications:
- memory: 128MB
  disk_quota: 256MB
  random-route: true

1.6. Commands run the application(Procfile)

Here just write down a command, which runs hello.py application.

web: python hello.py

2. Deploy the application to cloud foundry

2.1. Check cloud foundry API endpoint

See the API endpoint from SAP Cloud Platform Cockpit.

2.2. Login Cloud Foundry using cf CLI

Run command prompt and login to the cloud foundry.

cf api https://api.cf.eu10.hana.ondemand.com
cf login

2.3. Deploy the application

Change current directory and deploy the application.

cf push <application name>

3. Check the result

3.1. Check the result

See the application status via SAP Cloud Platform Cockpit.  There is an application Routes, so just click the link.

Here I can see “Hello World”!

Conclusion

The advantage to use Cloud Foundry is that we can concentrate on development.  For me it is so helpful that there is few time to setup environment like OS, python.

Assigned tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Alejandro Sensejl
      Alejandro Sensejl

      Hi Yohei Fukuhara

      THX, for you blog. It was a great help understanding Python deployment to Cloud Foundry.

      I noticed that the app is accessible from the Internet without further login.

      Is that correct or did I miss some step? Can you explain how to implement check for authenticated user / authorization role, please?

      Cheers,

      Alej

      Author's profile photo Yohei Fukuhara
      Yohei Fukuhara
      Blog Post Author

      Hi,

       

      Yes, you are right.

      I haven't implemented any authentication checks.

      I haven't check how to implement it.

       

      Regards,

      Yohei

      Author's profile photo Perry Xu
      Perry Xu

      Awesome tutorial for those who are learning how to develop/deploy python app to CF

      Author's profile photo Daniel Wroblewski
      Daniel Wroblewski

      I am trying to follow this and get almost all the way but during push I get this error:

       

      Cell 578cfd64-547a-47eb-82d0-710b4a3fbd7a creating container for instance 6b6707da-7d3c-4305-81e5-f96bc447f13a
      Cell 578cfd64-547a-47eb-82d0-710b4a3fbd7a successfully created container for instance 6b6707da-7d3c-4305-81e5-f96bc447f13a
      Downloading app package...
      Error staging application: App staging failed in the buildpack compile phase
      FAILED

       

      Any idea what is happening?

       

      Author's profile photo Yohei Fukuhara
      Yohei Fukuhara
      Blog Post Author

      Will you share the whole log?

      Author's profile photo Daniel Wroblewski
      Daniel Wroblewski

      I kept at it and I had to change the Python version to 3.8.1 and it worked. At some point I saw a message indicating what versions were supported but not sure where I saw that.

      I also added in the manifest.yaml something about the buildpack and cflinux -- I don't have it right now -- but I don't think that did anything.

      Thanks for very helpful blog

      Author's profile photo Alok @SAP
      Alok @SAP

      Hi Yohei,

      Awesome Tutorial.

      Any idea how to debug the cloud api.

      Regards

      Alok

      Author's profile photo Yohei Fukuhara
      Yohei Fukuhara
      Blog Post Author

      > Any idea how to debug the cloud api.

      I have no idea.

      I usually output logs and see how processes work.

      I seldom develop apps, so I'm not sure if some good way exists.

       

      Yohei