AWS Builder Center
Encrypted ingestion per tenant, and let KMS keep your agents honest

Encrypted ingestion per tenant, and let KMS keep your agents honest

A new open-source sample shows how to ingest tenant documents into a multi-tenant agent platform so that tenant A's files cannot reach tenant B's agent, even when the application code is wrong.

The problem

Agents eat documents. A loan-processing agent reads loan files, a legal agent reads contracts, a claims agent reads case packages. In a multi-tenant agent platform all of those files arrive through the same door: one bucket, one pipeline, one set of Lambda functions. The platform then promises every tenant that its files are its own.

The usual way to keep that promise is a tenant check in code. The upload handler writes under  tenant-a/ . The processor reads the tenant id from the row and only opens  tenant-a/  objects. The agent's retrieval tool filters by tenant id before it hands documents to the model.
Every one of those checks is a line of code, and every line of code can be wrong, skipped, or refactored away. A missed  WHERE tenant_id = ?  in a retrieval query is enough. With an agent in the loop, the failure is worse than a data leak in a report: the agent reads the other tenant's contract and acts on it.

The sample  Per-tenant encrypted ingestion for agentic applications , the fourth pattern in the Agentic AI Acceleration Patterns library, moves the tenant boundary out of the code. Each tenant's documents are encrypted with that tenant's own AWS KMS key, and the key policy and IAM decide who can read what. A bug in application code can still ask for the wrong object. It gets a 403 instead of the bytes.

What you get

The sample ships as one AWS CDK construct,  TenantEncryptedIngestion , plus a demo stack, an integration test and an example validator.




The five numbered hops are the data path a tenant's document takes. Each box names the pattern it carries. The API hands out the URL; it never touches the bytes.

Drop the construct into your own stack and you get:
  • A per-tenant upload path. A tenant user calls  CreateDelivery  with an access token and gets back a presigned, write-once S3 PUT. The tenant's KMS key is fixed in the signed headers. The user uploads straight to S3; the API never touches the bytes.
  • A pipeline that validates before it lands. An S3 event wakes a processor that runs under a session scoped to that one tenant. It checks the object structurally (size, type, magic bytes, hash), runs your own validator chain, and only then copies the document to its final location under the tenant's key and publishes a  DeliveryAccepted  event for the agents downstream.
  • Two key tiers behind one contract.  vendor-key : you own the key, one per tenant.  tenant-key : the key lives in the tenant's own AWS account, the tenant can revoke it at any time, and when they do, the pipeline fails closed. Regulated customers tend to ask for the second one; the sample makes it a tenant attribute, not a second architecture.
  • Cryptographic offboarding. Offboarding a tenant schedules the key for deletion. Every object encrypted with it becomes unreadable, including backups and copies you forgot about.
  • Per-tenant observability and cost attribution. Every log line carries the tenant id. A registry keeps per-tenant counters (bytes stored, deliveries, KMS requests). Contributor Insights rules rank tenants without a metric per tenant. Vendor keys carry a  TenantId  cost allocation tag. The integration test checks that all three views agree.
Everything is TypeScript and CDK: 315 unit tests, a threat model with 39 threats and their mitigations, 25 architecture decision records, and an integration test that deploys, exercises 17 steps against two AWS accounts, and tears down.

Three design decisions worth knowing about

The repository records every decision as an ADR. Three of them shape the whole sample.

1. The wall is in KMS and IAM, and it is a Deny

