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

Qn / Feature Request - can I just make a moto Session without worrying about mock lifetimes? #8573

Open
jwhitaker-gridcog opened this issue Feb 7, 2025 · 1 comment
Labels

Comments

@jwhitaker-gridcog
Copy link

jwhitaker-gridcog commented Feb 7, 2025

The docs of moto recommend usage following a pattern of "start mocking", "use boto stuff", "stop mocking". e.g.

with mock_aws():
  sess = boto3.Session()
  sess.client('s3').do_stuff()... # uses moto

However, is it possible to use moto without interfering with global state like this? e.g. can I just ask for a moto session like:

moto_sess: boto3.Session = moto.get_session()
moto_sess.client('s3').do_stuff()... # uses moto
boto3.Session().client('s3').do_stuff()... # moto not involved

? This feels much neater to me - there's no global state being mutated, and if my code is already structured to accept a Session via DI or something then it's a natural fit.

I've tried using moto.core.base_backend.get_session but it doesn't seem to work outside of mock_aws() being active.

@bblommers
Copy link
Collaborator

Hi @jwhitaker-gridcog - Moto hooks into the list of global boto3 events, so once our mocking hook is set, every instantiated client uses those hooks. As far as I can see, there is no (easy) way to only mock specific boto3 sessions.

It's hard to tell without knowing your exact use case - but does it make sense to configure specific requests/services to passthrough instead? See the docs here: https://docs.getmoto.org/en/latest/docs/configuration/index.html

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