# EventBridge Pipes, and the Lambda I deleted

> Pipes went generally available at re:Invent with four stages and no code. Most of the Lambdas I have written between a queue and a target are exactly those four stages, badly.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2023-01-21
- Tags: AWS, Amazon EventBridge, Amazon SQS, Lambda, Serverless, Terraform
- URL: https://deyanpeev.com/posts/eventbridge-pipes-and-the-lambda-i-deleted

Amazon EventBridge Pipes became generally available on the first of December. The
pitch from the stage was that you shouldn't have to write glue code to connect
two AWS services together.

I wanted to test that against the glue code I've written myself. There is a lot
of it and it's all boring.

# Four stages

A pipe has four stages, in order: **source**, **filter**, **enrichment**,
**target**. Source and target are required, the two in the middle are optional.

Sources are the polling kind, so SQS, Kinesis, DynamoDB Streams, Amazon MQ, MSK
and self-managed Kafka. The filter drops events you don't want, with the same
pattern syntax EventBridge rules already use. The enrichment step can call a
Lambda, a Step Functions workflow, an API destination or API Gateway to transform
or enrich an event. Targets are the long list of things EventBridge can already
reach.

Those four stages are pretty much what a "just move this over there" Lambda
does.

# The function it replaces

The one I keep writing looks like this, with the details changed:

```js
export const handler = async (event) => {
  for (const record of event.Records) {
    const body = JSON.parse(record.body)

    if (body.type !== 'membership.activated') continue

    await sfn.startExecution({
      stateMachineArn: process.env.STATE_MACHINE_ARN,
      input: JSON.stringify({ memberId: body.memberId, at: body.occurredAt }),
    })
  }
}
```

Read it against the four stages. The `Records` loop is the source, the `continue`
is the filter, the `JSON.stringify` reshape is a transformation and
`startExecution` is the target. Everything else is what it costs to host those
four ideas inside a runtime.

And it costs a deployment package, a runtime version that goes end-of-life on
somebody else's schedule, an IAM role, a log group, a concurrency setting,
partial-batch-failure handling that's easy to get subtly wrong, and a
dependency manifest that security scanning will have an opinion about. All for
a function whose logic is one comparison.

The equivalent pipe has no runtime, no package, no log retention decision and no
cold start:

```hcl
resource "aws_pipes_pipe" "activations" {
  name     = "membership-activations"
  role_arn = aws_iam_role.pipe.arn
  source   = aws_sqs_queue.events.arn
  target   = aws_sfn_state_machine.onboarding.arn

  source_parameters {
    sqs_queue_parameters {
      batch_size = 10
    }
  }

  filter_criteria {
    filter {
      pattern = jsonencode({ body = { type = ["membership.activated"] } })
    }
  }

  target_parameters {
    input_template = <<EOT
{ "memberId": <$.body.memberId>, "at": <$.body.occurredAt> }
EOT
  }
}
```

# Where it stops

The filter is a pattern match. It can test values, prefixes, numeric ranges and
presence. It can't call anything, hold state, or make a decision that depends on
another system. As soon as the rule is "forward this if the member is on a paid
plan", the filter can't answer and you're into the enrichment step. That is a
Lambda, so you are back where you started plus a pipe.

So Pipes replace the glue that is a *projection* of the event. The glue that is
a *decision about* the event stays a function.

A pipe doesn't replace an EventBridge bus either. A bus is one-to-many, you
publish once and let many rules match. A pipe is one-to-one, source to target.
They solve adjacent problems and the naming doesn't help.

# The default for the next one

I won't migrate anything. The functions that already exist work, and rewriting
a working integration just to remove a Lambda is paying in real risk for
tidiness.

What changes is what I start with next time. A suprising share of the
integrations in any event-driven system are "take what arrives on this queue,
keep the ones that look like this, reshape them, and hand them to that". For
those I'll write a pipe first, and a Lambda only when something has to be
decided.

It also gives me a question to ask of existing code. Of the functions in an
account, how many are a `for` loop, an `if`, a reshape and a call? In the
accounts I've looked at, more than I would have guessed before somebody drew
the four boxes.
