# Add a Dead Letter Queue to a Lambda in AWS CDK from a Linear Issue

An agent gives five CDK Lambdas their own SQS dead letter queue and depth alarm, adds 9 assertion tests, and flags that API Gateway calls never reach them.

[Run this on Rush](https://cloud.getrush.ai/run/add-dlq/aws-cdk/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent works in `typescript/api-cors-lambda-crud-dynamodb`, reading `index.ts`, `package.json` and `jest.config.js`. Before changing anything it finds `npm test` fails with `Missing script: "test"` though Jest and its config are already there, and that `index.ts` calls `app.synth()` on import, which would synthesize the app inside every test.

It replaces the five copied `NodejsFunction` blocks with one `createFunction` helper that makes a `Queue` (14-day retention, `enforceSSL`), passes it as `deadLetterQueue`, and adds an `Alarm` on `metricApproximateNumberOfMessagesVisible` at one or more messages over a minute. It wraps the synth in `if (require.main === module)`, adds `"test": "jest"`, and writes `index.test.ts` with 9 tests: queue count, DLQ wiring, `sqs:SendMessage` permissions, alarm properties and a queue-to-alarm-to-function check.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "A Linear issue labeled agent says: \"Add a Dead Letter Queue to a Lambda in AWS CDK. In typescript/api-cors-lambda-crud-dynamodb, give each Lambda an SQS dead letter queue and a CloudWatch alarm on its depth, with assertion tests.\" Do the work in this repository, add tests for it, run npm test and cdk synth in the example, and open a pull request that links back to the issue.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

Tried on aws-samples/aws-cdk-examples at 0ca4ff1: 4 files changed, 114 insertions(+), 24 deletions(-) in `index.ts`, a new `index.test.ts`, `package.json` and `README.md`. The issue text was written for the trial and had no ID. `npm test` went from `Missing script: "test"` to `Tests:       9 passed, 9 total`; `npm run build` and `npx cdk synth` exited 0 before and after.

For the reviewer: all five functions are called synchronously by API Gateway, and Lambda sends only failed asynchronous invocations to a DLQ, so these queues stay empty for API errors. If that is what you wanted covered, use alarms on the Lambda `Errors` metric instead. The alarms have no SNS action, and the threshold and retention are the agent's defaults.

## Run it on every Linear issue labeled agent

An automation with a `linear` trigger on `linear.issue.labeled` and the label `agent` runs the `linear-label-to-pr` recipe's prompt when someone in your Rush org labels an infrastructure ticket. An issue that says which failures it wants caught, API errors or asynchronous invocations, lets the agent choose between a dead letter queue and alarms on the Lambda `Errors` metric.

## Why run it on Rush?

The run did what the issue asked and then said why it would not help: every function here is invoked synchronously, so a dead letter queue never fills. A reviewer learns that from the pull request, not from an alarm that stays green through an outage.

## Frequently asked questions

### Why did the agent change how index.ts synthesizes?

`index.ts` ran `app.synth()` when imported, so the tests would synthesize the whole app as a side effect. Wrapping it in `if (require.main === module)` keeps `cdk synth` working while the tests import the stack.

### When does an alarm fire?

When `ApproximateNumberOfMessagesVisible` is one or more over a 1-minute period, with missing data treated as not breaching. Nothing is notified until you add an action such as an SNS topic.

## More AWS CDK jobs

- [Enable DynamoDB Point-in-Time Recovery in an AWS CDK Stack from a Linear Issue](https://getrush.ai/run/add-dynamodb-point-in-time-recovery/aws-cdk/)
- [Set Up GitHub Actions CI for an AWS CDK App](https://getrush.ai/run/add-github-actions-ci/aws-cdk/)
- [Add Request Validation to an API Gateway in AWS CDK](https://getrush.ai/run/add-input-validation/aws-cdk/)

## The same job on other stacks

- [Add a Dead-Letter Queue to a Serverless SQS Worker](https://getrush.ai/run/add-dlq/serverless/)

## More jobs like this: Turn a Linear label into a PR

- [Add Doxygen API Docs to a CMake C++ Library](https://getrush.ai/run/add-doxygen-docs/cmake/)
- [Add a --dry-run Flag to a Click CLI](https://getrush.ai/run/add-dry-run-flag/click/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
