Search code examples
dockerrustmacrosgoogle-cloud-run

Appropriate way to access environment variables in google cloud run (docker) with rust


I built an API in rust and am deploying it via Google Cloud Run. Within my API I access a URI for a remote database stored as an environment variable.

When I run the program on my local computer the environment variable can be accessed using env!("MONGODB_URI_2").

When I deploy the program to gcloud with the flag --set-env-vars "MONGODB_URI_2", "theurigoeshere" the same env macro can't find the variable and panics.

Edit: I tried removing the underscores from the key and making the text all lowercase before posting this, it didn't help..


Solution

  • It looks like --set-env-vars expects = as the separator between key and value, while , separates multiple variables:

    gcloud run deploy --set-env-vars MONGODB_URI_2=theurigoeshere
    

    Also note that the env! macro gives you compile-time environment variables, so if you want runtime environment variables, use std::env::var. It looks like compile-time environment variables will work here, but it's a common mistake so it's important to keep in mind.