All jobs

Give Each Lambda Its Own IAM Role in a Serverless Framework Service

Yes. An agent reads what each handler calls, replaces the shared provider role with one least-privilege role per function in serverless.yml, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the handlers before it touches a policy, because the right permissions come from the code and not from the existing YAML. In a service like aws-node-http-api-dynamodb in serverless/examples, serverless.yml declares frameworkVersion: '4', nodejs24.x on arm64, one DYNAMODB_TABLE variable, an HTTP API with CORS, and a single provider.iam.role.statements entry that allows dynamodb:Query, Scan, GetItem, PutItem, UpdateItem and DeleteItem on the table's ARN for every function. The five handlers each use one command from @aws-sdk/lib-dynamodb: todos/create.js sends PutCommand, todos/list.js ScanCommand, todos/get.js GetCommand, todos/update.js UpdateCommand and todos/delete.js DeleteCommand. Nothing calls Query, so the shared role grants an action no code uses, and the function that lists todos can delete them.

From that mapping the agent gives each function its own role. The Serverless Framework documents two ways and the agent picks the one that keeps the file short: per-function iam.role.statements under each function, which the framework turns into a dedicated role, with the manual form, an AWS::IAM::Role under resources.Resources referenced by its logical id from the function's role key, as the fallback it explains in the pull request. create gets dynamodb:PutItem, list gets Scan, get gets GetItem, update gets UpdateItem and delete gets DeleteItem, each on !GetAtt TodosDynamoDbTable.Arn as before, and the provider-level statement goes. The framework's IAM guide says a custom role carries no default logging permissions, so the agent checks the packaged template for the logs:CreateLogGroup, logs:CreateLogStream and logs:PutLogEvents statements on each role and adds them explicitly where they are missing.

Serverless Framework v4 requires sign-in for its CLI, so proving the change needs a key. When a SERVERLESS_LICENSE_KEY secret is saved in the Rush Project the run starts in, the agent runs serverless package and reads .serverless/cloudformation-template-update-stack.json, confirming five AWS::IAM::Role resources with one DynamoDB action each, the log statements, and that each AWS::Lambda::Function references its own role. Without a key it still writes the YAML, runs node --check on each handler, and says in the pull request that the packaged template was not inspected and why.

Nothing is deployed. The agent changes no handler, because the point of the exercise is that the code already needs exactly one action each; it updates the README's structure section with a sentence on the per-function roles and keeps the DeletionPolicy: Retain on the table.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Give every function in this Serverless Framework service its own least-privilege IAM role. Read serverless.yml, package.json, the README and every file under todos first, and map each handler to the single DynamoDB command it sends. Replace the shared provider.iam.role.statements with a per-function role for create, list, get, update and delete that allows only PutItem, Scan, GetItem, UpdateItem or DeleteItem respectively on the table ARN, using the framework's per-function iam.role.statements form and explaining the explicit AWS::IAM::Role alternative in the pull request. Make sure each role carries the CloudWatch Logs permissions the framework's IAM guide says a custom role needs, adding them if the packaged template lacks them. If SERVERLESS_LICENSE_KEY is available in the environment, run serverless package and inspect the generated CloudFormation to confirm five roles with one DynamoDB action each; if not, say so. Run node --check on each handler, change no handler code, do not deploy, update the README, and open a pull request.",
  "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

What comes back is a pull request on its own branch that changes serverless.yml and the README. The provider-level statement is gone, each of the five functions carries its own iam.role.statements with one DynamoDB action on the table ARN plus the log permissions, and the table resource, the HTTP API events, the runtime and the architecture are unchanged. No file under todos/ moves.

The description shows the mapping table from handler to command to action, with dynamodb:Query listed as granted today and used by nobody. When the run had a license key, it pastes the five role resources from the packaged template and the function-to-role references; when it did not, it says the template was not generated and the reviewer should run serverless package once before deploying. The transcript at GET /o/{org}/sessions/{id}/messages keeps every command.

Before merging, a reviewer deploys to a non-production stage and exercises the five routes, because a missing action fails at runtime with an AccessDeniedException rather than at package time, and checks that nothing outside this service assumed the old shared role name. A follow-up message to the same session adds an action a handler turned out to need.

Run it on every Linear issue labelled security

A team that tracks hardening work in Linear starts one run per issue from an automation with a linear trigger on the security label. The automation adds the issue after this prompt, so a service name or a function the issue calls out is what the agent reads, and the run starts in the repository the automation names.

Each run opens its own pull request from its own sandbox, and an automation run has its own max_runs_per_hour cap. The pull request links the issue, and the mapping table in its description is the review.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

Least privilege is a reading job before it is a YAML job: the only way to know that list needs Scan and nothing else is to open todos/list.js and see the one command it sends. The agent does that for every handler and writes the table down, and where it can package the service it reads the generated CloudFormation to confirm the roles rather than trusting the YAML it wrote.

The pull request carries that reading and, with a key, the packaged template, in the transcript beside the commands that produced them. Several services can be handled at once, each in its own sandbox, and a run that could not package says so rather than claiming a check it did not make.

Frequently asked questions

Why not keep one role and just remove dynamodb:Query?

Because a shared role still lets the read-only list function delete items. One role per function means a bug or a compromised handler can do only what that handler does, which is the point of least privilege. Removing the unused Query is a side effect of writing each role from the code.

Does the framework add logging permissions to a custom role?

The framework's IAM guide says a custom role carries no default logging permissions and lists the logs: actions to add. The agent inspects the packaged template when it can and adds the statements to each role where they are missing, so the functions keep writing to CloudWatch after the change.

What does the run need to package the service?

Serverless Framework v4 requires sign-in for its CLI, which it reads from SERVERLESS_LICENSE_KEY in CI. Save that as a secret in the Rush Project the run starts in and it reaches the sandbox as that environment variable; without it the agent writes the YAML and says the template was not inspected.

Will this deploy anything?

No. The run packages at most and never calls serverless deploy. The reviewer deploys to a non-production stage and calls each route, because an action a handler needs and the role lacks shows up as a runtime AccessDeniedException, not as a packaging error.

More Serverless Framework jobs

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

Keep reading