Stop Storing OAuth Secrets in Lambda: A First Look at Credential Broker
Most Serverless projects I have seen hit the same wall at some point. A Lambda function needs to talk to something outside AWS: a SaaS ticketing tool, a partner API, a service running in Azure. That means an OAuth token
Most Serverless projects I have seen hit the same wall at some point. A Lambda function needs to talk to something outside AWS: a SaaS ticketing tool, a partner API, a service running in Azure. That means an OAuth token or an API key. Soon there is a client secret in an environment variable, a token cache in a DynamoDB table and a slightly different refresh routine in every other function. Nobody likes writing that glue code, and even fewer people like maintaining it.
AWS IAM Credential Broker, in public preview since November 29, 2026, is aimed squarely at this problem. I tried it with a TypeScript Lambda function and the Serverless Framework and wrote up the full walkthrough here: IAM Credential Broker: OAuth for Lambda without secrets. Below is the short version.
The idea in one sentence
Your workload asks the broker for a token using its normal IAM role, and the broker takes care of obtaining, caching and refreshing it. Client secrets and API keys stay in Secrets Manager and never show up in your code or configuration.
An important detail: the broker does not issue tokens itself. That remains the job of the external authorization server, such as Okta, Entra ID or a vendorβs own OAuth server. The broker handles everything around it. According to the documentation, it keeps a cached token for at most an hour, or less if the token expires sooner.
There are two resource types. A credential provider stands for exactly one credential, for example βOAuth with vendor Xβ. A credential provider pool groups providers and holds shared settings like the KMS key.
Which flow fits your case
The broker supports three OAuth flows plus static API keys:
- Client credentials (M2M): the application acts on its own behalf, like a nightly job.
- Authorization code (3LO): a person consents to access to their data.
- Token exchange (OBO): the personβs identity is already known and the app acts for them.
- API key provider: for services that only understand a static key.
The two person-centric flows rely on trusted identity propagation, so a token is only handed out for requests that really come on behalf of that person. That makes the broker especially interesting for AI agents that should never act with more rights than the human behind them.
It is not for everything, though. Agents on Bedrock AgentCore should keep using AgentCore Identity, and if the external system accepts a JWT signed by AWS, outbound identity federation is the leaner option because no client secret exists at all.
What it looks like in Lambda
In my example, a fictional bike rental runs a nightly function that checks a parts catalog (API key) and creates work orders in a maintenance planner (OAuth client credentials). The function only knows two provider ARNs. Fetching a token boils down to this:
import { CredentialBrokerClient, GetOAuthTokenCommand } from "@aws-sdk/client-credentialbroker";
const client = new CredentialBrokerClient({});
const res = await client.send(new GetOAuthTokenCommand({
credentialProviderArn: process.env.MAINTENANCE_PROVIDER_ARN,
grantType: "CLIENT_CREDENTIALS",
scopes: ["workorders:write"],
}));
// use res.accessToken as Bearer token
The real win is the IAM policy. The functionβs own role is allowed exactly credentialbroker:GetOAuthToken on one provider and credentialbroker:GetApiKey on another. No secretsmanager:GetSecretValue, no wildcards. Every retrieval is an IAM call you can restrict and trace in CloudTrail.
Things to keep in mind
It is a preview. During the preview there is no extra charge according to AWS, but pricing after general availability has not been announced, and the APIs may still change. My rule for preview services: experiment yes, business-critical production after GA.
A static API key is still returned in plain text, so the broker centralizes management and permissions but does not make that key short-lived. And at launch I could not find a CloudFormation resource type for pools and providers, so the setup currently runs through a script.
I also see some fragmentation: AgentCore Identity, outbound identity federation and now the broker solve related problems, and it takes a moment to understand which one fits.
What the full article covers
- A complete setup script in TypeScript that creates the pool, an OAuth provider and an API key provider
- A small credential wrapper with in-memory caching, 401 handling and unit tests without an AWS account
- The full Lambda handler and a least-privilege
serverless.ymlwith one role per function - Typical errors during the preview (AccessDenied, providers stuck in FAILED, wrong
jsonKey) and how to fix them
If you have a function that still reads a client secret from an environment variable, the full walkthrough on sascha-ms.de shows you how to swap it out step by step.
π This is the condensed version. The full article on sascha-ms.de goes deeper β with all code samples, diagrams and a short knowledge check.
π Next step: Curious what your own Lambda functions cost? Try my Lambda cost calculator β it runs right in your browser.
π¬ Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).
Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.