Search code examples
playframeworktypesafe-activator

Play Framework 2.4.x - activator ~run does not substitute env vars in application.conf


I'm running my Play application on heroku. On heroku the idea is to set various settings through env variables. For example AWS S3 secret access key and its ID. That's understandable. So following good practices I put the following settings to my conf/application.conf:

# AWS.
# ~~~~~
aws.accessKeyID = ${?AWS_ACCESS_KEY_ID}
aws.secretAccessKey = ${?AWS_SECRET_ACCESS_KEY}

Ok, all good. Now whenever I run in PROD or DEV mode I can change those and not be worried about hard-coding them. The problem is though that when I run activator ~run to develop my app locally these settings are not being read and set. It's like activator ~run ignores completely substituting options for env variables. I read and print them in the following way when loading my index page:

import play.api.Play.current
...
val v1 = current.configuration.getString("aws.accessKeyID")
val v2 = current.configuration.getString("aws.secretAccessKey")
println("v1=" + v1 + ", v2=" + v2)

Let me give even more details as to what's happening:

floyd@floyd-VirtualBox:~/myproj$ echo $AWS_ACCESS_KEY_ID
access-key-id
floyd@floyd-VirtualBox:~/myproj$ echo $AWS_SECRET_ACCESS_KEY
secret-access-key
floyd@floyd-VirtualBox:~/myproj$ activator ~run
[info] Loading project definition from /home/floyd/myproj/project
[info] Set current project to myproj (in build file:/home/floyd/myproj/)

--- (Running the application, auto-reloading is enabled) ---

[info] p.a.l.c.ActorSystemProvider - Starting application default Akka system: application
[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

[info] Compiling 1 Scala source to /home/floyd/myproj/target/scala-2.11/classes...
[success] Compiled in 15s
[info] - play.api.libs.concurrent.ActorSystemProvider - Starting application default Akka system: application
[info] - application - ReactiveMongoApi starting...
[info] - application - ReactiveMongoApi successfully started with DB 'test'! Servers:
        [localhost:27017]
[info] - play.api.Play - Application started (Dev)
v1=None, v2=None

Both cases None is printed and I would expect to see the value that was stored in the env variables.

Thanks upfront for your help!


Solution

  • I think the answer might be in how you set your Environment variable. To simulate your experience

    With a application.conf of

    foo.bar=${?FOO}

    Then

    $ FOO=BAR
    $ activator ~run
    [info] Loading project definition from /Users/pw/dev/play-project/project
    [info] Set current project to play-project (in build file:/Users/pw/dev/play-project/)
    
    --- (Running the application, auto-reloading is enabled) ---
    
    [info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
    
    (Server started, use Ctrl+D to stop and go back to the console...)
    
    [success] Compiled in 687ms
    [info] - application - Application is started!!! ****
    [info] - play.api.Play - Application started (Dev)
    v1=None
    

    crucially if you export your environment variable like in the following snippet, the intended behaviour is achieved

    $ unset FOO
    $ echo $FOO
    
    $ export FOO=ASDF
    $ activator ~run
    [info] Loading project definition from /Users/pw/dev/play-project/project
    [info] Set current project to play-project (in build file:/Users/pw/dev/play-project/)
    
    --- (Running the application, auto-reloading is enabled) ---
    
    [info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
    
    (Server started, use Ctrl+D to stop and go back to the console...)
    
    [success] Compiled in 1s
    [info] - application - Application is started!!! ****
    [info] - play.api.Play - Application started (Dev)
    v1=Some(ASDF)