Nomad Job
The nomad_job resource applies one or more Nomad job files to a Nomad cluster. It monitors changes to job files and automatically recreates the resource when changes are detected.
HCL Syntax
Section titled “HCL Syntax”Basic Syntax
Section titled “Basic Syntax”resource "nomad_job" "name" { cluster = resource.nomad_cluster.dev paths = ["./jobs/web.nomad"]}
Full Syntax
Section titled “Full Syntax”resource "nomad_job" "name" { cluster = resource.nomad_cluster.production
paths = [ "./jobs/web.nomad", "./jobs/api.nomad", "./jobs/workers/" ]
health_check { timeout = "60s" jobs = ["web", "api", "worker"] }}
Fields
Section titled “Fields”Field | Type | Required | Description |
---|---|---|---|
cluster | reference | ✓ | Reference to a nomad_cluster resource |
paths | list(string) | ✓ | Paths to Nomad job files or directories |
health_check | block | Health check configuration for job deployment |
Health Check Configuration
Section titled “Health Check Configuration”Optional health check to verify job deployment success.
Field | Type | Required | Description |
---|---|---|---|
health_check | block | Health check configuration | |
↳ timeout | string | ✓ | Timeout duration (e.g., ”60s”, “5m”) |
↳ jobs | list(string) | ✓ | Names of jobs to monitor for health |
Computed Attributes
Section titled “Computed Attributes”These attributes are set by the system after job deployment:
Field | Type | Description |
---|---|---|
job_checksums | list(string) | Checksums of deployed job files |
Validation Rules
Section titled “Validation Rules”- All paths are made absolute relative to the config file location
- Referenced cluster must exist and be healthy
- Job files must be valid Nomad job specifications
- Health check timeout must be a valid Go duration string
Examples
Section titled “Examples”Simple Job Deployment
Section titled “Simple Job Deployment”resource "nomad_cluster" "dev" { network { id = resource.network.main }}
resource "nomad_job" "web" { cluster = resource.nomad_cluster.dev paths = ["./jobs/nginx.nomad"]}
Multiple Jobs with Health Check
Section titled “Multiple Jobs with Health Check”resource "nomad_job" "app_stack" { cluster = resource.nomad_cluster.production
paths = [ "./jobs/web.nomad", "./jobs/api.nomad", "./jobs/database.nomad" ]
health_check { timeout = "120s" jobs = ["web", "api", "database"] }}
Directory-based Job Deployment
Section titled “Directory-based Job Deployment”resource "nomad_job" "microservices" { cluster = resource.nomad_cluster.dev
paths = [ "./jobs/microservices/", # Directory containing multiple .nomad files "./jobs/shared/redis.nomad" ]
health_check { timeout = "90s" jobs = ["user-service", "order-service", "payment-service", "redis"] }}
Job with Service Configuration
Section titled “Job with Service Configuration”Example Nomad job file that might be deployed:
## ./jobs/web.nomadjob "web" { datacenters = ["dc1"] type = "service"
group "web" { count = 3
network { port "http" { to = 80 } }
task "nginx" { driver = "docker"
config { image = "nginx:latest" ports = ["http"] }
service { name = "web" port = "http"
check { type = "http" path = "/" interval = "10s" timeout = "2s" } }
resources { cpu = 100 memory = 128 } } }}
And the corresponding lab configuration:
resource "nomad_job" "web" { cluster = resource.nomad_cluster.app paths = ["./jobs/web.nomad"]
health_check { timeout = "60s" jobs = ["web"] }}
Batch Job Example
Section titled “Batch Job Example”job "data-processor" { datacenters = ["dc1"] type = "batch"
group "processor" { count = 1
task "process" { driver = "docker"
config { image = "myapp/data-processor:latest" args = ["--input", "/data/input", "--output", "/data/output"] }
resources { cpu = 500 memory = 512 } } }}
resource "nomad_job" "batch_processor" { cluster = resource.nomad_cluster.processing paths = ["./jobs/data-processor.nomad"]
health_check { timeout = "30s" jobs = ["data-processor"] }}
Job File Management
Section titled “Job File Management”The nomad_job resource supports various ways to organize job files:
Single Job File
Section titled “Single Job File”paths = ["./jobs/single-app.nomad"]
Multiple Specific Files
Section titled “Multiple Specific Files”paths = [ "./jobs/web.nomad", "./jobs/api.nomad", "./jobs/worker.nomad"]
Directory of Jobs
Section titled “Directory of Jobs”paths = ["./jobs/"] # All .nomad files in directory
Mixed Approach
Section titled “Mixed Approach”paths = [ "./jobs/core/", # Directory "./jobs/addon/cache.nomad", # Specific file "./jobs/addon/queue.nomad" # Another specific file]
Best Practices
Section titled “Best Practices”- Job Organization: Group related jobs in directories for easier management
- Health Checks: Always configure health checks for service jobs
- Resource Limits: Set appropriate CPU and memory limits in job specifications
- Service Discovery: Use Consul Connect or service mesh for inter-service communication
- Rollback Strategy: Keep previous job versions for quick rollbacks
- Monitoring: Include health checks and monitoring in job specifications
- Dependencies: Use job dependencies when services rely on each other
Common Use Cases
Section titled “Common Use Cases”- Web Applications: Deploying web services with load balancing
- Microservices: Managing multiple interconnected services
- Batch Processing: Running data processing or ETL jobs
- Background Workers: Queue processors and scheduled tasks
- Development Environment: Rapid deployment for testing and development
Integration with Other Resources
Section titled “Integration with Other Resources”With Service Resources
Section titled “With Service Resources”resource "nomad_job" "api" { cluster = resource.nomad_cluster.app paths = ["./jobs/api.nomad"]}
resource "service" "api" { name = "api" port = 8080
target { resource = resource.nomad_cluster.app named_port = "http" config = { job = "api" group = "api" task = "server" } }}
With Ingress Resources
Section titled “With Ingress Resources”resource "ingress" "web" { port = 80
target { resource = resource.nomad_cluster.app named_port = "http" config = { job = "web" group = "frontend" task = "nginx" } }}