moto icon indicating copy to clipboard operation
moto copied to clipboard

Mock single resource, allow other real resource

Open kylegibson opened this issue 4 years ago • 1 comments

Moto version: 1.3.14

Consider the following example. My system is configured such that the boto3 S3 code will execute properly, on its own. However, if I execute the moto code to mock out SQS, the S3 (list_objects) will fail with ClientError: An error occurred (InvalidAccessKeyId) when calling the ListObjects operation: The AWS Access Key Id you provided does not exist in our records.:

import moto
mock_sqs = moto.mock_sqs()
mock_sqs.start()

import boto3
s3 = boto3.client('s3')
resp = s3.list_objects(Bucket='real-bucket-name', Prefix='', Delimiter="/")
print([x['Prefix'] for x in resp['CommonPrefixes']])

I've narrowed the cause of this error to the following: https://github.com/spulec/moto/blob/1299fef8b8217a39cd8446731024614b2191fe04/moto/core/models.py#L51-L56

Based on that code, it look as if I could call mock_sqs.env_variables_mocks.stop() immediately after mock_sqs.start().

I have tried this and it works, which is awesome. I wanted to share this with others in case they encounter the same issue, and also open up a discussion to learn if there's a better way to achieve the same result.

The system I am working in is extremely complex and it is not practical for me to convert all the AWS boto usage over to mocks, thus for now I do need to be able to mock out a single resource.

kylegibson avatar Jul 06 '20 19:07 kylegibson

Thanks for sharing the solution @kylegibson! That is very useful.

bblommers avatar Jul 10 '20 13:07 bblommers

Hi @kylegibson , it's been a while, but we've just created a new alpha-release where this behaviour can now be configured: moto==5.0.0alpha2

This release has a new mock_aws-decorator, with a config-parameter where you can specify which services should passthrough to AWS:

@mock_aws(
    config={"core": {
        "mock_credentials": False, 
        "passthrough": {
            "services": ["s3"]
    }}}
)

See the release announcement here: https://github.com/getmoto/moto/issues/7198

Are you able to upgrade and verify that this works for you?

bblommers avatar Jan 14 '24 21:01 bblommers

Moto V5 has now been released, so I'll close this.

Release: https://pypi.org/project/moto/5.0.0/

Documentation about this feature: http://docs.getmoto.org/en/5.0.0/docs/configuration/index.html

bblommers avatar Jan 27 '24 20:01 bblommers