Restricting Access to Items and Attributes with Fine-Grained Access Control
This lesson explains how IAM policy conditions restrict access within a single DynamoDB table, down to the items and attributes each user may read or write, so the database enforces per-user isolation.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how the dynamodb:LeadingKeys condition key limits a user to items with their own partition key.
- Describe how dynamodb:Attributes and dynamodb:Select restrict the attributes a request can access.
- Explain how the same technique isolates tenants in a pooled multi-tenant table.
Condition keys
IAM policies normally grant access to a whole DynamoDB table. Fine-grained access control narrows this with condition keys, so that a principal may work only with certain items or attributes within the table, and DynamoDB itself enforces the restriction on every request. Two condition keys are used.
dynamodb:LeadingKeyscompares the partition key of the items in a request with allowed values, such as the caller's own user ID, so each user can reach only their own items.dynamodb:Attributeslists the attributes a request may read or write, so a user can be prevented from seeing or changing particular fields.
Fine-grained access control is most often used when clients call DynamoDB directly, for example a mobile application whose users sign in through an Amazon Cognito identity pool and receive temporary AWS credentials. The policy uses a policy variable, which IAM replaces with a value from the caller's credentials at request time. In the following policy, ${cognito-identity.amazonaws.com:sub} becomes the caller's Cognito identity ID, so each user may access only items whose partition key is their own identity ID.
{
"Effect": "Allow",
"Action": ["dynamodb:GetItem", "dynamodb:Query", "dynamodb:PutItem", "dynamodb:UpdateItem"],
"Resource": "arn:aws:dynamodb:eu-west-1:111122223333:table/UserNotes",
"Condition": {
"ForAllValues:StringEquals": {"dynamodb:LeadingKeys": ["${cognito-identity.amazonaws.com:sub}"]}
}
}The ForAllValues qualifier requires every partition key in the request to match, so a batch request that includes another user's key is denied.
Restricting attributes
dynamodb:Attributes lists the attributes a request may touch, and dynamodb:Select together with dynamodb:ReturnValues stops a caller from retrieving every attribute another way.
Example. Users may read only the public profile attributes of any item, and the request must ask for specific attributes rather than all of them.
{
"Effect": "Allow",
"Action": ["dynamodb:GetItem", "dynamodb:Query"],
"Resource": "arn:aws:dynamodb:eu-west-1:111122223333:table/Profiles",
"Condition": {
"ForAllValues:StringEquals": {"dynamodb:Attributes": ["userId", "displayName", "avatarUrl"]},
"StringEqualsIfExists": {"dynamodb:Select": "SPECIFIC_ATTRIBUTES"}
}
}Multi-tenant tables
The same technique isolates tenants in a software-as-a-service (SaaS) application that stores all tenants' data in one pooled table. Here the callers are the application's own back-end services rather than end users. When a service handles a request for a tenant, it assumes an IAM role and passes a session tag that identifies the tenant, such as tenant=acme. The role's policy compares the partition key with that tag using the policy variable ${aws:PrincipalTag/tenant}.
The table's partition key must therefore equal the tenant ID or, with a StringLike condition and a value such as ${aws:PrincipalTag/tenant}#*, begin with it. With this design, isolation does not depend on every query in the application code including the correct tenant filter: a request for another tenant's items is denied by IAM even if the code contains a bug. The lesson Isolating Tenant Data in Multi-Tenant Applications in the AWS IAM module explains session tags in more detail.
A notes app with no backend
A mobile notes app lets users read and write their notes directly in DynamoDB using credentials from a Cognito identity pool. A tester discovers that changing the partition key in a request returns another user's notes.
| Layer | Before | After |
|---|---|---|
| Table key | noteId | Partition key userId (the identity ID), sort key noteId |
| Role policy | Allows GetItem, Query and PutItem on the table | Same actions, with dynamodb:LeadingKeys equal to ${cognito-identity.amazonaws.com:sub} |
| Result | Any item readable | DynamoDB denies any request for another user's partition key |
Isolation is now enforced by IAM on every request, not by the app, so a modified client cannot bypass it.
Which IAM condition key limits users to items whose partition key matches their own identity?
Show answer
dynamodb:LeadingKeys, compared with a variable such as ${cognito-identity.amazonaws.com:sub}.
Exam Tips
Remember for the exam
- To let users access only their own items, use the
dynamodb:LeadingKeyscondition. - To limit which attributes a user can read or write, use the
dynamodb:Attributescondition. - Cognito identity pool users are scoped with the
${cognito-identity.amazonaws.com:sub}policy variable. - To isolate tenants in a shared table, combine a tenant session tag with a
LeadingKeyscondition.