We derived the arrow backwards

At Mixmax we generate a service catalog by parsing Terraform. Every service in it carries a consumes[] array, and a queue listed there claims one specific thing, that the service is triggered by that SQS queue or drains it. That array answers the question people ask the catalog most often, "what triggers service X?". Engineers ask it directly, and so do the LLM agents we point at the catalog when they need to work out what a code change touches.

A teammate asked what triggers a Lambda that categorises incoming messages. He got two queues back. One was right. The other was a queue that the Lambda only writes to, drained by a different downstream Lambda that generates draft replies.

He was right to flag it, and the report was more interesting than a one-line patch, so I turned it into an audit of every queue claim in the catalog.

A backwards edge is worse than no edge

When you derive a graph from infrastructure code, the hard part is not deciding whether an edge exists. Two resources sitting in the same directory are related somehow, and you will usually notice. The hard part is which way the edge points.

A missing edge is a gap. Someone asks the catalog a question, gets an incomplete answer, and usually notices, because they already knew a piece was missing. A reversed edge is a confident lie. It says a service is downstream of something it is upstream of, and nothing about the answer looks wrong.

For a trigger graph that is worse than usual. The whole point of "what triggers X" is to reason about what happens when a message lands. Get the direction wrong and you have inverted cause and effect in the one field designed to express it.

One function, one missing check

The cause was small. A function in the program that derives the catalog walked every service's Terraform directory, found each module "..." { source = ".../modules/sqs" } block, resolved the queue name, and recorded that queue in consumes[]. The only check was whether the name resolved.

It looked roughly like this, with serviceKind being either lambda or worker.

for (const block of sqsModulesIn(serviceDir)) {
  const queue = queueFromModule(block);
  if (queue) {
    consumes.push({ channel: 'queue', delivery: deliveryFor(serviceKind), ...queue });
  }
}

Co-location was doing all the work. If a queue was declared next to a service, that service consumed it. Nothing checked whether anything actually wired the queue to the service.

The Lambda in the report declares two queues in one directory. Cleaned up, that directory looks like this.

module "input_queue" {
  source = "../../modules/sqs"
  name   = "messageCategoriser"
}

module "output_queue" {
  source = "../../modules/sqs"
  name   = "messageCategoriser-Categorised"
}

resource "aws_lambda_event_source_mapping" "input" {
  event_source_arn = module.input_queue.arn
  function_name    = aws_lambda_function.categoriser.arn
}

There is one event-source mapping, which is the AWS resource that actually wires a queue to a function, and it binds one of the two queues. The second queue's only relationship to this Lambda is an IAM statement granting sqs:SendMessage, with a comment above it saying in plain words that the Lambda is allowed to send to it. The catalog read both queues as inbound and labelled both as triggers.

Auditing all 301 claims, not a sample

I did not want to patch the one case. A reversed edge that nobody caught for months means the derivation rule is wrong, and the interesting number is how often it fires.

So the audit covered all 301 queue-consume claims in the catalog. One agent per repository read the Terraform behind each claim on main and checked whether any resource actually binds that queue to that service. Every suspected false positive then went to a second agent whose only job was to overturn the first one by finding a binding it had missed. A claim counted as confirmed only if the first agent flagged it and the second failed to overturn it.

Eleven confirmed false positives out of 301, about 3.7 percent. Zero overturned. They fell into four kinds.

Six were publish targets, the same case as the original report. The service sends to the queue and never reads it, and the real consumer is a different service that does have the binding.

Two were provisioned but unused. In one of them the consumer registration and the subscription feeding the queue had both been commented out. The queue and its IAM grant survive that deletion, so from Terraform alone the deletion is invisible.

One was a stale attribution in a central registry of consumers, naming a service that no longer drains the queue while a sibling service actually does.

The last two are my favourite, because they get the direction right and the queue wrong. The Terraform declares two queues, a Standard "intermediate" queue and a FIFO queue. An EventBridge rule writes into the intermediate queue. An EventBridge Pipe reads that queue and forwards into the FIFO queue. The Lambda's only event-source mapping binds the FIFO queue.

The intermediate queue is drained by the Pipe, and the Pipe's role is the one holding ReceiveMessage and DeleteMessage on it. The Lambda never touches it. Co-location cheerfully reported it as a second trigger.

The fix everyone reaches for, and why it fails in both directions

