Skip to content

Getting 404 Errors from API Gateway Integrated with Keycloak and Lambda for Weighted Routing

0

We are using AWS API Gateway for routing and traffic distribution and have integrated it with Keycloak for authentication purposes. Below is a brief overview of our architecture and the issue we are facing.

Architecture Workflow

  1. API Gateway Call Initiation A request is made to the API Gateway, which acts as the entry point.

  2. Lambda Function 1 – Keycloak Authentication

  • A Lambda function is used as a custom authorizer.
  • This function verifies the client_id and client_secret against Keycloak.
  • The authorizer is configured and attached to both GET and POST methods of the API.
  1. Lambda Function 2 – Weighted Routing Logic
  • Once authenticated, the request is passed to a second Lambda function.
  • This function is responsible for weight-based routing, where it forwards requests to different target endpoints based on pre-defined weights.

Issue Everything was working correctly a few weeks ago. However, upon resuming testing last week, we started receiving HTTP 404 errors from the API Gateway.

We haven’t made any changes to the code or infrastructure that would cause this issue. We are looking for guidance on how to debug or resolve the 404 errors that are now occurring.

Note: Even if I disable the Authentication process, it throws the same error. But the Test API from specific resources is working fine and showing a response. Verified the logs in CloudWatch, could see that the Routing Lambda is getting executed successfully but it throws 404 immediately after successful execution of the lambda.

1 Answer
0

Based on your description, it sounds like you're experiencing a 404 error from API Gateway despite your Lambda functions executing successfully. This is a common issue that can occur for several reasons.

Since you mentioned that the Lambda functions are executing correctly (as evidenced by CloudWatch logs) but you're still getting a 404 error, the issue is likely related to how API Gateway is handling the response from your Lambda function or how the integration is configured.

Here are some potential causes and solutions:

  1. Lambda Response Format: Ensure your Lambda function is returning a response in the format that API Gateway expects. API Gateway requires a specific structure for Lambda proxy integrations.

  2. Route Configuration: Verify that the API Gateway routes are correctly configured to match the endpoints you're calling. The 404 error suggests that API Gateway cannot find the route you're trying to access.

  3. API Deployment: Check if you've deployed your API after making any recent changes. Until you deploy your API, the changes will not be live and accessible from the front end.

  4. Lambda Permissions: Ensure your Lambda function has the necessary IAM permissions to be invoked by API Gateway. You need to add the lambda:InvokeFunction permission to your API Gateway role.

  5. Integration Response Mapping: If you're using a non-proxy integration, check your integration response mappings to ensure they're correctly configured.

  6. Logging Enhancement: Add more detailed logging in your API Gateway by including variables like $context.error.message, $context.error.responseType, and $context.integrationErrorMessage to get more information about what's happening.

  7. API Gateway Stage: Confirm you're calling the correct stage of your API Gateway (e.g., prod, dev).

Since you mentioned that testing the API from specific resources works fine, it suggests that the issue might be related to how the client is calling the API or how certain routes are configured. The fact that disabling authentication doesn't resolve the issue further indicates that the problem is likely in the API Gateway configuration or the Lambda response handling, not in the authentication flow.

I recommend checking these areas systematically to identify and resolve the 404 error.
Sources
AWS Gateway: API does not respond in Postman | AWS re:Post
POST Request Made from Within Lambda Only Receiving a Not Found Response | AWS re:Post
Troubleshooting issues with HTTP API Lambda integrations - Amazon API Gateway

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.