Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot passthrough bedrock-runtime #7682

Closed
acro-s-yoshioka opened this issue May 10, 2024 · 2 comments
Closed

Cannot passthrough bedrock-runtime #7682

acro-s-yoshioka opened this issue May 10, 2024 · 2 comments
Labels

Comments

@acro-s-yoshioka
Copy link

problem

I cannot passthrough requests to bedrock-runtime.

Since bedrock-runtime is not yet supported by moto, I want to patch it myself.
I cannot get rid of mock_aws all together because there is a section that uses dynamodb in the same function.

When I try using passthrough option so that moto does not intercept the request, the passthrough does not work.

I see that, in moto.backend_index.backend_url_patterns, regex string for bedrock-runtime is missing.

Is there any work arounds I can take?
Or should I just downgrade to v4.

code to reproduce the issue

import boto3
import botocore
from unittest.mock import patch
from pytest import fixture

from moto import mock_aws

@fixture(autouse=True)
def mock_envs(mocker: MockerFixture):
    mocker.patch.dict(os.environ, {
        "AWS_ACCESS_KEY_ID": "dummy_do_not_change",
        "AWS_SECRET_ACCESS_KEY": "dummy_do_not_change",
        "AWS_SECURITY_TOKEN": "dummy_do_not_change",
        "AWS_SESSION_TOKEN": "dummy_do_not_change",
        "AWS_DEFAULT_REGION": "us-west-2",
    }, clear=True)


@fixture(autouse=True)
def aws(mock_envs):
    with mock_aws(config={
        "core": {
            "passthrough": {
                "services": ["bedrock-runtime"],
            },
        },
    }):
        yield


# Original botocore _make_api_call function
orig = botocore.client.BaseClient._make_api_call

# Mocked botocore _make_api_call function
def mock_make_api_call(self, operation_name, kwarg):
    # For example for the Access Analyzer service
    # As you can see the operation_name has the list_analyzers snake_case form but
    # we are using the ListAnalyzers form.
    # Rationale -> https://github.com/boto/botocore/blob/develop/botocore/client.py#L810:L816
    if operation_name == 'InvokeModel':
        return {}  # I have actual response mock here.
    # If we don't want to patch the API call
    return orig(self, operation_name, kwarg)


def test_ok():
    client = boto3.client("bedrock-runtime")

    with patch('botocore.client.BaseClient._make_api_call', new=mock_make_api_call):
        response = client.invoke_model(...)
        # I get `botocore.exceptions.ClientError: An error occurred (404) when calling the InvokeModel operation: Not yet implemented`
@acro-s-yoshioka
Copy link
Author

sorry, I think i found the solution myself in the documents.

I can add bedrock-runtime url in this passthrought.urls .

@mock_aws(config={
    "core": {
        "passthrough": {
            "urls": ["s3.amazonaws.com/bucket*"],
        },
})

@bblommers
Copy link
Collaborator

Hi @acro-s-yoshioka! The services-argument only works for services that Moto knows about. If it's an unknown service, like bedrock-agent, Moto doesn't know what URL to intercept, so you would indeed have to specify it explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants