Ravindra BagaleCourses & study guides मराठी Track your progress

DynamoDB Security: IAM Fine-Grained Access, KMS and VPC Endpoints

Security is not a bolt-on after the app works. Dubai SaaS lead Layla Hassan and Pune bank engineer Arjun Mehta both said: We shipped CartLite, then a leaked key read every tenant. IAM LeadingKeys, KMS CMK, and a VPC endpoint would have blocked that blast. Builds on multi-tenant keys and modeling.

What you'll learn in this chapter

  • Explain IAM least privilege for DynamoDB tables and indexes
  • Use LeadingKeys / fine-grained access for tenant isolation
  • Choose AWS-owned vs customer managed KMS keys
  • Put DynamoDB behind a VPC gateway endpoint
  • Run a safe lab and cleanup

1. Everyday example: hotel key cards

A master key opens every room — that is a wild IAM * on DynamoDB. A guest card opens one room — that is LeadingKeys on TENANT#acme. The lock cylinder is KMS. The private lift that never goes through the lobby is a VPC endpoint. Meera Joshi (Pune clinic) gives each ward app a card for PATIENT#wardA only.

2. Vertical steps

Why -> What -> How -> Lab -> CleanupWhyWhatHowLabCleanup
Always cleanup lab resources

3. Why do we need this?

Why do we need this?

Stolen access keys, over-broad roles, and public network paths turn a good table into a data leak. Fine-grained IAM stops cross-tenant reads. KMS controls who can decrypt. VPC endpoints keep traffic off the public internet. Without these, modeling and transactions still leak.

4. What is it?

What is it?

IAM: identity policies on roles/users for dynamodb:GetItem, Query, PutItem, and index ARNs. Fine-grained access: condition keys like dynamodb:LeadingKeys so PK must start with a tenant prefix. KMS: SSE-DynamoDB (AWS owned) or SSE-KMS with your CMK. VPC gateway endpoint: private route from VPC to DynamoDB without NAT/IGW.

5. How to do it (step by step)

