# Set CloudWatch Log Retention in a Serverless Framework Service

Yes. An agent sets logRetentionInDays for every function in your serverless.yml, checks the packaged CloudFormation template for the retention, and opens a PR.

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

## What the agent does

The agent reads the service definition first, because retention is a property of the log group the framework creates, not of the function. In a service like `aws-node-express-dynamodb-api` on the `v4` branch of serverless/examples, `serverless.yml` declares `frameworkVersion: "4"`, a `provider` on `nodejs24.x` and `arm64` with an IAM role limited to six DynamoDB actions on the `UsersTable`, a `USERS_TABLE` environment variable taken from `${param:tableName}`, which `stages.default.params` sets to `users-table-${sls:stage}`, and one function, `api`, whose handler is `handler.handler` behind `httpApi: "*"`. The `resources` block defines the table with `PAY_PER_REQUEST` billing. Nothing in the file mentions logs, so the function's log group keeps every line it ever writes.

`handler.js` is where those lines come from. It builds an Express 5 app wrapped in `serverless-http` 4, with `GET /users/:userId` reading through a `GetCommand` and `POST /users` writing through a `PutCommand` from `@aws-sdk/lib-dynamodb`, and both `catch` blocks print the whole error object with `console.log` or `console.error` before answering 500. Every request also produces Lambda's own `START`, `END` and `REPORT` lines. The agent reads those paths to say in the pull request what a retention period actually discards: request errors with stack traces and per-invocation reports, nothing the service needs later.

The change is one line under `provider`: `logRetentionInDays: 30`, or the number the issue asks for, which applies to every function in the service, with the per-function form noted for a service where one handler needs a longer window. The framework already emits an `AWS::Logs::LogGroup` named `ApiLogGroup` for the function, so the agent runs `serverless package` in its sandbox and reads `.serverless/cloudformation-template-update-stack.json` to confirm the group now carries `RetentionInDays`. Packaging resolves `${sls:stage}` and `${param:tableName}` locally and needs no AWS account; it does need the `SERVERLESS_ACCESS_KEY` that Framework v4 requires, saved as a Project secret so the run reads it from its environment. Without that secret the agent still makes the edit, quotes the template fragment it expects, and says the packaging step was skipped.

The agent also reads what the `README.md` promises about `serverless remove` taking the DynamoDB table with it and leaves that alone, since retention and deletion policy are different decisions. It checks whether the service turns on HTTP API access logging with `provider.logs.httpApi`, which this one does not, and says in the pull request that the access-log group would need the same setting if it were enabled later. `package.json` has no scripts, so there is no test to run; the packaged template is the evidence.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Set CloudWatch log retention for this Serverless Framework service. Read serverless.yml, handler.js, package.json and README.md first. Add logRetentionInDays under provider with the number of days the issue asks for, or 30 if it names none, and note the per-function form in the pull request for handlers that need a longer window. Run serverless package with the SERVERLESS_ACCESS_KEY from the environment and confirm in .serverless/cloudformation-template-update-stack.json that the function's log group carries RetentionInDays; if the key is missing, make the edit, quote the fragment you expect, and say packaging was skipped. Check whether provider.logs.httpApi is enabled and say what that group would need. Do not change the handler, the IAM statements or the table's deletion behaviour. Open a pull request that quotes the template fragment and names what the retention period discards.",
  "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

The pull request changes `serverless.yml` by one line under `provider` and nothing else. Its description quotes the `ApiLogGroup` resource from the packaged template with `RetentionInDays` set, names the stage and region the package resolved, and says what the window discards: Lambda's per-invocation `REPORT` lines and the error objects the two `catch` blocks print.

The description also records two things the agent checked and left: the service does not enable `provider.logs.httpApi`, so there is no access-log group to configure yet, and the DynamoDB table still has no `DeletionPolicy`, which the repository's own README mentions and which is a separate decision.

Before merging, a reviewer confirms the number of days against the team's retention policy, and deploys to a non-production stage first, since the first deploy after the change updates the log group in place and CloudFormation reports any conflict with a group created outside the stack.

## Run it on every Linear issue labelled logging

A team that tracks platform work in Linear runs this from an automation with a `linear` trigger on the `logging` label. The trigger appends the issue to this prompt, so the retention the issue asks for is what the agent reads, and the run starts in the Project whose repository the automation names. An automation run has its own `max_runs_per_hour` cap.

Each run opens one pull request from its own sandbox and links the issue. A service with several functions gets the `provider`-level setting and a note per function that may need its own, so one issue covers the whole service.

## Why run it on Rush?

Retention is a one-line change whose effect is invisible in the repository: the log group is created by the framework's generated template, and whether it expires anything depends on a property nobody wrote. The agent packages the service in its sandbox and reads the generated template, so the pull request shows the property on the resource instead of asserting that the line works.

Every command and its output stays on the run, including the package step and the template it produced, and nothing is deployed. A team with many services can start one run per service from the same issue and read each template fragment from the session list.

## Frequently asked questions

### Does the change apply to functions added later?

Yes. `logRetentionInDays` under `provider` applies to every function in the service, including ones added after the pull request. A function that needs a different window sets its own `logRetentionInDays`, which overrides the provider value for that function only.

### Will the first deploy fail because the log group already exists?

The framework has been creating `ApiLogGroup` in the stack since the service was first deployed, so the deploy updates that resource in place. A group created by hand or by a previous tool outside the stack would conflict, which is why the pull request asks for a non-production stage first and the transcript shows the resource name to look for.

### What about the HTTP API's access logs?

This service does not enable them. If `provider.logs.httpApi` is turned on later, the framework creates a second log group for API Gateway, and the pull request says that group needs its own retention. The agent does not enable access logging as part of this change.

### Why does it need a Serverless access key?

Framework v4 requires sign-in for every command, including `serverless package`. The key goes in as a Project secret, and the run reads it from its environment the way any variable is read. No AWS credentials are needed, because packaging resolves only `${sls:stage}` and `${param:tableName}` and deploys nothing.

## More Serverless Framework jobs

- [Add Rate Limiting to a Serverless Framework API from a Linear Issue](https://getrush.ai/run/add-rate-limiting/serverless/)
- [Add Request ID Logging to a Serverless Express API](https://getrush.ai/run/add-request-id-logging/serverless/)
- [Add a Scheduled Function to a Serverless Framework API](https://getrush.ai/run/add-scheduled-functions/serverless/)

## The same job on other stacks

- [Set Lambda Log Retention in an AWS CDK Stack](https://getrush.ai/run/add-log-retention/aws-cdk/)

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

- [Add macOS Code Signing and Notarization to a Tauri Release Workflow](https://getrush.ai/run/add-macos-code-signing/tauri/)
- [Add a Man Page to a Go CLI](https://getrush.ai/run/add-man-page/go-cli/)

## 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/)
