Set environment variables in pivotal application

Environment variable is part of the environment in which a process running. An environment variable is a dynamic-named value that can affect the running processes will behave on a computer. In pivotal, environment variable will play a key role to push and run an application. Cloud foundry will provide different set of command to view and set environment variables through command line interface.

To view specific application environment variables we can use the cf env followed by application name. Syntax look like below,

cf env

 For example, my application_name is usermanagement. Then the command which I need to execute is like below,

cf env usermanagement

The response which I got after the execution of above command is,
Environment Variables:

Getting env variables for app usermanagement in org javaisocean / space development
 as javaisocean@gmail.com...
OK

System-Provided:

{
 "VCAP_APPLICATION": {
  "application_id": "bf7ee518-3722-4dc7-98f4-4c86769769f5",
  "application_name": "usermanagement",
  "application_uris": [
   "usermanagement.cfapps.io"
  ],
  "application_version": "0e6a6fae-4aa5-4d1a-8148-a33a268d62a4",
  "cf_api": "https://api.run.pivotal.io",
  "limits": {
   "disk": 1024,
   "fds": 16384,
   "mem": 650
  },
  "name": "usermanagement",
  "space_id": "ecc72a08-9fd2-4763-bc00-07282a3f528d",
  "space_name": "development",
  "uris": [
   "usermanagement.cfapps.io"
  ],
  "users": null,
  "version": "0e6a6fae-4aa5-4d1a-8148-a33a268d62a4"
 }
}

No user-defined env variables have been set

No running env variables have been set

No staging env variables have been set

If you observe the above information, there are different types of env variables we can see. Those are like, System provided, user-defined, running and staging env variables. We can not change anything which is coming from System provided which was configured by the System while pushing the application or binding services.

As per the above information, there is only VCAP_APPLICATION information and no VCAP_SERVICES information. Because the usermanagement application doesn’t bind with any services. That means, VCAP_APPLICATION which contains the information of application and VCAP_SERVICES will contains the information of services which are bind to the application.

The below is the application environment variables, after binding the ClearDB MySQL database to usermanagement application.

Environment variables after binding ClearDB MySQL service:

Getting env variables for app usermanagement in org javaisocean / space development
as javaisocean@gmail.com...
OK

System-Provided:
{
 "VCAP_SERVICES": {
  "cleardb": [
   {
    "binding_name": "mysql-db",
    "credentials": {
     "hostname": "us-cdbr-iron-east-01.cleardb.net",
     "jdbcUrl": "jdbc:mysql://us-cdbr-iron-east-01.cleardb.net/ad_ce4b59777591895
?user=ba9fca71350825\u0026password=80a3164a",
     "name": "ad_ce4b59777591895",
     "password": "80a3164a",
     "port": "3306",
     "uri": "mysql://ba9fca71350825:80a3164a@us-cdbr-iron-east-01.cleardb.net:3306/
ad_ce4b59777591895?reconnect=true",
     "username": "ba9fca71350825"
    },
    "instance_name": "mysql-spark-db",
    "label": "cleardb",
    "name": "mysql-db",
    "plan": "spark",
    "provider": null,
    "syslog_drain_url": null,
    "tags": [
     "Data Stores",
     "Cloud Databases",
     "Developer Tools",
     "Web-based",
     "Data Store",
     "Online Backup \u0026 Storage",
     "Development and Test Tools",
     "Single Sign-On",
     "Buyable",
     "relational",
     "mysql",
     "Cloud Security and Monitoring"
    ],
    "volume_mounts": []
   }
  ]
 }
}

{
 "VCAP_APPLICATION": {
  "application_id": "5b64b05f-4ba0-4f51-9680-c9fe5c8f109a",
  "application_name": "usermanagement",
  "application_uris": [
   "usermanagement.cfapps.io"
  ],
  "application_version": "d12ee54c-57fd-4973-9eb0-5e20eca520f0",
  "cf_api": "https://api.run.pivotal.io",
  "limits": {
   "disk": 1024,
   "fds": 16384,
   "mem": 650
  },
  "name": "usermanagement",
  "space_id": "ecc72a08-9fd2-4763-bc00-07282a3f528d",
  "space_name": "development",
  "uris": [
   "usermanagement.cfapps.io"
  ],
  "users": null,
  "version": "d12ee54c-57fd-4973-9eb0-5e20eca520f0"
 }
}

No user-defined env variables have been set

No running env variables have been set

No staging env variables have been set

How to set environment variables?

Cloud Foundry provides a command called set-env to set environment variables followed by the application name along with property name and value.

Set-Env command syntax look like,

cf set-env

You can see the environment variables provided by user under User-defined env variables when you ran the below command.

cf env

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s