Blog

Isolating SaaS tenants in DynamoDB with IAM and LeadingKeys

With dynamodb:LeadingKeys, IAM only lets each request read its own tenant's items. How to set it up, and the pitfalls AWS's documentation warns about.

Ink and watercolor illustration: a tall teal chest of drawers with a keyhole on each drawer; a hand holding a key opens just one.

If you run a SaaS with every client in the same DynamoDB table, put tenant isolation in IAM, not only in your code. With the dynamodb:LeadingKeys condition, AWS rejects any request for another client’s items even when your code, or code an agent wrote, uses the wrong ID.

What it is

In a multi-tenant SaaS (several clients, or tenants, sharing the same infrastructure), the usual DynamoDB setup is a single table whose partition key starts with the client ID: TENANT#acme, TENANT#globex. Your code filters by that ID in every query. The problem: if a single query forgets the filter, or gets the wrong ID, one client sees another client’s data.

The DynamoDB documentation offers a second barrier. The dynamodb:LeadingKeys condition key represents the partition key of the items a request touches. Put it in the IAM policy with the tenant ID, and IAM denies any GetItem, Query or UpdateItem on another partition before it reaches the table.

The trick to avoid one role per client is ABAC (attribute-based access control). The AWS Security Blog explains it: your service assumes a single role with sts:AssumeRole and adds a session tag (a tag on the temporary session) carrying the TenantID. The policy reads that tag with ${aws:PrincipalTag/TenantID}. One role serves a thousand clients, and onboarding a new one doesn’t touch IAM.

How to set it up

  1. Design the partition key around the tenant. The partition has to be exactly the value you’ll compare. If you use TENANT#acme, the session tag must be TENANT#acme, or the policy has to build the prefix. The simplest option: make the partition key the tenant ID as is.

  2. Create the table access role with this policy:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "dynamodb:GetItem",
        "dynamodb:BatchGetItem",
        "dynamodb:Query",
        "dynamodb:UpdateItem"
      ],
      "Resource": "arn:aws:dynamodb:eu-west-1:123456789012:table/App",
      "Condition": {
        "ForAllValues:StringEquals": {
          "dynamodb:LeadingKeys": ["${aws:PrincipalTag/TenantID}"]
        },
        "StringEqualsIfExists": {
          "dynamodb:ReturnValues": ["NONE", "UPDATED_OLD", "UPDATED_NEW"]
        }
      }
    }
  ]
}
  1. Allow tagging in the role’s trust policy. The trust policy lets your service’s role call sts:AssumeRole and sts:TagSession, the latter only when the request carries a TenantID tag (the aws:RequestTag/TenantID condition).

  2. Assume the role on each request with the tenant taken from the user’s token, never from the request body:

aws sts assume-role \
  --role-arn arn:aws:iam::123456789012:role/TenantDataAccess \
  --role-session-name tenant-acme \
  --tags Key=TenantID,Value=acme

With the credentials it returns, the DynamoDB client can only touch the acme partition. Ask for globex and you get an AccessDeniedException.

The pitfalls AWS warns about

The documentation is clear on several points, and each one is a leak if you ignore it:

  • No Scan. The allowed actions leave it out because Scan “returns all items regardless of the leading keys”. Grant it and the isolation is gone.
  • Watch ReturnValues. On PutItem, UpdateItem and DeleteItem, this parameter can return the whole item, including attributes you shouldn’t see. That’s why the policy limits it to NONE, UPDATED_OLD and UPDATED_NEW.
  • PutItem replaces the whole item. If you also restrict attributes with dynamodb:Attributes, the documentation advises against allowing PutItem or DeleteItem, since they always replace the entire item.
  • Version 2012-10-17 is required. Variables like ${aws:PrincipalTag/TenantID} don’t work with the default 2008-10-17 version. Forget the Version line and the policy won’t do what you think.
  • Always ForAllValues. LeadingKeys is plural even for single-item operations, and the condition needs that modifier.
  • Allow policies, not deny policies. AWS discourages building this with Deny, because a future API change can make the policy ineffective.
  • Global tables. Fine-grained access control isn’t supported for global table replication and can interrupt it.

My take

I’d add this to any multi-tenant SaaS on DynamoDB from the first client, not after the first scare. It costs one role, one policy and one STS call. In return, a bug in a query stops being a data incident with a client and becomes a 500 error in your logs.

The argument that weighs most for me today is agents. More and more data-access code is written by AI, and an AI can forget the tenant filter in function number forty. Reviewing every query by hand doesn’t scale. An IAM rule that doesn’t depend on the code being right does.

Two limits worth keeping in mind. First, this isolates rows, not cost: a noisy client still shares capacity with everyone else. Second, it only covers DynamoDB. Files in S3 or KMS keys need their own policy with the same session tag, and the AWS post shows how to do it with the same pattern.

And if you already have a Scan in production for an internal report, move it to a separate role that client traffic never uses.

Sources

If you’re building a SaaS on AWS and want more simple architecture decisions like this one, follow me on LinkedIn.

← All articles

Have a product to build or a tech stack to put in order?

Tell me what you are trying to achieve and where you are today. I reply personally, usually within a day.

Free · 30 minutes

Digital strategy session

30 minutes to map where to start digitalizing and transforming your business. You pay nothing, before or after.

When you book, you answer three short questions: what you need, what stage your project is at and when. That way the session starts with context.