Zum Inhalt springen

Bind a service instance to your app

Diese Seite ist noch nicht in deiner Sprache verfügbar. Englische Seite aufrufen

In this document we want to take a look on how you can bind a Service Instance to your application. With those services you can enrich your cloud experience with a persistance layer, a message queue or other usefull tools. The Cloud Foundry makes this process especially easy with its service brokers. Here you will learn how to use them.

This is a first steps overview on the topic of services within the Cloud Foundry. You can find more detailed information about managing services in the official Cloud Foundry documentation.

In this tab you will learn how to use the Cloud Foundry CLI to:

Binding your dataservice to your application means, that you automatically inject the connection credentials you need for accessing your dataservice into the environment variables of your applications space. If an automated process is updating your dataservice in a way, that the connection credentials change, the Cloud Foundrys automations will then directly make sure, that the injected credentials are also updated. This means after creating and binding a service instance, all you have to do is read out the connection credentials from the environment variables and you are ready to use your dataservice without any further worries.

You can easily bind a dataservice instance to your application via the following $ cf bind-service command. After the binding is complete, you have to restage your application in order to let the application read out the newly injected environment variables.

Terminal window
cf bind-service APP_NAME SERVICE_INSTANCE_NAME [-c PARAMETERS_AS_JSON] [--binding-name BINDING_NAME]
cf restage APP_NAME

After the binding is successfull you can see a new object in the environment variables of your space called VCAP_SERVICES. In there you can find the list of services bound to your application like in the example below.

{
"VCAP_SERVICES": {
"service-name": [
{
"name": "postgres-database",
"binding_name": "postgres-database",
"credentials": {
...
}
}
]
}
}

You can easily read out the environment variable for your bound dataservice via the set binding name.

You can also unbind your service instances again if you no longer need them. You can do an unbind with the following command:

Terminal window
cf unbind-service APP_NAME SERVICE_INSTANCE_NAME