Skip to main content

router

router runs HTTP requests, commands, and Massdriver API calls when an event matches a route. Each route selects events by type, attributes, and an optional expression, and runs a list of steps in order. Use it for small reactions that need no code: page someone, post a message, call a cleanup endpoint, run a script, or plan a deployment. Routes are YAML, so router builds your own event destinations without writing an automation.

Settings​

The config file is YAML, at CONFIG_FILE (default /etc/massdriver-automations/config.yaml). router reads it at start. It does not start when the file has no routes, an unknown key, a route without events or steps, a sync route with a timeout over 8s, or an expression that does not compile. config.example.yaml has every key, with comments.

yaml
engine: jq                      # or go; one engine for the whole file
routes:
- name: page-on-prod-failure
events: [deployment.failed]
match:
md-environment: prod
when: '.data.metadata.deployment_action == "PROVISION"'
timeout: 5s
do:
- http:
url: https://events.pagerduty.com/v2/enqueue
body: '{routing_key: env.PAGERDUTY_ROUTING_KEY, event_action: "trigger", dedup_key: .id,
payload: {summary: "\(.data.attributes["md-instance"]) failed", source: "massdriver", severity: "error"}}'
KeyDefault
enginejqjq or go. Every expression in the file uses this engine.
routes[].nameRequired. Unique. Shown in the logs.
routes[].eventsRequired. Event types, such as deployment.failed. instance.* selects every event type of one entity.
routes[].matchAttributes that the event's entity must have, each with exactly this value.
routes[].whenAn expression. The route runs only when jq outputs true, or the Go template renders true.
routes[].asyncfalseSee Sync and async routes.
routes[].timeout8s, or 1m when asyncThe time for all steps of the route. 8s is also the largest value for a sync route.
routes[].doRequired. The steps. Each step has exactly one of http, exec, and massdriver.

More than one route can match an event. Each matching route runs.

In the bundle, Routes (YAML) is a text field that holds the whole config file. Scripts holds files for exec steps, by file name, mounted as executables in /scripts.

Steps​

Steps run in order. A step that fails stops its route, and the next steps of that route do not run. A failure is an expression error, a response other than 2xx, a command that exits with a status other than 0, a GraphQL error, a mutation result with successful: false, or the route timeout.

http​

yaml
- http:
method: POST # default POST
url: 'https://dns.example.com/records/\(.data.attributes["md-instance"])'
headers:
Authorization: 'Bearer \(env.DNS_TOKEN)'
body: '{instance: .data.attributes["md-instance"], event: .id}'
Fieldjqgo
url, headers valuesText. Each \(expr) is replaced with the output of the jq expression.A template that renders the text.
bodyAn expression. Its output is sent as JSON.A template. Its text is sent as it is.

A step with a body sends Content-Type: application/json. Set the header to send another type. The step fails on a response other than 2xx.

exec​

yaml
- exec: [/scripts/purge-bucket.py, '\(.data.entity.identifier)']

The program and its arguments. Each argument is text with \(expr) interpolation under jq, or a template under go. No shell runs the command: use [/bin/sh, -c, '...'] for one. The command gets the event as JSON on standard input, and these environment variables:

Variable
MD_EVENT_ID, MD_EVENT_TYPE, MD_EVENT_SUBJECT, MD_EVENT_TIMEFrom the event.
MD_ATTR_<KEY>Each attribute. The key is in upper case, with _ in place of other characters: md-instance is MD_ATTR_MD_INSTANCE.
MD_METADATA_<KEY>Each metadata field, such as MD_METADATA_DEPLOYMENT_ACTION.

The command also gets the environment of router, without MASSDRIVER_SIGNING_SECRET and MASSDRIVER_API_KEY. The default image has no shell and no tools. Commands need the -scripts image.

massdriver​

yaml
- massdriver:
query: |
mutation PlanProposal($org: ID!, $id: UUID!) {
planDeployment(organizationId: $org, id: $id) {
successful
messages { field message }
}
}
variables: '{id: .data.entity.id}'

Runs one operation against the V2 GraphQL API as the service account of MASSDRIVER_API_KEY. variables is an expression that outputs an object under jq, or a template that renders a JSON object under go. When the query uses $org and variables does not set org, router sets it to MASSDRIVER_ORGANIZATION_ID. Map inputs, such as params, are JSON strings: use tojson in jq, or json (json .x) in a template.

The step fails on a GraphQL error, or when a field of data has successful: false. The logs show the messages.

Expressions​

Every expression sees the event as Massdriver sends it:

