Getting the environment variables supplied to your Cloud Foundry application with Python 3 Flask

When I was trying to build a Python 3 Flask application on Cloud Foundry, I wanted to quickly check the environment variables that Cloud Foundry will supply to my application.

As a result of that, I created a Python 3 Flask application that will probe the environment variables that it receives from Cloud Foundry.

This post lists the files that constitute that Python 3 Flask application.

File structure of my Python 3 Flask application on Cloud Foundry

My Python 3 Flask application consists of a directory of the following files at the same level:

  • app.py for realising the Python 3 Flask application.
  • manifest.yml for feeding Cloud Foundry with the information to build the environment for running my Python 3 Flask application.
  • requirements.txt for specifying the Python libraries that my Python 3 Flask application will depend on.
  • runtime.txt for specifying the version of Python 3 that I want Cloud Foundry to run my Python 3 Flask application with.

Contents of app.py

import os
from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/')
def home():
    environment_variables = { key: os.environ[key] for key in os.environ.keys() }
    return jsonify(environment_variables)

if __name__ == '__main__':
    port = int(os.getenv("PORT", "5678"))
    app.run(host='0.0.0.0', port=port)

When we run the above script, we first import the Python modules that we will use in our application.

After that, we create an instance of Flask that will help give us a web interface to check the environment variables. Using the Flask instance, we then apply the route() decorator on the home function.

Within the home function, we store the environment variables as a dictionary and use jsonify to return that dictionary as a HTTP response.

After the definition of the home function, we then attempt to extract a port number from the environment. Finally, we run the flask application to listen to that port.

Contents of manifest.yml

---
applications:
- name: FlaskViewCFEnv
  memory: 256MB
  disk_quota: 256MB
  random-route: true
  buildpack: python_buildpack
  command: python app.py
  instances: 1

When Cloud Foundry look up manifest.yml, it does the following:

  • name our application FlaskViewCFEnv.
  • allocate 256MB of ram for our application to use.
  • allocate 256MB of disk space for our application to use.
  • randomly allocate a route for this application.
  • build the container with the Python Buildpack.
  • run app.py with the Python binary.
  • spawn one computing instance to run our application

Contents of requirements.txt

Flask==1.0.2

Since our Python 3 Flask application requires the Flask microframework, we specify it in requirements.txt.

Contents of runtime.txt

python-3.7.3

When we leave out runtime.txt, Cloud Foundry will use Python 2 runtime to run our Python Flask application. Therefore, we specify that we want to run our Python 3 Flask application with version the Python 3.6.7 runtime.

Deploying the Python 3 Flask application to Cloud Foundry and using it to probe the environment variables

Once I had created the files, I then

  • navigate to the directory that contains the files for my Cloud Foundry application.
  • use cf login to select the corresponding Cloud Foundry space to deploy my application.
  • use cf push to deploy my application.
  • use my browser to access the URL that Cloud Foundry had allocated to my application. My application will then return the environment variables that Cloud Foundry had supplied to it.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.