It would be easy to give the pipeline's role  kms:Decrypt  on every tenant key and trust the code to pick the right one. The sample does the opposite.
Each tenant key gets a deny-first key policy written once at creation. The Allow side lets the three pipeline roles use the key only through S3, only for objects under that tenant's two prefixes ( ingest/<tenantId>/  and  documents/<tenantId>/ ). The Deny side refuses every other use, including administration by anyone except two named roles with explicit action lists. Nobody holds  kms:PutKeyPolicy  on the key, so nobody can quietly widen it later.
On the identity side, each pipeline function runs under one tenant role per function whose policy uses  ${aws:PrincipalTag/TenantId}  as a variable: the role may touch  ingest/${aws:PrincipalTag/TenantId}/*  and keys tagged with that tenant id, and nothing else. The tenant id arrives as a session tag that only the pipeline's own base role can set, never the tenant.
That gives two independent walls. The integration test knocks each one down on its own, with the other still standing, and checks that the request is still refused. If a future change weakens the key policy, the identity policy still holds, and the test says so.

2. Bring your own key is a first-class tier, not a special case

Sooner or later a regulated tenant asks the question every platform vendor dreads: "Can we hold the key?" The honest answer is usually "that would be a different product." BYOK lands as a parallel code path, a separate onboarding runbook, and a list of features that do not work for those tenants. It ships late, it drifts from the main path, and the first time the tenant revokes the key, something in the pipeline keeps going because nobody tested that.
This sample starts from the other end. The tenant owning the key is one of two equal tiers on the same contract.  vendor-key  means the vendor creates and owns a key per tenant.  tenant-key  means the key lives in the tenant's own AWS account, with a key policy the tenant writes and can change at any time. The tier is a single attribute on the tenant record. The upload path, the pipeline, the validator chain, the events and the offboarding flow do not branch on it. Only  CreateTenantKey  and  DeleteTenantKey  know the difference, and  RotateTenantKey  tells a  tenant-key  tenant that rotation is theirs to do.
Onboarding a BYOK tenant is four steps on their side: create a symmetric key in their account, tag it  TenantId=<their id> , paste in the key policy the sample renders for them (it names the pipeline roles, nothing else), and hand over the key ARN.  CreateTenantKey  then checks the key is the right kind, in the right Region, in an account that is not the vendor's, tagged for that tenant, and usable through S3. A key that fails any check is refused with a 422 that names the reason. Nothing is written until the key works.
Treating BYOK as first class forces the hard question: what happens when the tenant uses the control they asked for? When a  tenant-key  tenant disables their key or removes the pipeline from the key policy, S3 returns a 403 somewhere in the middle of a delivery. The processor cannot tell from that error whether the key is gone or something else broke, so it runs a probe: one tiny PUT and GET under the tenant's own prefix with the tenant's key. If the probe fails with a KMS error, the delivery is marked  KEY_UNAVAILABLE , the key is marked  REVOKED , new uploads for that tenant are refused, and a  DeliveryKeyUnavailable  event tells the downstream agents why nothing is arriving. The hourly sweep re-probes, and the tenant is back to  ACTIVE  the moment their key is. No exception path can land a document under a key the tenant has withdrawn, and the integration test does exactly this to tenant B, twice, to prove it.
The same symmetry applies to offboarding. A  vendor-key  tenant leaves and the vendor schedules the key for deletion. A  tenant-key  tenant leaves and the vendor deletes its own copies and registry rows, but the key is the tenant's and is left untouched; the tenant decides what happens to it.
The cost of giving tenants this control is small: about one dollar per key-month, paid by whoever owns the key. The value is that "can we hold the key?" becomes a checkbox at onboarding, not a roadmap item.

3. Your validators, in the pipeline, with no keys to hold

Nothing should reach an agent's context window without being checked first. What "checked" means is yours to decide: a malware scan, a page limit, a schema check on a JSON export, a classifier that refuses documents that look like they belong to a different customer. The sample does not try to guess. It does the structural checks every document needs (declared size and type match the bytes, magic bytes, a hash) and then hands the document to a chain of validators you write.
A validator is a Lambda function and one line of CDK:
ts
validators: [ new LambdaValidator(pageLimitFn, { name: 'pdf-pages', timeout: Duration.seconds(30) }), new LambdaValidator(scanFn, { name: 'malware', timeout: Duration.minutes(5) }),],
The contract is small and versioned JSON. The processor calls each validator in order with the tenant id, the delivery id, the declared metadata, the hash, and the verdicts of the validators before it. The validator answers  ACCEPT , or  REJECT  with a code and a short message. The chain stops at the first  REJECT , and the delivery is marked  REJECTED  with the reason  <validator>:<code> , so the tenant sees  malware:INFECTED  or  pdf-pages:TOO_MANY_PAGES  and knows what to fix.
Two things make this safe to open up to arbitrary code.
The validator holds no permission. It never sees a KMS key, a bucket name, or a role it can assume. It gets one presigned  GET  for the one object it is judging, signed under the tenant-scoped session, good for five minutes, and pinned to the object's ETag with  If-Match . If the object changed under it, the GET fails. A validator written by a contractor, a third-party scanning service in another account, or a function you reuse from another project cannot read a second object even if it tries. The deployer grants the processor  lambda:InvokeFunction  on the validator and nothing else, in either direction.
An error is never an accept. A validator that throws, times out, or returns a verdict outside the schema is a validator error, not a pass. The message is retried through SQS, and on the last receive the delivery is rejected as  <validator>:VALIDATOR_ERROR  and the message lands in the dead-letter queue for a human. The integration test flips the demo's test validator into  throw  mode and  sleep  mode to prove both paths end in a rejection, not a landed document.
Around the chain, the pipeline pins everything so a validator's verdict stays attached to the bytes it saw: the upload URL is write-once, the processor holds a lease on the delivery row, and the final copy into  documents/  carries the same ETag as a precondition. A verdict on one version of a file can never land a different one.
The repository ships one example validator, a PDF page limit in under forty lines of code, as a template. The two you will probably write first are a malware scan and a classifier, and both fit the same shape.

When this fits

This pattern fits when a platform ingests documents on behalf of many tenants and needs to show a security team, not tell them, that tenants are separate. It fits especially well when some tenants will ask to hold their own keys.
It is deliberately not a transfer endpoint, not client-side encryption, not document understanding and not an identity provider. The construct verifies an access token any OIDC issuer can mint; the demo pairs it with the library's  tenant-identity  sample. The seams for a sealed-drop tier and for AWS Transfer Family are named in the spec.

Try it

The repository has a demo stack you can deploy in a test account, a  demo.sh  that walks one delivery end to end, and the integration test that proves each wall on its own:
Start with the README's components diagram and the  architecture.md  status block, then read ADR-0002, ADR-0004 and ADR-0008. They are the three decisions above, with the options that were rejected and why.
The earlier patterns in the same library cover the control plane, tenant identity and runtime isolation for agents on Amazon Bedrock AgentCore. This one feeds them.
Questions or feedback? Let me know!
Cheers, Dave
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article