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

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel
Get started for free

Enter your email to get $200 in credit for your first 60 days with DigitalOcean.

New accounts only. By submitting your email you agree to our Privacy Policy.

© 2023 DigitalOcean, LLC.