How to access InfluxDB credentials on Platform.sh

Goal

Access credentials for InfluxDB from within a Platform.sh application using PHP.

Assumptions

  • an active Platform.sh project
  • properly configured .platform/services.yaml for the given service
  • SSH key configured on account if developing locally
  • InfluxDB installed if developing locally
  • properly configured InfluxDB relationship like so, in .platform.app.yaml:
relationships:
    influxdb: 'influxdb:influxdb'

If developing locally, remember to first open a tunnel to the project environment using the Platform CLI.

$ platform tunnel:open && export PLATFORM_RELATIONSHIPS="$(platform tunnel:info --encode)"

This will open an SSH tunnel to your current Platform.sh environment and expose a local environment variable that mimics the relationships array on Platform.sh. Check the Platform.sh documentation and How to develop locally on Platform.sh with a tethered connection for more information.

Problems

Platform.sh service credentials are made available to applications as the PLATFORM_RELATIONSHIPS environment variable, which is a base64-encoded JSON string that has to be decoded before it can be used.

There are two primary options for accessing service credentials on Platform.sh that can be used within an application:

  1. Using the Platform.sh Config Reader library
  2. Accessing environment variables directly

Steps (Config Reader)

1. Install the library

Install the configuration library. See the documentation for minimum requirements.

$ composer install platformsh/config-reader    

2. Create a Config object

Creating a Config object provides access to the Platform.sh environment.

use Platformsh\ConfigReader\Config;
    
$config = new Config();    

3. Read the credentials

// Get the credentials to connect to the InfluxDB service.   
$credentials = $config->credentials('influxdb');   

Steps (Manual)

1. Load and decode the environment variable

$relationships = json_decode(base64_decode(getenv('PLATFORM_RELATIONSHIPS')), TRUE);    

2. Read the credentials

$credentials = $relationships['influxdb'];   

Use:

In most cases, you will need only the host and port properties to connect to InfluxDB. Pass those to your InfluxDB library’s setup routine in your application. Most of the time the other values may be ignored.

In either case, credentials is now an array matching the relationship JSON object.

{
  "service": "influxdb",
  "ip": "169.254.113.144",
  "hostname": "haz5rys6n2dsnjusqa54os3ii4.influxdb.service._.eu-3.platformsh.site",
  "cluster": "rjify4yjcwxaa-master-7rqtwti",
  "host": "influxdb.internal",
  "rel": "influxdb",
  "scheme": "http",
  "type": "influxdb:1.3",
  "port": 8086
}

Conclusions:

Using either the language-specific Platform.sh Configuration library or direct access methods for environment variables, an application can get InfluxDB credentials in PHP.

Platform.sh supports configuration libraries for multiple languages. The PHP configuration library can be useful for inspecting the project environment:

// Checks whether the code is running in a build environment
$config->inBuild();
// Checks whether the code is running in a runtime environment
$config->inRuntime();

and for reading environment variables as attributes of config:

// Available in Build and at Runtime
$config->appDir;
// Available at Runtime only
$config->branch;
$config->smtpHost;

The APIs for each language are written to be as consistent as possible, but seek out each library’s documentation for specific differences.

Thanks for sharing!

I’m able to setup the database & tunnel to my InfluxDB, but whenever I’m trying to connect from my application or from the influx cli I get the following message:

ERR: error authorizing query: create admin user first or disable authentication
Warning: It is possible this error is due to not setting a database.
Please set a database with the command "use <database>".

Unfortunately there are no credentials in the relationship JSON. Is there a way to add some settings to the service.yaml or how is one able to connect a InfluxDB.

Thanks!

@chrigu for now, you have to create admin user yourself like this:

curl http://influx.internal:<port>/query -X POST -F q="CREATE USER admin WITH PASSWORD 'admin' WITH ALL PRIVILEGES"

We’re working on making this automatic and seamless, as with other services. It should be released rather soon.

1 Like

Thanks! That exactly the information I was missing.

Funnily enough it did not work when I used the curl command.

I had to (after I started the tunnel)

influx -host 127.0.0.1 -port 30000                                                                                                                                                              [7:13:48]
Connected to http://127.0.0.1:30000 version 1.7.10
InfluxDB shell version: v1.7.9
> CREATE USER admin WITH PASSWORD 'admin' WITH ALL PRIVILEGES

then to use the user

influx -host 127.0.0.1 -port 30000 -username admin -password ''