Cloud Engineer: Securing Private B2B Quoting Endpoints with AWS API Gateway Custom Token Authorizers
Exposing high-value B2B pricing and supply-chain quoting APIs to partner ERP systems requires strict authentication, tenant-scoped rate limiting, and zero-trust context propagation before traffic ever reaches internal compute instances or triggers expensive third-party supplier lookups. As a Cloud Engineer and Backend Engineer, I design low-latency AWS API Gateway Custom Lambda Authorizers that verify asymmetric RS256/EdDSA JWTs against cached JWKS keysets and inject verified tenant context headers into downstream microservices.
1. JWKS-Cached Asymmetric Token Verification & IAM Policy Generation
Instead of performing a synchronous database round-trip on every API request, our Lambda Authorizer validates the cryptographic signature of the partner's bearer token against a warm in-memory JWKS cache, verifies custom tenant claims, and returns a cached IAM policy document along with an immutable context map passed directly to upstream services:
import time
import jwt
from jwt import PyJWKClient
JWKS_URL = "https://auth.internal.example.com/.well-known/jwks.json"
jwk_client = PyJWKClient(JWKS_URL, cache_jwk_set=True, lifespan=3600)
def lambda_handler(event: dict, context: object) -> dict:
auth_header = (event.get("headers") or {}).get("authorization", "")
if not auth_header.lower().startswith("bearer "):
raise Exception("Unauthorized")
token = auth_header.split(" ", 1)[1].strip()
try:
signing_key = jwk_client.get_signing_key_from_jwt(token)
claims = jwt.decode(
token,
signing_key.key,
algorithms=["RS256"],
audience="https://api.quoting.internal",
options={"require": ["exp", "sub", "tenant_id", "tier"]},
)
except jwt.PyJWTError as exc:
raise Exception("Unauthorized") from exc
return {
"principalId": claims["sub"],
"policyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Allow",
"Resource": event["methodArn"],
}
],
},
"context": {
"tenant_id": str(claims["tenant_id"]),
"pricing_tier": str(claims["tier"]),
"verified_at": str(int(time.time())),
},
}
2. Defense-in-Depth: Usage Plans, mTLS & VPC Link Private Integrations
By enabling authorizer policy caching (`TTL = 300s`) keyed on the Authorization header and routing API Gateway traffic over an internal AWS VPC Link to private Application Load Balancers, backend containers never expose public IPv4 addresses—aligning with our Cloud Engineer AWS & GCP Terraform architecture and Edge API Gateway integration testing patterns.
Need a Cloud Engineer to harden your AWS API Gateway, mTLS, and custom authorizer infrastructure? Book a 1-Day Engineering Sprint.