How to do it (step by step)

  1. List who reads/writes which table and GSI — no * unless break-glass.
  2. Scope Resource to table ARN and /index/* you need.
  3. For multi-tenant: add StringLike dynamodb:LeadingKeys TENANT#id.
  4. Enable encryption: default AWS owned, or CMK when you need key policy + rotation proof.
  5. Create vpc endpoint dynamodb gateway; update route tables.
  6. Deny dynamodb: via public path in SCPs if your org requires private only.
  7. Test with a second role that should fail cross-tenant GetItem.

6. IAM policy sketch (LeadingKeys)

{
  "Effect": "Allow",
  "Action": ["dynamodb:GetItem","dynamodb:Query","dynamodb:PutItem","dynamodb:UpdateItem"],
  "Resource": [
    "arn:aws:dynamodb:ap-south-1:111122223333:table/lab-ddr-secure",
    "arn:aws:dynamodb:ap-south-1:111122223333:table/lab-ddr-secure/index/*"
  ],
  "Condition": {
    "ForAllValues:StringLike": {"dynamodb:LeadingKeys": ["TENANT#acme"]}
  }
}
# Meaning: role may only touch items whose PK begins with TENANT#acme.

Pair with app code that always prefixes PK. UI filters are not security — IAM is.

7. KMS and encryption at rest

  • SSE-DynamoDB (default): AWS owns the key; simplest.
  • SSE-KMS CMK: you own key policy, rotation, CloudTrail on Decrypt.
  • Encrypts table, LSI, GSI, streams, backups at rest.
  • In-transit: HTTPS/TLS to the DynamoDB API (and PrivateLink/endpoint path).

8. VPC gateway endpoint

Gateway endpoint for com.amazonaws.region.dynamodb adds a prefix list route. No security group on gateway endpoints — use endpoint policy to limit which principals/tables. Apps in private subnets reach DynamoDB without a NAT gateway for that traffic.

9. Commands with output and meaning

aws dynamodb create-table --table-name lab-ddr-secure   --attribute-definitions AttributeName=PK,AttributeType=S AttributeName=SK,AttributeType=S   --key-schema AttributeName=PK,KeyType=HASH AttributeName=SK,KeyType=RANGE   --billing-mode PAY_PER_REQUEST   --sse-specification Enabled=true,SSEType=KMS
# Output: TableStatus CREATING; SSEDescription present when active
aws dynamodb describe-table --table-name lab-ddr-secure   --query 'Table.SSEDescription'
# Output: Status ENABLED, SSEType KMS — meaning encryption at rest on
aws ec2 create-vpc-endpoint --vpc-id vpc-0abc --service-name com.amazonaws.ap-south-1.dynamodb   --route-table-ids rtb-0xyz --vpc-endpoint-type Gateway
# Output: VpcEndpointId vpce-… State available — private path ready

create-table with SSE KMS turns encryption on. describe proves it. create-vpc-endpoint makes the private route. Always delete lab tables and endpoints after practice.

10. Industry stories

  • SaaS (Layla Hassan, Dubai): LeadingKeys per tenant role; stolen token cannot read other tenants.
  • Bank (Arjun Mehta, Pune): CMK + key policy only payment role Decrypt; CloudTrail on misuse.
  • Health (Meera Joshi): ward roles LeadingKeys PATIENT#wardX; VPC endpoint only.
  • Retail (Diego Alvarez): separate IAM for batch export role vs online API role.
  • IoT (Mei Lin): device writer role PutItem only; no Scan.
  • Gaming (Carlos Rivera): score writer vs admin reader different policies.

11. Lab

How to do it (step by step)

  1. Create lab-ddr-secure on-demand with SSE KMS (AWS managed key ok for lab).
  2. Put TENANT#acme / ITEM#1 and TENANT#beta / ITEM#1.
  3. Attach a role with LeadingKeys TENANT#acme; confirm beta GetItem fails.
  4. Optional: create DynamoDB gateway endpoint in a lab VPC.
  5. Delete table (and endpoint if created).

Cleanup (stop billing)

  1. Delete lab-ddr-secure.
  2. Delete lab VPC endpoint if you made one.
  3. Confirm describe-table fails (ResourceNotFound).

12. Ops checklist before you ship

  1. Confirm TTL attribute name and units (epoch seconds).
  2. Decide soft-delete vs hard-delete vs archive-to-S3.
  3. Enable PITR on any table you cannot rebuild from source.
  4. Document RPO/RTO and who runs restore drills.
  5. If multi-region: pick write stickiness and conflict rules.
  6. If DAX: prove cache-miss path and warm-up plan.
  7. Tag Owner + Expiry on every lab resource.
  8. Budget alert in the account before load tests.

13. Copy-paste DynamoDB CLI starters (batch 2)

export AWS_DEFAULT_REGION=ap-south-1

# TTL status
aws dynamodb describe-time-to-live --table-name ShopApp

# PITR / continuous backups
aws dynamodb describe-continuous-backups --table-name ShopApp

# On-demand backup
aws dynamodb create-backup --table-name ShopApp --backup-name lab-shop-$(date +%Y%m%d)

# List backups
aws dynamodb list-backups --table-name ShopApp

# Global table description (v2)
aws dynamodb describe-table --table-name ShopApp --query 'Table.Replicas'

14. More industry stories

  • Retail (Aisha Khan, Dubai): TTL clears abandoned carts; soft-delete keeps 30-day undo.
  • Bank (Lucas Meyer, Berlin): PITR on ledger table; restore drill every quarter.
  • Travel (Omar Haddad, Amman): Global Tables for catalog reads; inventory writes sticky.
  • Gaming (Carlos Rivera, Mexico City): DAX for hot profile Gets; never cache money ledgers.
  • Social (Priya Nair, Kochi): adjacency edges for follows; sparse GSI for unread inbox.
  • IoT (Mei Lin, Shenzhen): device#day PK + TTL 14 days; rollups to S3.

15. Glossary (batch 2)

TermMeaning
TTLTime to Live — attribute with epoch seconds; DynamoDB deletes later
Soft deleteFlag deleted=true; item remains until TTL/archive
PITRPoint-in-time recovery — continuous backups window
Global TableMulti-region replicated DynamoDB table
LWWLast-writer-wins conflict resolution on replicas
DAXDynamoDB Accelerator — in-memory cache cluster
Adjacency listEdge items modeling relationships in one table
Fan-outWrite once; push copies to many feeds/inboxes

16. Practice: explain out loud

  1. 60s: TTL vs soft-delete vs S3 archive.
  2. 60s: PITR vs on-demand backup.
  3. 60s: when Global Tables help and hurt.
  4. 60s: DAX consistency trade-off.
  5. 60s: CartLite checkout keys in one breath.
  6. 60s: follow edge vs feed item.
  7. Record once; cut filler; record again.

17. Safe lab hygiene

  • Use ap-south-1 labs with a budget alert.
  • Prefix lab tables with lab- and your initials.
  • Delete replicas, DAX clusters, and backups you created for labs.
  • Never enable Global Tables on production without a write plan.
  • Tag every resource Owner + Expiry.
  • Export/import labs can create large S3 bills — delete prefixes.

19. Delete / archive decision pad

NeedPreferWhy
Undo 7-30 daysSoft-delete flagUser restore
Hard expiry, no readTTL attributeAuto delete later
Compliance keep coldExport/archive to S3Cheap long store
Mistake recoveryPITR / backupTable-level restore

20. Ten oral drill questions

  1. What units does DynamoDB TTL use?
  2. Does TTL delete instantly?
  3. PITR window typical length?
  4. Global Tables conflict default?
  5. When sticky writes matter?
  6. DAX vs API Gateway cache?
  7. Cart PK for a shopper?
  8. Order lines under which PK?
  9. Follow edge SK example?
  10. Unread inbox sparse GSI idea?

21. RPO / RTO mini workshop

  1. Write the worst data loss you can accept (RPO) in minutes.
  2. Write how long restore may take (RTO) before business pain.
  3. Map: on-demand backup vs PITR vs multi-region replica.
  4. Schedule a restore drill on a lab table this month.
  5. Name the person who runs the drill.

22. Cost watch list (batch 2)

  • PITR storage and on-demand backup storage.
  • Global Tables: replicated WCUs in every region.
  • DAX node hours even at idle.
  • Export to S3 + S3 storage + Athena later.
  • Streams + Lambda still bill during TTL deletes.

23. Project Definition of Done

  1. Access patterns written and peer-reviewed.
  2. Sample items drawn for happy path + one failure.
  3. Lab table created, exercised, deleted (or tagged Expiry).
  4. At least one conditional write or transaction exercised.
  5. Cleanup commands pasted in your notes.
  6. One interview answer recorded out loud.
  • TTL hygiene → Backup/PITR for “oops” recovery.
  • Global Tables → sticky writes from capacity/hot lessons.
  • DAX → only after Query shapes are correct.
  • P1 CartLite reuses modeling + transactions.
  • P2 Social reuses adjacency + Streams ideas.

25. Quick self-check (yes/no)

  1. I can explain TTL delay vs instant DeleteItem.
  2. I know when soft-delete beats hard delete.
  3. I can start PITR and say what restore costs in time.
  4. I can name one reason NOT to use Global Tables.
  5. I know DAX does not fix a bad partition key.
  6. I can sketch CartLite USER# and ORD# keys.
  7. I can sketch a follow edge item.
  8. I delete lab replicas/DAX/backups the same day.

26. Common errors and fixes

Each row is a real mistake students and juniors hit. Read the fix before you panic.

Error / symptomFix
IAM Resource * on all tablesScope table + index ARNs.
UI hide tenants onlyLeadingKeys on IAM.
LeadingKeys wrong prefixMatch PK design exactly.
Forget /index/* ARNAdd index ARNs you Query.
CMK policy blocks DynamoDB serviceAllow service via key policy.
Expect gateway endpoint to use SGUse endpoint policy + routes.
Access key in mobile appUse Cognito/temp creds.
Scan allowed on tenant roleDeny Scan for app roles.
No negative testProve cross-tenant fails.

27. Troubleshooting checklist

  1. AccessDeniedException: check action, ARN, LeadingKeys.
  2. ValidationException on condition: PK must be single value string for LeadingKeys.
  3. SSE not showing: wait CREATING; re-describe.
  4. Endpoint created but still public: route table not associated.
  5. CMK AccessDenied on backup/export: grant key to that role too.

28. FAQ

Q. Should I always use a single table?

Often yes for related access patterns; not a religion. Separate tables are fine for unrelated domains or different capacity/security needs.

Q. Do I still need RDS?

Yes for heavy joins, multi-row SQL reports, and strong relational constraints. See the intro DynamoDB vs RDS chapter.

Q. Is Scan evil?

Scan is a tool for rare admin jobs and small tables. Design so hot paths use Query/GetItem.

Q. Where are Put/Get command details?

In the CRUD survey, CLI and SDK chapters — this deep set links them instead of copying.

Q. Is VPC endpoint required?

Strongly recommended for private subnets and compliance; public HTTPS with locked IAM still encrypts in transit but uses public service endpoints.

Q. LeadingKeys vs app filter?

Both — app for UX, IAM for enforcement.

Q. AWS owned key vs CMK?

CMK when you need key policy, rotation proof, or cross-account control.

Q. Does FGAC work on GSI?

LeadingKeys applies to table PK on the request; design GSI keys so tenant still cannot cross-query blindly — often keep tenant in GSI PK too.

Q. What does dynamodb:LeadingKeys enforce?

The partition key value on the request must match the condition (often a tenant prefix).

Q. Why prefer a VPC gateway endpoint?

Private route to DynamoDB without NAT for that traffic; pair with endpoint policy.

Q. Name one CMK benefit over AWS-owned.

Key policy, CloudTrail Decrypt, rotation ownership.

Q. MNC: how do you prove tenant isolation?

Negative IAM test: role A cannot GetItem tenant B; show CloudTrail.

Try at home

  1. Write an IAM policy for one table + one GSI with LeadingKeys.
  2. Sketch who needs CMK Decrypt in your app.
  3. List route tables that need the DynamoDB endpoint.

In short

  1. Least privilege on table and index ARNs.
  2. LeadingKeys for tenant PK prefixes.
  3. KMS: default or CMK when you need control.
  4. VPC gateway endpoint for private path.
  5. Always negative-test cross-tenant access.

Revision, interview and comparison cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: LeadingKeys IAM. UI filters alone leak other tenants when tokens steal. IAM dynamodb:LeadingKeys condition on role or policy. Attach condition → deny Get/Query outside prefix → test stolen role. Priya Iyer demos LeadingKeys before any multi-tenant story.
Revision card: LeadingKeys IAM · Download PNG · Open card page
Revision card: KMS + VPC endpoint. AWS-owned key + public path leave audit and network gaps. CMK (KMS) for table SSE + Gateway VPC endpoint for DynamoDB. Create CMK → encrypt table → endpoint policy → block 0.0.0.0/0. Finance labs ask CMK rotation and private hops together.
Revision card: KMS + VPC endpoint · Download PNG · Open card page
Comparison: UI filter only vs LeadingKeys IAM. UI filter only: Hide other tenants only in the UI. LeadingKeys IAM: IAM dynamodb:LeadingKeys Deny on role.
Comparison: UI filter only vs LeadingKeys IAM · Download PNG · Open card page
Comparison: AWS-owned key vs Customer CMK. AWS-owned key: SSE with AWS-owned key; little CloudTrail on decrypt. Customer CMK: SSE-KMS CMK with grant and rotation.
Comparison: AWS-owned key vs Customer CMK · Download PNG · Open card page
Interview card: Fine-grained access. Table-wide Allow lets any PK be read. Condition: dynamodb:LeadingKeys equals tenant prefix. MNC: show Deny GetItem when PK ≠ caller tenant.
Interview card: Fine-grained access · Download PNG · Open card page
Interview card: Customer-managed KMS. AWS-owned key hides who can decrypt in CloudTrail. SSE-KMS with your CMK; grant kms:Decrypt only to roles. MNC: rotate CMK schedule + deny Decrypt outside app role.
Interview card: Customer-managed KMS · Download PNG · Open card page
Interview card: VPC endpoint. NAT to public DynamoDB endpoint adds cost and exposure. Gateway VPC endpoint + endpoint policy for dynamodb. MNC: prove no IGW route needed for PutItem from private subnet.
Interview card: VPC endpoint · Download PNG · Open card page
Interview card: Least-privilege actions. dynamodb:* on * breaks blast-radius interviews. List exact actions: GetItem Query PutItem; scope ARN + conditions. MNC: refuse Scan in prod role; allow Query only.
Interview card: Least-privilege actions · Download PNG · Open card page

More cards: Ravi Sir's Cards.

Practice Tasks

About this scenario

Maple Leaf Books is a bookselling business in Toronto. Emily Tremblay looks after its IT and cloud work. They count money in CAD and run their AWS resources in the ca-central-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 16

Easy Least privilege ARNs — 1

Why: IAM.

What to do: Enter 1 if policies scope table and index ARNs.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy LeadingKeys FGAC — 1

Why: FGAC.

What to do: Enter 1 if dynamodb:LeadingKeys enforces PK prefixes.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy UI filter not enough — 1

Why: Truth.

What to do: Enter 1 if UI tenant filters alone are not security.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy CMK for key policy — 1

Why: KMS.

What to do: Enter 1 if CMK gives key policy and Decrypt audit.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Describe secure table

Why: CLI.

What to do: Describe table ShopApp

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy LeadingKeys YAML

Why: Name.

What to do: YAML: condition_key: dynamodb:LeadingKeys.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Easy Endpoint policy limits — 1

Why: Policy.

What to do: Enter 1 if VPC endpoint policies can limit principals/tables.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium SSE encrypts at rest — 1

Why: Rest.

What to do: Enter 1 if SSE encrypts table indexes streams backups at rest.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Gateway VPC endpoint — 1

Why: Private.

What to do: Enter 1 if DynamoDB uses a gateway VPC endpoint.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Endpoint needs routes — 1

Why: Net.

What to do: Enter 1 if gateway endpoints need route table associations.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Include /index/* ARN — 1

Why: GSI.

What to do: Enter 1 if Query on GSI needs /index/* in Resource.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Tenant PK YAML

Why: Sketch.

What to do: YAML: pk: TENANT#acme, sk: ITEM#1.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Hard Negative cross-tenant test — 1

Why: Proof.

What to do: Enter 1 if you must prove cross-tenant GetItem fails.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Deny Scan on app roles — 1

Why: Anti.

What to do: Enter 1 if app roles should deny Scan.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard No access key in app — 1

Why: Creds.

What to do: Enter 1 if mobile apps must not embed long-lived access keys.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Delete lab same day — 1

Why: Hygiene.

What to do: Enter 1 if lab-ddr-secure should be deleted same day.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.