My first design instinct, and I suspect yours, was to read the IAM grants. A service consumes a queue if its role has sqs:ReceiveMessage on that queue, and publishes to it if the grant is sqs:SendMessage. The direction is right there in the permission. It even reads like a nice property, the infrastructure declaring its own intent.

It is wrong in both directions, and I only found that out by testing it against the real cases instead of reasoning about it.

It produces false negatives on real consumers. The Lambda from the original report is a true consumer of its input queue, and there is no per-queue ReceiveMessage statement for it anywhere in its Terraform. It holds that permission through the AWS managed execution role for SQS-triggered Lambdas. A second Lambda in a different repository had the same shape. A rule that reads IAM would have dropped both of them, and dropped them precisely because they are wired the standard way.

It also produces false positives on real publishers. One service holds broad ReceiveMessage on queues it only sends to, because the grant was written once at a coarser resource scope. An IAM-reading rule keeps those entries, which are exactly the ones I was trying to remove.

Cloud permissions say what a principal is allowed to do, shaped by managed policies and by whoever wrote the statement in a hurry. They do not say what it does. A rule built on them fails in both directions at once, and tidier policy writing would not fix that.

The signal that holds, and the resolver that nearly reversed the fix

For a Lambda, the reliable signal is the event-source mapping, full stop. It is the resource that actually wires a queue to a function. If no mapping in the directory binds the queue, the Lambda is not triggered by it.

Long-running workers have no such resource. They drain their queues in application code, so Terraform carries no structural evidence either way. That decided the scope. Apply the new check to Lambdas and leave workers exactly as they are.

const mappedQueues = serviceKind === 'lambda' ? eventSourceMappedQueues(serviceDir) : null;

// ... in the same loop over the service's queue modules:

if (queue && (mappedQueues === null || mappedQueues.has(queue.physicalName))) {
  consumes.push({ channel: 'queue', delivery: deliveryFor(serviceKind), ...queue });
}

A null set means no check applies, and the result is byte-identical to the current behaviour. The check can only ever remove a queue that a Lambda declares in its own directory. It can never add one back, and it cannot touch anything else.

There was already a parser for event-source mappings in the same file, added earlier for a different purpose, and reusing it looked free. It matched event_source_arn only in the data.aws_sqs_queue.<x>.arn form, which is how you reference a queue defined in another service.

Then I counted. All 44 of those inline Lambda queue claims in the catalog bind through a module output ARN, module.<q>.arn, and none of them through a data source. Building the set of mapped queues from the existing parser would have produced an empty set in every single directory, and the check would then have dropped all 40 real triggers. A 40-queue regression, shipped as a bug fix, with the four intended removals hidden inside it.

The resolver I actually needed, the one that handles both ARN forms, already existed elsewhere in the same file, written for EventBridge rule targets. Factoring that out and reusing it was the difference between a fix that removes four wrong entries and one that removes forty right ones.

I then ran the check over all 44 claims, with a second agent re-checking each proposed drop. Forty kept, four dropped, zero regressions, and the four drops are exactly the four intended ones. The class I was most worried about was Lambdas whose own names contain the word "publisher", since a name like that is a decent guess at a publish-only service. All of them were kept, because all of them are event-source-mapped and are real consumers as well.

Why it is still on the backlog

In 10 of the 11 cases the correct answer was already in the catalog, sitting next to the false one. Asking who consumes one of those queues returns the real consumer plus one extra entry. That is noise on top of a right answer, not a wrong answer standing alone. Exactly one case, the stale registry attribution, actually corrupts a result. It names the wrong sibling service and omits the real one.

For the publish-target cases, removing the consume entry deletes a wrong claim without recording the right one. What is true is that the service produces to that queue, and that belongs on the producer side of the model, which we build separately. Fixing it on the consume side would have been a second mechanism competing to express the same fact, which is how derived graphs start disagreeing with themselves.

So the fix is specced, the patch is three edits in one file, the regression set is enumerated down to the four exact entries it removes, and it is sitting on the backlog rather than in main. The honest reason is that a 3.7 percent rate of extra but harmless entries on one field lost the prioritisation argument to the work that adds the missing producer edges, which fixes the same cases from the right direction and closes gaps this check cannot reach.

What I do not regret is the audit. The patch was cheap either way. What it established is worth keeping: IAM direction is unusable as a signal for dataflow, and all 44 real bindings take the ARN form our parser did not match. Both will matter the next time we teach this catalog to infer an arrow.