Running Traefik using nomad-pack

How can I get variables to nomad-pack using Consul or environment variables?

To run Traefik with nomad-pack, I need to pass the following variables to Nomad:

  • CONSUL_HTTP_TOKEN
  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_REGION

The AWS variables need to be define as environment variables for the Traefik task. I therefore added this to the Traefik pack (traefik.nomad.tpl):

      env {
        [[- range $var := .traefik.traefik_env_vars ]]
        [[ $var.key ]] = "[[ $var.value ]]"
        [[- end ]]
      }  

and the define traefik_env_vars as:

traefik_env_vars = [
    {key = "AWS_ACCESS_KEY_ID", value = "${AWS_ACCESS_KEY_ID}"},
    {key = "AWS_SECRET_ACCESS_KEY", value = "${AWS_SECRET_ACCESS_KEY}"},
    {key = "AWS_REGION", value = "${AWS_REGION}"}
  ]

Now, the question is how I can get the value of these variables to nomad-pack. I tried to use Consul key values but it did not work for me. I have not seen an example.

Hi @bsamadi

Thanks for using Nomad and Nomad Pack! There are detailed instructions on how to pass variables to a pack in the usage guide. The short answer is you can provide variables defined for you template either as command line flags or via a file. Take a look and let us know if you have any further questions. Happy Hacking!

Derek and the Nomad Team

Thank you @DerekStrickland. I don’t want to commit AWS credentials to the repository. Using the --var option helps me use environment variables easily. However, it is not convenient for multi-line variables (files). I needed a way to use environment variables in a var-file. I ended up using envsubst. It is OK but I wanted to make sure that I wasn’t missing anything.

I wish I could use "[[ env "CONSUL_HTTP_TOKEN" ]]" with nomad-pack similar to Levant.

So something like this example from the community registry? The syntax is slightly different, but it looks like what you are trying to achieve. There are tons of great community examples in that repo if you haven’t checked it out.

listeners.ssl.default = {{ env "NOMAD_PORT_amqp" }}

Thank you @DerekStrickland. I tried it and it did not work. Does it support general environment variables? How can I use variables stored in Consul kv?

It is very difficult to find examples. There are many packs but few examples. The example for Traefik is not helpful because there is no SSL certificate resolver.