The Simplest Way to Make Keycloak and Lambda Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Keycloak and Lambda work together by pairing open source identity with serverless code execution. Keycloak issues signed user tokens, and AWS Lambda checks those tokens before running business logic. This workflow lets teams build scalable apps without managing servers or writing custom login code. Users log in once through Keycloak and trigger cloud functions based on their assigned roles. Teams keep identity rules in one place while running fast backend tasks on demand. How identity and per

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

Keycloak and Lambda work together by pairing open source identity with serverless code execution. Keycloak issues signed user tokens, and AWS Lambda checks those tokens before running business logic.

This workflow lets teams build scalable apps without managing servers or writing custom login code. Users log in once through Keycloak and trigger cloud functions based on their assigned roles. Teams keep identity rules in one place while running fast backend tasks on demand.

How identity and permissions flow between keycloak and lambda

The login flow starts when a user signs in to your app through Keycloak. Keycloak checks the user credentials against your directory or database. After a valid check, Keycloak sends back a signed JSON Web Token to the client. This token holds the user ID, group memberships, and role claims.

Next, the client app sends a request to an AWS API endpoint with that token. Before the Lambda function runs, an authorizer step checks the token signature. The authorizer fetches public keys from the Keycloak realm endpoint to verify the token. It also checks that the token is still active and comes from the right issuer.

Once the check passes, the request moves forward to the Lambda function. The function reads the verified user claims from the event context. Your code then uses these claims to decide what data to read or write. Meanwhile, the Lambda execution role controls which AWS resources the function itself can touch. This separates who the user is from what the cloud code can do.

Common failure modes and how to scope roles

Many teams struggle when they mix up user identity with cloud execution permissions. A common mistake is giving a Lambda function a broad IAM role with admin rights. If an attacker tricks the function, they gain full control over your cloud resources. Another failure happens when teams skip token checks inside the function logic. They check that a user logged in, but forget to check the specific user role.

Shared service accounts also create big blind spots in your system logs. When every function uses the same database user, you lose track of who changed data. If a bug or breach happens, your audit logs only show the shared account name. You should also watch out for long token lifetimes in Keycloak. If a token stays valid for days, revoked users can still run functions.

Keycloak + Lambda Execution Roles: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

To fix these issues, scope every role down to the bare minimum. In Keycloak, create clear roles for each job function and map them into token claims. Keep access tokens short so stolen credentials expire within minutes. On the AWS side, give each Lambda function its own small execution role. Allow the function to reach only the specific tables or queues it needs. Place your Keycloak servers and Lambda functions in private network subnets when they talk directly. Finally, log the Keycloak user ID alongside every database query or state change. When functions return sensitive records, masking rewrites matched fields like emails, phone numbers, or card numbers in results.

Keeping your serverless identity checks fast and safe

Network latency between AWS and your Keycloak cluster can slow down function cold starts. If every single Lambda run fetches public keys from Keycloak, your identity server gets overloaded. Instead, cache the public signing keys inside your authorizer or function memory. Refresh the cached keys only when a key rotation happens in your realm.

You should also monitor failed login attempts and rejected tokens in your central logs. High rejection rates often point to expired client secrets or broken token refresh flows. Clear alerts help your team spot and fix broken login paths before users complain.

Govern statements and responses across databases and APIs

Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.

First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.

Frequently asked questions

How does AWS Lambda verify a Keycloak token?

Lambda uses an authorizer or code library to check the token signature against Keycloak public keys. It also checks the expiration time and issuer URL before running the function.

Should Lambda fetch Keycloak public keys on every request?

No, fetching keys on every run adds delay and puts heavy load on Keycloak. Cache the public keys in memory and refresh them only when keys rotate.

How do we pass Keycloak user roles to backend databases?

Extract the user ID and role claims from the verified token inside your Lambda function. Pass those values into your audit logs and query context when talking to the database.

The query runs. The emails stay hidden.