Question

MongoDB CA_CERT environment variable not being evaluated

I have an app spec that defines a database and a service, and the service defines two environment variables, DATABASE_URL and CA_CERT, that it expects to be supplied by the database:

name: grocery-list
region: sfo

databases:
- cluster_name: my-cluster-name
  engine: MONGODB
  name: db
  production: true

services:
- build_command: npm install && npm run build
  envs:
  - key: DATABASE_URL
    scope: RUN_TIME
    value: ${db.DATABASE_URL}
  - key: CA_CERT
    scope: RUN_TIME
    value: ${db.CA_CERT}
  github:
    branch: main
    deploy_on_push: true
    repo: my-org/my-repo
  http_port: 8000
  instance_count: 1
  instance_size_slug: basic-xxs
  name: backend
  routes:
  - path: /api
    preserve_path_prefix: true
  run_command: npm start
  source_dir: /backend

However, when I start the app, only DATABASE_URL seems to be getting evaluated. Here’s a snippet from the Console tab of my running backend service:

apps@backend-7cfd7d7588-4xj9z:~/backend$ env

HOSTNAME=backend-7cfd7d7588-4xj9z
...
DATABASE_URL=mongodb+srv://doapps-[redacted]:[redacted]@cluster-[redacted].mongo.ondigitalocean.com/admin?authSource=admin&tls=true
CA_CERT=${db.CA_CERT}
...

What am I doing wrong, here? Why isn’t ${db.CA_CERT} being provided as the docs say it should be?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
December 22, 2022

Hi there,

Does it work if you try to use process.env.CA_CERT directly?

An alternative you can write the certificate to a file before your app is started.

Let’s say your run command is npm run start, replace it with the following:

echo "$CA_CERT" >mongodb.ca.crt
npm run start

Then, in your code, you can reference the mongodb.ca.crt file.

Let me know how it goes!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up