2

I would like to specify dependent environment variables on a Cloud Run service.

If the environment variables have been defined in a .env file it would look like this

DATABASE_NAME=my-database
DATABASE_USER=root
DATABASE_PASSWORD=P4SSw0rd!
DATABASE_PORT=5432
DATABASE_HOST="/socket/my-database-socket"
DATABASE_URL="user=${DATABASE_USER} password=${DATABASE_PASSWORD} dbname=${DATABASE_NAME} host=${DATABASE_HOST}"

In this example, DATABASE_URL depends on every other environment variables.

To deploy the service I run the following command:

gcloud run deploy my-service \
--image gcr.io/my-project/my-image:latest \
--region europe-west1 \
--port 80 \
--platform managed \
--allow-unauthenticated \
--set-env-vars 'DATABASE_NAME=my-database' \
--set-env-vars 'DATABASE_USER=root' \
--set-env-vars 'DATABASE_PASSWORD=P4SSw0rd!' \
--set-env-vars 'DATABASE_PORT=5432' \
--set-env-vars 'DATABASE_HOST="/socket/my-database-socket"' \
--set-env-vars 'DATABASE_URL="user=$(DATABASE_USER) password=$(DATABASE_PASSWORD) dbname=$(DATABASE_NAME) host=$(DATABASE_HOST)"'

Here is the created YAML definition of the service (some values are omitted)

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: my-service
spec:
  template:
    metadata:
      name: ...
    spec:
      containerConcurrency: 80
      timeoutSeconds: 300
      containers:
      - image: ...
        ports:
        - name: http1
          containerPort: 80
        env:
        - name: DATABASE_NAME
          value: my-database
        - name: DATABASE_USER
          value: root
        - name: DATABASE_PASSWORD
          value: P4SSw0rd!
        - name: DATABASE_HOST
          value: /socket/my-database-socket
        - name: DATABASE_URL
          value: user=$(DATABASE_USER) password=$(DATABASE_PASSWORD) dbname=$(DATABASE_NAME) host=$(DATABASE_HOST)

The problem is that when the service is running, the env vars in DATABASE_URL seem not interpolated.

I read that Kubernetes supports dependent env vars but I can't figure out how to make this run in Cloud Run.

I am wondering if it is supported in Cloud Run in the end.

  • Try to put all values into "", its string value. For example : `value: "P4SSw0rd!"` – Thanh Nguyen Van Sep 26 '21 at 02:46
  • I tried many different syntaxes. As you can see the composed environment variables are already double quoted. As E. Anderson wrote it is likely not supported. To do what I want I had to write the whole string as a secret and use it as an env var. – Benjamin Grandfond Sep 26 '21 at 13:37

1 Answers1

1

It's likely this may work in Knative open source (which uses Kubernetes to execute pods) but not on Google Cloud Run (fully hosted), which runs on a proprietary execution engine.

E. Anderson
  • 3,405
  • 1
  • 16
  • 19
  • Yes it seems it is not supported at all. I managed to do what I wanted configuring a secret but it would be better to do it composing an env var. – Benjamin Grandfond Sep 26 '21 at 13:33