Skip to content

Proxying to bedrock agent core from API Gateway

0

I am trying to proxy from API gateway v2 to AgentCore without using a lambda route. The reason is that if I use a lambda the lambda will sit there eating up time waiting for the agent to continue.

The problem is that I need to pass the Authorization header through to the runtime and I don't know how. Authorization is a restricted header.

What I am attempting is this:

` private addAgentRoute() { const stack = Stack.of(this);

    // Use CloudFormation intrinsic function to substitute the ARN at deploy time
    const invokeUrl = Fn.join('', [
        'https://bedrock-agentcore.',
        stack.region,
        '.amazonaws.com/runtimes/',
        Fn.join('%3A', Fn.split(':',
            Fn.join('%2F', Fn.split('/', this.runtime.attrAgentRuntimeArn))
        )),
        '/invocations'  // ← Remove the query parameters entirely
    ]);

    const mapping = new ParameterMapping()
        .appendQueryString('runtimeSessionId', MappingValue.requestBody('sessionId'));



    const agentIntegration = new HttpUrlIntegration("AgentIntegration", invokeUrl, {
            method: HttpMethod.POST,
            parameterMapping: mapping,
            timeout: Duration.seconds(29)
        }
    );

    this.addRoutes({
        path: '/chat',
        methods: [HttpMethod.POST],
        integration: agentIntegration,
        authorizer: this.authorizer
    });
}

`

You can't add Authorization to the Parameter Mapping because it's restricted. Should API Gateway be passing Authorization through by default?

I also tried to see if there was some way I could do it with a different integration, and have the runtime protected by IAM instead of OAuth. I couldn't figure out a way to make that work, either.

2 Answers
0

When proxying from API Gateway v2 to AWS Bedrock AgentCore, you're facing a challenge with passing the Authorization header because it's a restricted header in API Gateway.

There are a few approaches you can take to solve this:

  1. Use X-Amzn-Bedrock-AgentCore-Runtime-User-Id header instead:
  • Instead of passing the Authorization header directly, you can use the X-Amzn-Bedrock-AgentCore-Runtime-User-Id header to specify the user identity.
  • This header allows AgentCore to associate the request with a specific user context for OAuth token management.
  • You would extract the user ID from your authorization flow and pass it in this header.
  1. Configure custom header allowlisting:
  • AgentCore Runtime supports passing custom headers that start with "X-Amzn-Bedrock-AgentCore-Runtime-Custom-".
  • You could pass the token in a custom header and then handle it in your agent code.
  1. Configure JWT Inbound Authentication for AgentCore:
  • When creating your AgentCore gateway, configure it with a customJWTAuthorizer.
  • Set up the discovery URL, allowed clients, and other parameters to match your authentication requirements.
  • Example configuration:
authorizer_config={
"customJWTAuthorizer": {
"discoveryUrl": "https://cognito-idp.us-west-2.amazonaws.com/some-user-pool/.well-known/openid-configuration",
"allowedClients": ["clientId"]
}
}
  1. Use IAM authentication instead of OAuth:
  • If you're having trouble with OAuth, you could configure your AgentCore gateway to use IAM authentication.
  • This would require setting up the appropriate IAM roles and permissions.
  • API Gateway can be configured to sign requests with IAM credentials when proxying to AWS services.

For your specific use case of avoiding Lambda timeouts while waiting for agent responses, the direct HTTP integration approach is sensible, but you'll need to address the authentication challenge using one of the methods above.
Sources
How to route pass through an access token in AWS Bedrock AgentCore? | AWS re:Post
Regarding X-Amzn-Bedrock-AgentCore-Runtime-User-Id | AWS re:Post
Pass custom headers to Amazon Bedrock AgentCore Runtime - Amazon Bedrock AgentCore
Create an AgentCore gateway using the API - Amazon Bedrock AgentCore

answered a year ago

AWS
SUPPORT ENGINEER

reviewed 5 months ago

  • "API Gateway can be configured to sign requests with IAM credentials when proxying to AWS services."

    How would API Gateway get the IAM credentials to use? And what API Gateway v2 integration do you use for that?

0

How would API Gateway get the IAM credentials to use? And what API Gateway v2 integration do you use for that?

You need to create an IAM role using which you have to sign your request using sigV4 signatures[1]. To configure IAM Auth, you can refer the documentations[2].

References:

[1] https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html

[2] https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-access-control-iam.html

AWS
SUPPORT ENGINEER

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.