Hello,
Loving all things Nomad, so work is much appreciated!
I have previously had a look at Levant, and I’m now evaluating nomad-pack. Last time around I ended up writing the job-templates in Terraform over Levant.
As we already use terraform in our environment, I struggling to see any benefit to replacing current terraform-templates with nomad-packs. Even the syntax is very similar.
I see the registries as a nice feature, but terraform has terraform registry serving a similar role.
If anyone has some input as to what can be gained from using nomad-pack vs just using terraform, that would be much appreciated. Would love to have a good reason to use a shiny new tool.
Example TERRAFORM version of the “hello_world” pack below.
FILE: hello_world.nomad.tpl
# =================================================================================================
# CUSTOMIZE JOB (HELPERS)
# =================================================================================================
locals {
region = "${lookup(OPT,"region","global")}"
datacenters = ${jsonencode(lookup(OPT,"datacenters",["dc1"]))}
count = ${lookup(OPT,"count",1)}
# OPT.register_service defaults to false
service_name = "${lookup(OPT,"service_name","hello-world")}"
service_tags = ${jsonencode(lookup(OPT,"service_tags",[]))}
image = "${lookup(OPT,"image","traefik/whoami:latest")}"
name = "${lookup(OPT,"name","")}"
}
# =================================================================================================
# NOMAD JOB
# =================================================================================================
job "${~ JOB_NAME ~}" {
region = local.region
datacenters = local.datacenters
type = "service"
# -------------------------------------------------------------------------------------------------
# GROUP app
# -------------------------------------------------------------------------------------------------
group "app" {
count = local.count
network {
port "http" {
to = 80
}
}
%{~ if lookup(OPT,"register_service",false) ~}
# -------------------------------------------------------------------------------------------------
# CONSUL settings
# -------------------------------------------------------------------------------------------------
service {
name = local.service_name
tags = local.service_tags
port = "http"
check {
name = "alive"
type = "http"
path = "/health"
interval = "10s"
timeout = "2s"
}
}
%{~ endif ~}
restart {
attempts = 2
interval = "30m"
delay = "15s"
mode = "fail"
}
# -------------------------------------------------------------------------------------------------
# TASK server
# -------------------------------------------------------------------------------------------------
task "server" {
driver = "docker"
env {
WHOAMI_NAME = local.name
}
config {
image = local.image
ports = ["http"]
}
}
}
}
FILE: main.tf
# Example variable defining 2 jobs using template
variable nomad_jobs {
type = any
default = {
hello_world1 = {
template = "templates/hello_world.nomad.tpl"
required = {}
optional = {
register_service = true
service_tags = [
"traefik.enable=false",
]
}
}
hello_world2 = {
template = "templates/hello_world.nomad.tpl"
required = {}
}
}
}
# =================================================================================================
# NOMAD JOB(S)
# =================================================================================================
resource nomad_job "JOB" {
for_each = var.nomad_jobs
purge_on_destroy = true
hcl2 {
enabled = true
allow_fs = false
}
jobspec = templatefile(each.value.template,{
JOB_NAME = each.key
REQ = each.value.required,
OPT = lookup(each.value,"optional",{}),
})
}
/* uncomment to render job-file to disk
resource local_file "JOB" {
for_each = var.nomad_jobs
filename = "${path.module}/rendered.${each.key}.nomad"
content = templatefile(each.value.template,{
JOB_NAME = each.key
REQ = each.value.required,
OPT = lookup(each.value,"optional",{}),
})
}
*/