Using variables when registering services with consul

Is it possible to use variables when registering services with consul using the CLI and an HCL file?

I’d like to write a service definition such as:

variable "foo" {
  type = string
  default = "bar"
}

service {
  name = var.foo
  port = 7000
}

See the docs: Configuration | Consul by HashiCorp

I quote:

Environment variables cannot be used to configure the Consul client. They can be used when running other consul CLI commands that connect with a running agent, e.g. CONSUL_HTTP_ADDR=192.168.0.1:8500 consul members.

As a hack you could use envsubst in a wrapper before starting Consul to manipulate the configuration file.

1 Like