json
{
"id": "4b0b5a8e-0f6c-4a53-9d0c-5a1f4ad5c1c2",
"type": "deployment.failed",
"subject": "mri:acme:api:prod:database",
"time": "2026-11-24T05:54:03Z",
"data": {
"entity": {"id": "b9dc3d18-...", "status": "FAILED", "action": "PROVISION", "version": "1.2.3", "deployed_by_name": "Jane Doe", "message": "Bump the instance size"},
"attributes": {"md-project": "api", "md-environment": "prod", "md-component": "database", "md-instance": "api-prod-database", "md-repo": "aws-aurora", "md-bundle": "aws-aurora@1.2.3"},
"metadata": {"deployment_action": "PROVISION"},
"actor": {"account_id": "0e6f2c1a-...", "name": "Jane Doe", "organization_id": "5a2c8e4f-..."}
}
}

The event destination docs list each event type and its entity.

jq uses gojq. A when, body, or variables expression uses its first output. env.NAME and $ENV.NAME read the environment of router, without MASSDRIVER_SIGNING_SECRET and MASSDRIVER_API_KEY. Each evaluation stops after 2 seconds. In YAML, put jq in single quotes or a block (|): \( is not valid in a double-quoted YAML string.

Valuejq
Event type.type
Instance, environment, project.data.attributes["md-instance"], .data.attributes["md-environment"], .data.attributes["md-project"]
Bundle and version.data.attributes["md-bundle"] (aws-aurora@1.2.3)
Deployment action (PROVISION, PLAN, DECOMMISSION).data.metadata.deployment_action
Deployment status, message, and who deployed.data.entity.status, .data.entity.message, .data.entity.deployed_by_name
Identifier of an instance or environment event.data.entity.identifier
Person or service account that acted.data.actor.name
Value with a default.data.entity.message // "No message"
Event type of one entity.type \| startswith("deployment.")

go uses Go text/template. A field that the event does not have stops the step with an error. A when template must render true; spaces around it are ignored.

Function
attr "md-instance"An attribute of the event's entity. Use it for keys with -.
json .data.entityThe value as JSON. Use it to put values in a JSON body or variables.
env "DNS_TOKEN"An environment variable of router. Empty for MASSDRIVER_SIGNING_SECRET and MASSDRIVER_API_KEY.

No branching​

A route is a list of steps and nothing else. There are no conditions between steps, no loops, no values passed from one step to the next, and no state between events. When a reaction needs any of these, write a script and run it with exec, or write a Go automation.

Sync and async routes​

A route is sync unless it sets async: true.

  • Sync: router runs the route before it responds to Massdriver. The sync routes of an event run at the same time, each within its timeout of at most 8s, because Massdriver waits 10 seconds. When a sync route fails, router responds 500 and Massdriver sends the event again. The retry runs only the routes that failed; routes that succeeded for that event id in the last hour do not run again.
  • Async: router queues the route and responds. A failed async route is logged, and nothing runs it again. Use async for steps that take longer than 8s.

Massdriver delivers each event at least once, and a restart forgets which routes ran. Write every step so that running it twice has the same effect as running it once: use the event id as a dedup key, PUT and DELETE in place of POST, and scripts that check before they change.

Examples​

examples/ has each of these files.

Clean up after a decommission (Go templates)​

yaml
engine: go
routes:
- name: release-dns
events: [deployment.completed, instance.deleted]
match:
md-environment: prod
when: '{{ or (eq .type "instance.deleted") (eq .data.metadata.deployment_action "DECOMMISSION") }}'
timeout: 5s
do:
- http:
method: DELETE
url: 'https://dns.example.com/records/{{ attr "md-instance" }}'
headers:
Authorization: 'Bearer {{ env "DNS_TOKEN" }}'
body: '{"instance": {{ json (attr "md-instance") }}, "event": {{ json .id }}}'

- name: purge-environment-bucket
events: [environment.deleted]
async: true
timeout: 5m
do:
- exec: [/scripts/purge-bucket.py, '{{ .data.entity.identifier }}']

or stops at the first true value, so an instance.deleted event, which has no deployment_action, does not reach the second eq.

Page PagerDuty when a production deployment fails​

yaml
routes:
- name: page-on-prod-failure
events: [deployment.failed]
match:
md-environment: prod
when: '.data.metadata.deployment_action == "PROVISION"'
timeout: 5s
do:
- http:
url: https://events.pagerduty.com/v2/enqueue
body: |
{
routing_key: env.PAGERDUTY_ROUTING_KEY,
event_action: "trigger",
dedup_key: .id,
payload: {
summary: "Deployment of \(.data.attributes["md-instance"]) failed",
source: .data.attributes["md-instance"],
severity: "error",
custom_details: {
bundle: .data.attributes["md-bundle"],
deployed_by: .data.entity.deployed_by_name,
message: .data.entity.message
}
}
}

Post proposed deployments to Microsoft Teams​

yaml
routes:
- name: teams-on-proposal
events: [deployment.proposed]
timeout: 5s
do:
- http:
url: '\(env.TEAMS_WEBHOOK_URL)'
body: |
{
type: "message",
attachments: [{
contentType: "application/vnd.microsoft.card.adaptive",
content: {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
type: "AdaptiveCard",
version: "1.4",
body: [
{type: "TextBlock", weight: "Bolder", text: "\(.data.entity.deployed_by_name) proposed \(.data.entity.action) of \(.data.attributes["md-instance"])"},
{type: "TextBlock", wrap: true, text: (.data.entity.message // "No message")}
]
}
}]
}

Plan each proposed production deployment​

When a deployment to a production instance is proposed, this route plans it with planDeployment, so the reviewer sees the changes before approving it. The plan uses the params of the proposed deployment and changes nothing. The service account needs instance:plan on the instance.

yaml
routes:
- name: plan-proposed-deployments
events: [deployment.proposed]
match:
md-environment: prod
when: '.data.metadata.deployment_action == "PROVISION"'
timeout: 5s
do:
- massdriver:
query: |
mutation PlanProposal($org: ID!, $id: UUID!) {
planDeployment(organizationId: $org, id: $id) {
successful
messages { field message }
result { id status }
}
}
variables: '{id: .data.entity.id}'

Secrets​

Variable
The credentials that routes read with envSuch as PAGERDUTY_ROUTING_KEY. In the bundle, the ROUTER_ENV secret is a JSON object of them, such as {"PAGERDUTY_ROUTING_KEY": "..."}, and each key becomes a variable.

MASSDRIVER_ORGANIZATION_ID and MASSDRIVER_API_KEY are required only when a route has a massdriver step. The MASSDRIVER_* variables are in the common settings.

Permissions​

The service account needs the permissions of the operations in the massdriver steps, such as instance:plan for planDeployment. Without massdriver steps, it needs none. Deployed with the bundle, it also needs organization:manageEventDestinations.

Host​

The bundle serves router at Ingress > Host, such as router.automations.acme.com. On ECS, an empty host uses router.<domain of the cluster's hosted zone>. /hooks/massdriver must be public. Refer to Deploy.

Event destination​

The bundle creates a destination for the event types of every route, with no attributes and no condition. Each route filters with its own match and when.

To create it yourself, with mass 2.6.0 or later, add one --event-type for each type in the events of your routes. For the PagerDuty example:

shell
mass event destination create \
--name router \
--url https://router.example.com/hooks/massdriver \
--event-type deployment.failed \
--attribute md-environment=prod \
--condition '.data.metadata.deployment_action == "PROVISION"'

An --attribute or --condition on the destination applies to every route. Use them only when every route has the same filter, and keep the route's own match and when as well. Massdriver marks the events that the filter drops as SKIPPED and does not send them.

Copy the signing secret from the destination's Overview tab into MASSDRIVER_SIGNING_SECRET.

Run​

shell
docker run -p 8080:8080 \
-v "$PWD/config.yaml:/etc/massdriver-automations/config.yaml:ro" \
-e MASSDRIVER_SIGNING_SECRET=whsec_... \
-e PAGERDUTY_ROUTING_KEY=... \
ghcr.io/massdriver-cloud/massdriver-automations:0.3 run router

The -scripts image​

ghcr.io/massdriver-cloud/massdriver-automations:<tag>-scripts has the same binary on Alpine Linux with sh, jq, curl, and python3. It runs as user 65532 and runs router by default. Use it for exec steps:

shell
docker run -p 8080:8080 \
-v "$PWD/config.yaml:/etc/massdriver-automations/config.yaml:ro" \
-v "$PWD/scripts:/scripts:ro" \
-e MASSDRIVER_SIGNING_SECRET=whsec_... \
ghcr.io/massdriver-cloud/massdriver-automations:0.3-scripts

Each script must be executable and start with a #! line, such as #!/usr/bin/env python3. For other tools, build your own image FROM ghcr.io/massdriver-cloud/massdriver-automations:<tag>-scripts.

The bundle runs the -scripts image when a route has an exec step or Scripts has a file. To use your own image, push it with the same -scripts tag to your own repository and set Image > Repository. On ECS, the image needs an ARM64 variant.

Limits​

  • A failed step logs its HTTP status or exit status at error, with the last 512 bytes of its output. With LOG_LEVEL=debug, every step logs its output, up to 4 KiB. Scripts and the services they call must not print secrets. Errors in expressions name the expression's field, not the values it read.
  • The async queue holds 100 events, in memory. When it is full, router responds 500, and Massdriver sends the event again later. A restart loses the queue.
  • router remembers the routes that ran for each event id for one hour, in memory. Run one replica.
  • Go templates have no evaluation timeout. A template cannot loop without end, because it can only range over the event.