getmoto / moto

A library that allows you to easily mock out tests based on AWS infrastructure.

Home Page:http://docs.getmoto.org/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot passthrough bedrock-runtime

acro-s-yoshioka opened this issue · comments

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`

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*"],
        },
})

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.