Skip to content

AWS Bedrock Core Gateway - Lambda Interceptor for custom headers

0

I'm trying to handle some custom authentication to targets via a Lambda Request Interceptor. The hope was that an agent would authenticate for the inbound auth to AWS Bedrock Core Gateway. In the lambda interceptor I would initiate some custom auth and pass in a custom header in a non standard format like header - "auth":<token> and that would be passed on to the target. I have set passRequestHeaders set to true and I'm adding my custom header to the array of headers.

The issue is that I'm required to set an Outbound auth on the target of oAuth or ApiKey and create a static api key resource. If I set Location -> header, Parameter name -> "auth" it overwrites the apikey with the resource I was forced to create. In this instance I get below which means the wrong auth token was sent.

OpenAPIClientException - Error executing HTTP request for null: Client error: API request failed with status: 401 - { "message": "Unauthorized"} 

If I set the Location -> header, Parameter name -> "noHeader", it doesn't appear that my custom "auth" header is forwarded to the target. In this instance I get

OpenAPIClientException - Error executing HTTP request for null: Client error: API request failed with status: 401 - {
  "exceptionType": "SecurityException",
  "exceptionMessage": "Request for principal permission failed.",
  "exceptionTime": "12/18/2025 11:31:56 PM",
  "requestUri": "<url>,
  "exceptionStackTrace": null
}

which means the auth header wasn't sent at all. Although I do find the request for "null" interesting in the logs.

I can't imagine that every Gateway target will handle a single ApiKey or oAuth (client_id, client_secret) and there would be some means of dynamic auth allowed, but I can't seem to find it in documentation.

asked 10 months ago593 views

1 Answer
0

When working with AWS Bedrock AgentCore Gateway interceptors for custom authentication, there are a few key considerations to understand.

Gateway interceptors provide a way to implement custom authentication logic through Lambda functions. For your use case of passing custom authentication headers to targets, you can use a request interceptor to transform the request before it reaches the target.

To pass request headers to your interceptor Lambda function, you need to explicitly enable this by setting the passRequestHeaders parameter to true in your gateway configuration. This allows your interceptor to access the original request headers, including authentication tokens.

Here's how you can implement a solution:

  1. Configure your gateway with a request interceptor that has passRequestHeaders set to true.
  2. In your Lambda interceptor, extract the authentication information from the incoming request.
  3. Transform the request to include your custom authentication header in the format required by your target.

For example, your Lambda interceptor could look something like this:

def lambda_handler(event, context):
# Extract the gateway request
mcp_data = event.get('mcp', {})
gateway_request = mcp_data.get('gatewayRequest', {})
headers = gateway_request.get('headers', {})
body = gateway_request.get('body', {})

# Extract auth header or generate custom auth token
auth_header = headers.get('authorization', '') or headers.get('Authorization', '')

# Return transformed request with custom auth header
response = {
"interceptorOutputVersion": "1.0",
"mcp": {
"transformedGatewayRequest": {
"headers": {
"Accept": "application/json",
"auth": auth_header,  # Your custom auth header
"Content-Type": "application/json"
},
"body": body
}
}
}
return response

When implementing this approach, be aware of security best practices:

  • Be careful with handling sensitive information in headers
  • Ensure your gateway execution role has only the necessary permissions
  • Implement idempotent Lambda functions for your interceptors

While AgentCore Gateway does require configuring outbound auth methods like OAuth or API Key for targets, the interceptor approach allows you to dynamically modify the authentication information before it reaches the target, giving you more flexibility than the static configurations alone.
Sources
Apply fine-grained access control with Bedrock AgentCore Gateway interceptors | Artificial Intelligence
Configuration - Amazon Bedrock AgentCore
Using interceptors with Gateway - Amazon Bedrock AgentCore

answered 10 months ago

  • I feel like this answer is what I have already tried.

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.