Disclaimer: AI was used to produce the diagram, code and to check my language. But the idea and structure defined by me.
Web Functions lets you use either IAM_AUTH or APPLICATION_MANAGED . The latter means the HTTP Server/framework handles authentication, while IAM_AUTH assumes the role or gets temporary credentials via STS.
In this blog post, we want to represent a real-world scenario where the client is receiving
What is OIDC anyway?
OIDC (OpenID Connect) is an authentication layer built on top of OAuth 2.0. They complement each other:
Protocol | Answers | Main Token |
|---|
OAuth 2.0 | "What resources may this application access?" | Access Token, presented on API |
OIDC | "Who is signed in?" | ID token, containing verified identity claims |
OIDC adds standardized identity information and validation to OAuth’s existing flows. A client requests it by including the openid scope.
In a classic frontend-to-backend, one authorization code + PKCE (pron.: "pixie") flow provides both:
- The frontend uses the ID token to establish who signed in.
- The frontend sends the access token to your Web Function, which verifies it and checks route permissions.
ID tokens are for the client; access tokens are for the API.
OIDC with Cognito
We want to use Amazon Cognito:
Example ExpressJS
We will use my construct that I described in part 1 of this series. I chose ExpressJS because it is a popular HTTP server. You can find more examples here.
We need token verification because Cognito issuing a token doesn’t automatically protect your Web Function.
By choosing APPLICATION_MANAGED each request reaches the application without AWS checking the bearer token. Anyone could send a forged or expired token—the backend must decide whether to trust it. That is why we need to add aws-jwt-verify .
import { CognitoJwtVerifier } from "aws-jwt-verify";
const verifier = CognitoJwtVerifier.create({
userPoolId: config.userPoolId,
clientId: config.clientId,
tokenUse: "access",
})
aws-jwt-verify checks:
- Signature: Cognito signed the token; its contents weren’t changed.
- Issuer: It came from your configured User Pool.
- App client: It was issued for your configured frontend client.
- Expiration: It is still valid.
- Token type: It is an access token, not an ID token.
It also fetches and caches Cognito’s public signing keys and handles signing-key changes, avoiding a Cognito call on every request.
Then we can get the verifier to match the Bearer Token
const authenticate = async (authorization: string) => {
const match = /^Bearer\s+(\S+)$/i.exec(authorization ?? "");
if (!match) return null;
try {
const claims = await verifier.verify(match[1]!);
return { userId: claims.sub, groups: claims["cognito:groups"] ?? [] };
} catch {
// Fail closed. Never return JWT contents or verification details to callers.
return null;
}
};
Then we can construct a RequestHandler object to protect the endpoints.
const protectedRoute: RequestHandler = async (req, res, next) => {
const user = await options.authenticate(req.get("Authorization"));
if (!user) {
res.set("WWW-Authenticate", "Bearer").status(401).json({ error: "Unauthorized" });
return;
}
res.locals.user = user;
next();
};
It works similarly for Hono and Node.js.
Conclusion
It is always good to secure your Web Function endpoints. In this blog post, I showed how you can do that with Amazon Cognito. Use aws-jwt-verify as a convenient package that accepts only tokens issued by AWS.
In the next blog in this series, I want to test if we can use Meta frameworks such as Astro, Next.js, and the new fancy TanStack Start.
Loading article