Articles in this section

How to access Memcached credentials on Upsun

Goal

Access credentials for Memcached from within a Upsun application using Node.js.

Assumptions

  • an active Upsun project
  • properly configured .upsun/services.yaml for the given service
  • SSH key configured on account if developing locally
  • Memcached installed if developing locally
  • properly configured Memcached relationship like so, in .upsun.app.yaml:
relationships:
     memcachedcache: "cachemc:memcached"

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

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

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

Problems

Upsun 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 Upsun that can be used within an application:

  1. Using the 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.

$ npm install platformsh-config --save

2. Create a Config object

Creating a Config object provides access to the Upsun environment.

const config = require("platformsh-config").config();

3. Read the credentials

// Get the credentials to connect to the Memcached service.
const credentials = config.credentials('memcachedcache');

Steps (Manual)

1. Load the environment variable

relationships = JSON.parse(new Buffer(process.env['PLATFORM_RELATIONSHIPS'], 'base64').toString());

2. Read the credentials

credentials = relationships['memcachedcache'];

Use

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

In either case, credentials is a an object matching the relationship JSON object, which includes the appropriate user, password, host, database name, and other pertinent information.

{
  "service": "cachemc",
  "ip": "169.254.192.212",
  "hostname": "tmimatsnz2dv26qlpuespenf3u.memcached.service._.eu-3.upsun.site",
  "cluster": "rjify4yjcwxaa-master-7rqtwti",
  "host": "memcachedcache.internal",
  "rel": "memcached",
  "scheme": "memcached",
  "type": "memcached:1.4",
  "port": 11211
}

Conclusions:

Using either the language-specific Upsun Configuration library or direct access methods for environment variables, an application can get Memcached credentials in Node.js.

Upsun supports configuration libraries for multiple languages. The Node.js 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.

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.