{"id":1500,"date":"2019-03-24T21:46:12","date_gmt":"2019-03-24T13:46:12","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1500"},"modified":"2020-05-12T10:32:31","modified_gmt":"2020-05-12T02:32:31","slug":"getting-the-environment-variables-supplied-to-your-cloud-foundry-application-with-python-3-flask","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/getting-the-environment-variables-supplied-to-your-cloud-foundry-application-with-python-3-flask\/","title":{"rendered":"Getting the environment variables supplied to your Cloud Foundry application with Python 3 Flask"},"content":{"rendered":"<p>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.<\/p>\n<p>As a result of that, I created a Python 3 Flask application that will probe the environment variables that it receives from Cloud Foundry.<\/p>\n<p>This post lists the files that constitute that Python 3 Flask application.<\/p>\n<h2>File structure of my Python 3 Flask application on Cloud Foundry<\/h2>\n<p>My Python 3 Flask application consists of a directory of the following files at the same level:<\/p>\n<ul>\n<li><strong>app.py<\/strong> for realising the Python 3 Flask application.<\/li>\n<li><strong>manifest.yml<\/strong> for feeding Cloud Foundry with the information to build the environment for running my Python 3 Flask application.<\/li>\n<li><strong>requirements.txt<\/strong> for specifying the Python libraries that my Python 3 Flask application will depend on.<\/li>\n<li><strong>runtime.txt<\/strong> for specifying the version of Python 3 that I want Cloud Foundry to run my Python 3 Flask application with.<\/li>\n<\/ul>\n<h2>Contents of app.py<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nfrom flask import Flask, jsonify\r\n\r\napp = Flask(__name__)\r\n\r\n@app.route('\/')\r\ndef home():\r\n    environment_variables = { key: os.environ&#x5B;key] for key in os.environ.keys() }\r\n    return jsonify(environment_variables)\r\n\r\nif __name__ == '__main__':\r\n    port = int(os.getenv(&quot;PORT&quot;, &quot;5678&quot;))\r\n    app.run(host='0.0.0.0', port=port)\r\n<\/pre>\n<p>When we run the above script, we first import the Python modules that we will use in our application. <\/p>\n<p>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 <a href=\"http:\/\/flask.pocoo.org\/docs\/0.12\/api\/#flask.Flask.route\" rel=\"noopener\" target=\"_blank\">route()<\/a> decorator on the <code>home<\/code> function. <\/p>\n<p>Within the <code>home<\/code> function, we store the environment variables as a dictionary and use <a href=\"http:\/\/flask.pocoo.org\/docs\/1.0\/api\/#flask.json.jsonify\" rel=\"noopener\" target=\"_blank\">jsonify<\/a> to return that dictionary as a HTTP response.<\/p>\n<p>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.<\/p>\n<h2>Contents of manifest.yml<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n---\r\napplications:\r\n- name: FlaskViewCFEnv\r\n  memory: 256MB\r\n  disk_quota: 256MB\r\n  random-route: true\r\n  buildpack: python_buildpack\r\n  command: python app.py\r\n  instances: 1\r\n<\/pre>\n<p>When Cloud Foundry look up manifest.yml, it does the following:<\/p>\n<ul>\n<li>name our application <strong>FlaskViewCFEnv<\/strong>.<\/li>\n<li>allocate <strong>256MB<\/strong> of ram for our application to use.<\/li>\n<li>allocate <strong>256MB<\/strong> of disk space for our application to use.<\/li>\n<li>randomly allocate a route for this application.<\/li>\n<li>build the container with the <a href=\"https:\/\/docs.cloudfoundry.org\/buildpacks\/python\/index.html\" rel=\"noopener\" target=\"_blank\">Python Buildpack<\/a>.<\/li>\n<li>run app.py with the Python binary.<\/li>\n<li>spawn one computing instance to run our application<\/li>\n<\/ul>\n<h2>Contents of requirements.txt<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nFlask==1.0.2\r\n<\/pre>\n<p>Since our Python 3 Flask application requires the <a href=\"http:\/\/flask.pocoo.org\/\" rel=\"noopener\" target=\"_blank\">Flask microframework<\/a>, we specify it in requirements.txt.<\/p>\n<h2>Contents of runtime.txt<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npython-3.7.3\r\n<\/pre>\n<p>When we leave out <code>runtime.txt<\/code>, 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.<\/p>\n<h2>Deploying the Python 3 Flask application to Cloud Foundry and using it to probe the environment variables<\/h2>\n<p>Once I had created the files, I then <\/p>\n<ul>\n<li>navigate to the directory that contains the files for my Cloud Foundry application.<\/li>\n<li>use <a href=\"https:\/\/cli.cloudfoundry.org\/en-US\/cf\/login.html\" rel=\"noopener\" target=\"_blank\">cf login<\/a> to select the corresponding <a href=\"https:\/\/docs.cloudfoundry.org\/concepts\/roles.html#spaces\" rel=\"noopener\" target=\"_blank\">Cloud Foundry space<\/a> to deploy my application.<\/li>\n<li>use <a href=\"https:\/\/cli.cloudfoundry.org\/en-US\/cf\/push.html\" rel=\"noopener\" target=\"_blank\">cf push<\/a> to deploy my application.<\/li>\n<li>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.<\/li>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-oc\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-oc&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-oc&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1500&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n<p>As a result of that, I created a Python 3 Flask application that will probe the environment variables that it receives from Cloud Foundry.<\/p>\n<p>This post lists the files that constitute that Python 3 Flask application.<\/p>\n","protected":false},"author":1,"featured_media":1501,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375,4],"tags":[607,584,226,233,254],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Cloud-Foundry-logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-oc","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1500"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1500"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1500\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1501"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}