External services for your Cloud Foundry app

As we have seen in previous posts Cloud Foundry Marketplace offers a large amount of services, including a variety of databases, message queuing, etc which will address the majority of use cases. However the offerings available in the Marketplace can be customized by the provider. For example the offerings from Pivotal Cloud Foundry might differ from Cloud Foundry on Azure or on Google Cloud Platform etc. In general each cloud provider will try to differentiate by extending the platform with whatever managed service they believe is unique to them and is going to help them attract customers.

For example Google offers services like Google Cloud Storage, BigQuery, BigTable or the Machine Learning APIs, etc. Whereas Microsoft Azure offers Azure Storage, Azure SQL or CosmosDB in addition to Redis, MySQL or PostgreSQL.


In any case, incorporating these services into your application should be done by reading credentials and other binding information from the environment so that we adhere to the 12-factor app principles. So what happens if a service is not available in the platform, can we still use it from our app? The answer is yes. Let’s see how.

In addition to environment variables defined by the platform itself, such as when we bind a new service to the app (see the Redis blog post) Cloud foundry allows us to define our own environment variables. We can do this in three different ways:
  • From the Console, app > settings tab > User Provided Environment Variables. In the screenshot below you can see we have defined 3 variables with connection details for a hypothetical database
  • From the CF CLI as follows. Here we have defined the same DB-user variable for our "piperhw" app:
cf set-env piperhw DB-user piper01
  • From the manifest.yml file, if using it
---
# this manifest includes the definition or 2 vars
applications:
- name: piperhw
  memory: 64M
  instances: 1
  path: ./piperhw/
  env:
    DB-user: piper01
    DB-password: S3cr3t_P@ssw0rd

Finally, regardless of the method used, the mechanism to read those vars from within the Python app is the same. Use the "os" Python module

import os
host = os.environ['DB-host'] 
user = os.environ['DB-user']
pasw = os.environ['DB-password']

In the next post we will use this mechanism to access either object storage from either AWS S3 or a compatible service like DellEMC ECS(Elastic Cloud Storage)

Comments

Popular posts from this blog

Sending PowerStore alerts via SNMP

Electronic Nose - eNose

Use Vagrant to deploy to AWS