DEV Community

Cover image for Stop sharing one Lambda role across your whole Serverless service
Sascha Mirow-Stanitzok
Sascha Mirow-Stanitzok

Posted on Edited on Originally published at sascha-ms.de

Stop sharing one Lambda role across your whole Serverless service

Picture a code review where a colleague adds one innocent line to the provider block of a serverless.yml: permission to write to a new queue. Only one function needs it. But because the whole service shares a single execution role, every function now gets it, including the read-only catalog endpoint that faces the internet. Nobody notices, because nothing breaks.

That is the quiet problem with the default setup of the Serverless Framework, and the reason I wrote a longer guide on giving every Lambda function its own IAM role. Here is the short version.

Why the shared role slowly turns into a liability

By default the framework creates one role for the entire service and attaches every function to it. Early on, with two functions and one table, that feels harmless. A year later you have a dozen functions, several tables, queues and a bucket, and each function can do whatever any other function ever needed.

Two things make this worse over time:

  • Blast radius. If one function has a vulnerable dependency or passes unchecked input into an SDK call, an attacker inherits every permission in that shared role.
  • Policy size. IAM caps the inline policies of a role at 10,240 characters. Large services eventually hit "Maximum policy size exceeded" during a deployment, which is usually the worst moment to start cleaning up.

One line changes the default

For years the answer was the community plugin serverless-iam-roles-per-function. Serverless Framework v4 now does this natively:

provider:
  iam:
    role:
      mode: perFunction

functions:
  listBooks:
    handler: src/list-books.handler
    iam:
      role:
        statements:
          - Effect: Allow
            Action: dynamodb:Query
            Resource: !GetAtt BooksTable.Arn
Enter fullscreen mode Exit fullscreen mode

Each function gets its own role, and its permissions sit right next to its definition. What I like most is that the framework still adds the boring parts for you: log permissions, the SQS, Kinesis or DynamoDB Streams permissions for event sources, kms:Decrypt when a key is configured, X-Ray and VPC access. You only describe what your own code actively calls.

There are also two in-between options. In the default shared mode, a function that defines its own statements already gets a dedicated role, which is handy for a gradual migration. And you can still hand in a fully custom role via role: if a platform team prescribes one, at the price of maintaining every permission yourself.

What I learned from switching

A few observations that matter more than the configuration itself:

  1. Look at the template, not just the YAML. Running serverless package writes the CloudFormation template without touching your account. After the switch, the old IamRoleLambdaExecution should be gone and you should see one role per function.
  2. Automate that check. I use a small dependency-free Node.js script in CI that fails when two functions share a role, when an action ends in *, or when an action is allowed on all resources without a good reason.
  3. Expect hidden dependencies. The most common surprise is a function that read a table for years without a statement of its own, because the permission came from another function through the shared role. After the split, it gets AccessDenied.
  4. Inheritance flips between modes. In perFunction mode, provider statements are inherited by every role unless you opt out. In shared mode it is the other way around. That is why I keep the provider block as small as possible.
  5. Prove the boundary. The IAM policy simulator can confirm that the read-only function really gets implicitDeny when it tries to write, without changing any code.

My recommendation: use mode: perFunction from day one for new services. For existing ones, migrate step by step and start with the functions reachable from outside. I would not swing to hand-built roles for everything, though; they look more controlled but lose the automatic permissions and go stale quickly.

What the full article covers

The complete guide on sascha-ms.de goes much deeper:

  • A full library loan API with serverless.yml, TypeScript handlers, SQS and a dead-letter queue
  • The complete CI check script plus tests with the Node.js test runner
  • A mapping table and step-by-step plan for moving off the old plugin
  • Typical errors such as role names over 64 characters and scoping DynamoDB index ARNs
  • Next steps with IAM Access Analyzer, permissions boundaries and account quotas

๐Ÿ“– This is the condensed version. The full article on sascha-ms.de goes deeper โ€“ with all code samples, diagrams and a short knowledge check.


๐Ÿ‘‰ Next step: Curious what your own Lambda functions cost? Try my Lambda cost calculator โ€“ it runs right in your browser.

๐Ÿ“ฌ Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).

Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en

Top comments (0)