To secure AWS API Gateway and Lambda for a FHIR API, the gateway has to answer one question on every call: does this token allow this request for this patient? SMART on FHIR scopes carry the answer, but only if a Lambda authorizer reads them correctly and API Gateway caches the result safely.
Startup founders and product or engineering leaders exposing FHIR data through API Gateway and Lambda will find how SMART scopes are written, how an authorizer maps requests to scopes, where patient context must be enforced and how to test it all.
What does it take to secure AWS API Gateway and Lambda for a FHIR API?
Securing AWS API Gateway and Lambda for a FHIR API takes a Lambda authorizer that validates each access token, maps the HTTP method and path to the SMART scope the request needs, and allows the call only when the token holds that scope. The backend then enforces patient context and records an audit event.
Putting a FHIR API behind API Gateway?
We build the authorizer, scope mapping and backend checks for SMART on FHIR clients.
A valid token is not enough on its own. A token issued to an app that may only read a patient's lab results should be refused when that app tries to write a medication or read another patient's record.
What does the authorizer have to decide?
The authorizer has to decide whether the caller is who the token says, and whether the token permits this specific request.
- Token checks: signature against the authorization server's published keys, issuer, audience, expiry and not-before time.
- Scope check: the resource type and interaction in the request, matched against the scopes in the token.
- Context check: the patient or user the token was issued for, passed on for the backend to enforce.
- Decision: an allow or deny policy, plus a context object the backend can read.
How are SMART scopes written?
SMART scopes name a context, a resource type and the interactions allowed, so the authorizer can compare them with the request.
The SMART App Launch implementation guide (version 2.2.0) builds clinical data scopes from a context, a resource type and permissions taken in order from c, r, u, d and s, meaning create, read, update, delete and search. Its own example, patient/Observation.rs, grants read and search on Observations.
The same guide says servers should process a v1 .read scope as v2 .rs, .write as .cud and .* as .cruds, and that scope requests with undefined or out-of-order interactions may be ignored, replaced with server defaults or rejected.
Normalize every token's scopes into one internal form before checking them, so v1 and v2 tokens follow the same rules. Decide in advance how the authorizer handles scopes it does not recognize, and log them.
How does a request map to a scope?
A request maps to a scope through its HTTP method, its path and whether it targets one resource or a search.
| Request | FHIR interaction | Permission letter needed |
|---|---|---|
| GET /Observation/{id} | read | r on Observation |
| GET /Observation?patient={id} | search | s on Observation |
| POST /Observation | create | c on Observation |
| PUT /Observation/{id} | update | u on Observation |
| DELETE /Observation/{id} | delete | d on Observation |
Paths that fall outside this pattern, such as the metadata endpoint, history, operations or batch bundles, need explicit rules. A batch bundle can contain many interactions, so either refuse it at the gateway or let the backend check each entry against the scopes.
Where should patient context be enforced?
Patient context should be checked twice: roughly at the gateway and precisely in the backend, because the authorizer sees only the method, path and parameters.
A patient-level scope limits access to the patient in the launch context. The authorizer can reject a request whose path or patient parameter names a different patient, but searches with chained parameters, includes or references can still reach other records. The FHIR backend has to filter every result to the patient compartment named in the context the authorizer passes on.
How does the request flow through the gateway?
The request flows from the SMART app to API Gateway, through the Lambda authorizer and on to the FHIR handler, with each step adding a check.
- SMART app: sends the bearer token with each FHIR request.
- API Gateway: applies throttling and calls the authorizer before any backend code runs.
- Lambda authorizer: validates the token, matches the request to a scope and returns a policy and context.
- FHIR handler: filters data to the patient in context, applies business rules and writes an audit event.
How should the authorizer handle signing keys?
The authorizer should fetch the authorization server's public signing keys from its published key set, cache them in memory between invocations and refresh them when a token arrives with a key identifier it has not seen.
Never accept a token whose algorithm differs from the one you expect, and never fall back to an unsigned token. Set a short network timeout on the key fetch, so a slow identity provider produces a clear denial instead of a hung request.
What are the authorizer caching traps?
Authorizer caching saves a Lambda call per request, but a cached policy is reused for later requests, so it has to be right for all of them.
AWS documents that an API Gateway Lambda authorizer takes the caller's identity as input and returns an IAM policy and a principal identifier. With authorization caching on, API Gateway caches the policy and skips the authorizer on later calls, so AWS tells builders to make sure the policy applies to all resources and methods across the API.
If the policy allows only the method and path of the first request, the next request with the same token may be wrongly refused. If it allows everything, scope checks are skipped for every later call. Three safer patterns exist.
- Full policy: build a policy listing every method and resource the token's scopes allow, and cache that.
- Fine-grained cache key: use a request authorizer with the token, method and path as identity sources.
- No cache: call the authorizer on every request and cache key material and token checks inside the function.
Whichever pattern you choose, keep the cache time shorter than the token lifetime, and remember a cached allow stays valid until it expires even if the token is revoked.
What should the authorizer pass to the backend?
The authorizer should pass the facts the backend needs to enforce access and write audit records, so the handler never parses the token again.
- The patient identifier from the launch context, if any.
- The normalized scopes and the client identifier.
- The user identifier, when a person is signed in.
- A request or correlation identifier for logs.
What else protects the gateway and functions?
Beyond scopes, the gateway and functions need the ordinary cloud controls that keep a public API safe.
- Throttling and usage plans per client, so one app cannot exhaust capacity.
- A web application firewall for common attack patterns and request size limits.
- Least-privilege IAM roles for each function, with access only to the tables and keys it uses.
- Logs that record decisions and identifiers without writing PHI or full tokens.
How do you test the authorizer?
Test the authorizer with a matrix of tokens and requests, so every scope, interaction and resource type has an expected allow or deny.
- Each permission letter, alone and combined, against each interaction.
- Version 1 scopes such as .read and .write, normalized and checked.
- Expired, wrong-issuer, wrong-audience and badly signed tokens.
- A patient-level token asking for a different patient by path and by search parameter.
- Several different requests with one token while caching is on.
Run the matrix against a deployed stage as well as in unit tests, because caching, identity sources and gateway responses only behave for real once API Gateway sits in front of the function.
A worked example: a patient app reading lab results
A patient app that shows lab results shows how the checks combine on one request.
- The app holds a token with patient/Observation.rs and a launch context for one patient.
- It calls a search for that patient's laboratory Observations. The authorizer matches search to the s letter, confirms the patient parameter equals the context patient and allows the call.
- The handler runs the search, filters results to the patient compartment and writes an audit event with the client and patient identifiers.
- The same app then tries to create an Observation. The token holds no c letter, so the authorizer denies the request before any backend code runs.
How do teams build it step by step?
- Step 1, list the FHIR interactions and resource types the API exposes.
- Step 2, write the mapping from method and path to required scope, including special endpoints.
- Step 3, build the authorizer: token validation, scope normalization and policy generation.
- Step 4, choose the caching pattern and set cache keys and time limits.
- Step 5, enforce the patient compartment and write audit events in the FHIR handler.
- Step 6, run the test matrix in CI and against a deployed stage.
What are the common mistakes when securing API Gateway and Lambda for FHIR?
The common mistakes when securing API Gateway and Lambda for FHIR come from trusting the token's validity instead of its contents.
- Valid token, any request: the authorizer checks the signature and ignores scopes.
- Cached single-route policy: later requests are wrongly allowed or refused.
- Patient context only at the gateway: searches and includes leak other patients' data.
- Batch bundles passed through unchecked: one allowed request hides many disallowed ones.
- Tokens in logs: full bearer tokens written to request logs.
What does this mean for your build plan?
Plan the scope mapping and the patient compartment rules before writing handler code, because every endpoint depends on them. The test matrix grows with each resource type, so automate it from the start.
- Agree with the authorization server team which scopes and token claims will be issued.
- Budget for special endpoints, such as batch, history and operations, which need their own rules.
- Include authorizer latency in performance tests, since it runs before every uncached request.
How does Agnotic secure FHIR APIs on AWS?
Agnotic Technologies builds the Lambda authorizer, scope mapping and caching strategy, enforces patient context in the FHIR handlers, sets up throttling and logging and automates the authorization test matrix. Your SMART on FHIR clients get exactly the access their scopes grant.
Make every FHIR request prove its scope
Authorizer, cache and backend checks designed together.
Related reading on Agnotic
FHIR security, SMART scopes and AWS.


