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
-
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 beTENANT#acme, or the policy has to build the prefix. The simplest option: make the partition key the tenant ID as is. -
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"]
}
}
}
]
}
-
Allow tagging in the role’s trust policy. The trust policy lets your service’s role call
sts:AssumeRoleandsts:TagSession, the latter only when the request carries aTenantIDtag (theaws:RequestTag/TenantIDcondition). -
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 becauseScan“returns all items regardless of the leading keys”. Grant it and the isolation is gone. - Watch
ReturnValues. OnPutItem,UpdateItemandDeleteItem, this parameter can return the whole item, including attributes you shouldn’t see. That’s why the policy limits it toNONE,UPDATED_OLDandUPDATED_NEW. PutItemreplaces the whole item. If you also restrict attributes withdynamodb:Attributes, the documentation advises against allowingPutItemorDeleteItem, 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 theVersionline and the policy won’t do what you think. - Always
ForAllValues.LeadingKeysis 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
- AWS: Using IAM policy conditions for fine-grained access control (DynamoDB)
- AWS Security Blog: How to implement SaaS tenant isolation with ABAC and AWS IAM
If you’re building a SaaS on AWS and want more simple architecture decisions like this one, follow me on LinkedIn.
