Reference Fields of Environment Variable Object in YAML File

42 Views Asked by At

I am using AWS App Runner to host my application, and to do this I am pushing a Docker image to Elastic Container Registry, from where it is read by App Runner.

I am using AWS Secrets Manager to store the host, username, and password of my DB. I am injecting this data as a single environment variable called DB_CREDENTIALS, which is an object containing key-value pairs, so you can imagine the structure is something like

{
  "host": "foo",
  "username": "bar",
  "password": "baz",
}

The data contained in this environment variable is being used in a YAML file for the application DB config:

spring:
  datasource:
    url: jdbc:postgresql://${DB_CREDENTIALS.HOST:localhost}:5432/
    username: ${DB_CREDENTIALS.USERNAME:postgres}
    password: ${DB_CREDENTIALS.PASSWORD:password}

The way I am reading the env variable right now^ is that I'm trying to reference the fields of the injected object from the Secrets Manager and hoping that YAML parses it / references the key directly to get the value from the environment variable (so if DB_CREDENTIALS contains key-value pairs, then .HOST should look at the key "host" of the DB_CREDENTIALS object and get its value "foo" from there).

The environment variable isn't being read automatically by the YAML file. The logs on application start indicate that the YAML file treats the environment variable like it doesn't even exist / have any data, so it uses the default values specified in the YAML file for each of the fields (i.e. 'HOST' uses "localhost", 'USERNAME' uses "postgres", 'PASSWORD' uses "password", instead of "foo", "bar", and "baz" respectively).

Is there a way to correctly reference the fields of the environment variable object in the YAML file (with or without the use of parsing logic) so that the application DB uses the configs from the AWS Secrets Manager?

I am developing in Kotlin using the Spring Boot framework.

0

There are 0 best solutions below