Skip to content

Instantly share code, notes, and snippets.

@arodd
Last active December 3, 2021 01:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arodd/577377ef4f700735f50783c692f35c58 to your computer and use it in GitHub Desktop.
Save arodd/577377ef4f700735f50783c692f35c58 to your computer and use it in GitHub Desktop.
Nomad HCL2 Job Sample w/ Connect Upstreams
variable "api_group_count" {
type = number
default = 1
}
variable "dash_group_count" {
type = number
default = 1
}
variable "datacenters" {
type = list(string)
default = [
"dev-us-west-2",
"prod-us-west-2",
"us-west-2",
]
}
variable "env" {
type = string
default = "prod"
}
variable "releases_api_image" {
type = string
default = "hashicorpnomad/counter-api"
}
variable "releases_api_image_tag" {
type = string
default = "v3"
}
variable "releases_dash_image" {
type = string
default = "hashicorpnomad/counter-dashboard"
}
variable "releases_dash_image_tag" {
type = string
default = "v3"
}
job "releases-api" {
id = "releases-api-${var.env}"
name = "releases-api-${var.env}"
type = "service"
datacenters = var.datacenters
update {
max_parallel = 1
health_check = "checks"
min_healthy_time = "2m"
healthy_deadline = "5m"
progress_deadline = "10m"
auto_revert = true
auto_promote = true
canary = 1
stagger = "2m"
}
migrate {
max_parallel = 1
health_check = "checks"
min_healthy_time = "2m"
healthy_deadline = "10m"
}
group "api" {
count = var.api_group_count
shutdown_delay = "1m"
network {
mode = "bridge"
}
service {
name = "count-api"
port = "9001"
connect {
sidecar_service {}
}
tags = [
"environment=${var.env}",
"backend"
]
# check {
# type = "http"
# interval = "30s"
# timeout = "5s"
# path = "/health"
# }
}
task "api" {
driver = "docker"
shutdown_delay = "1m"
config {
image = "${var.releases_api_image}:${var.releases_api_image_tag}"
labels = {
# Reference: https://docs.datadoghq.com/agent/logs/advanced_log_collection/?tab=docker
"com.datadoghq.ad.logs" = jsonencode([{
source = "go"
service = "releases-api"
}])
}
}
env {
DD_ENV = var.env
DD_RUNTIME_METRICS_ENABLED = "true"
DD_SERVICE = "count-api"
DD_TAGS = "source:go,env:${var.env}"
DD_VERSION = var.releases_api_image_tag
}
vault {
namespace = "team/unit1"
policies = [
"default",
"releases-api-${var.env}",
]
}
template {
destination = "${NOMAD_SECRETS_DIR}/.aws/credentials"
data = <<-CREDS
{{ with secret "aws/sts/releases-api-${var.env}-s3-access" "ttl=60m" }}
[s3]
aws_access_key_id = {{ .Data.access_key }}
aws_secret_access_key = {{ .Data.secret_key }}
aws_session_token = {{ .Data.security_token }}
{{ end }}
{{ with secret "aws/sts/releases-api-${var.env}-db-access" "ttl=60m" }}
[db]
aws_access_key_id = {{ .Data.access_key }}
aws_secret_access_key = {{ .Data.secret_key }}
aws_session_token = {{ .Data.security_token }}
{{ end }}
CREDS
}
}
}
group "dashboard" {
count = var.dash_group_count
shutdown_delay = "1m"
network {
mode = "bridge"
port "http" {
static = 9002
to = 9002
}
}
service {
name = "count-dashboard"
port = "http"
connect {
sidecar_service {
proxy {
upstreams {
destination_name = "${var.env}-count-api"
local_bind_port = 8080
}
}
}
}
}
tags = [
"environment=${var.env}",
"frontend"
]
# check {
# type = "http"
# interval = "30s"
# timeout = "5s"
# path = "/health"
# }
task "dashboard" {
driver = "docker"
shutdown_delay = "1m"
config {
image = "${var.releases_dash_image}:${var.releases_dash_image_tag}"
labels = {
# Reference: https://docs.datadoghq.com/agent/logs/advanced_log_collection/?tab=docker
"com.datadoghq.ad.logs" = jsonencode([{
source = "go"
service = "releases-api"
}])
}
}
env {
COUNTING_SERVICE_URL = "http://${NOMAD_UPSTREAM_ADDR_count_api}"
DD_ENV = var.env
DD_RUNTIME_METRICS_ENABLED = "true"
DD_SERVICE = "dashboard"
DD_TAGS = "source:go,env:${var.env}"
DD_VERSION = var.releases_dash_image_tag
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment