Authentication with Cognito
Goal
Protect your API Gateway endpoint with a Cognito User Pool authorizer so that only signed-in users can invoke the inference Lambda. You will create an edge Lambda that validates payloads, reads JWT claims for caller identity, and invokes your core Lambda via boto3. The React frontend attaches the Cognito ID token using Amplify Gen 2's fetchAuthSession.
Contract
The frontend sends the same Lambda Contract request body as before, but now includes an Authorization header containing the Cognito ID token (JWT).
API Gateway verifies the JWT before the request reaches your edge Lambda. The edge Lambda receives the verified claims at:
event["requestContext"]["authorizer"]["jwt"]["claims"]The edge Lambda validates the request body, invokes the core Lambda with the contract-conforming payload, and returns the core Lambda's response to the browser.
Required Reading
HTTP API JWT authorizers: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-jwt-authorizer.html
API Gateway HTTP API payload format v2.0 —
requestContext.authorizer: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html#http-api-develop-integrations-lambda.proxy-formatAmplify Gen 2
fetchAuthSession: https://docs.amplify.aws/react/build-a-backend/auth/connect-your-frontend/sign-in/#sign-in-with-an-external-identity-provider
Constraints
Use an API Gateway JWT authorizer configured with your Amplify Cognito User Pool — not a Lambda authorizer.
The edge Lambda runtime must be Python 3.12.
The edge Lambda must validate the request body against the contract fields (
question,ticker,year,period) and return HTTP 400 with missing field names if validation fails.The edge Lambda must invoke the core Lambda using boto3's
invokewithInvocationType="RequestResponse". Check theFunctionErrorkey in the response to detect core Lambda failures.The edge Lambda's IAM role must have an inline policy granting
lambda:InvokeFunctionon the core Lambda only — not a wildcard.The frontend must use
fetchAuthSessionfromaws-amplify/authto retrieve the ID token, and pass it in theAuthorizationheader. Use the Amplify Gen 2 configuration pattern (amplify_outputs.json) and Vite environment variables (import.meta.env.VITE_*).CORS allowed headers must include
Authorization.
Acceptance Criteria
Hints
Creating the JWT authorizer
In the API Gateway console under Authorizers, create a JWT authorizer. The issuer URL is your Cognito User Pool's URL:
The audience is your User Pool App Client ID. Attach this authorizer to your POST /inference route.
Reading claims in the edge Lambda
With a JWT authorizer on an HTTP API (payload v2.0), verified claims arrive at:
This path is specific to HTTP API v2.0. REST APIs use a different event structure.
Detecting core Lambda errors
After calling lambda_client.invoke(...), check for the FunctionError key:
Read the Payload stream for the error details.
Security options beyond JWT authorizer (advanced — skim)
Lambda authorizer: Custom logic (e.g., allow-listed emails, API keys) — more flexible but more code to maintain.
IAM auth with federated identities: Strongest, but adds SigV4 client complexity.
Public endpoint: CORS-limited only; no identity verification. Acceptable for non-sensitive demos but never for production.
Common pitfalls
CORS mismatch after adding auth: You must add
Authorizationto theAccess-Control-Allow-Headerslist in your HTTP API CORS config. Without it, the browser's preflight fails.Wrong token type: The JWT authorizer expects the ID token, not the access token.
fetchAuthSessionreturns both — usetokens.idToken.Edge Lambda timeout: Set to at least 15 seconds. The core Lambda may take time; ensure the edge Lambda's timeout exceeds the core Lambda's expected duration while staying under API Gateway's 30-second limit.
Missing invoke permission: The edge Lambda's role needs
lambda:InvokeFunctionon the specific core Lambda ARN. A generic Lambda role does not include this.
Last verified: 2026-06
Last updated