moto icon indicating copy to clipboard operation
moto copied to clipboard

Question: Is there any way to save moto server state?

Open dtsvirkun opened this issue 4 years ago • 7 comments

Hello, is there any way to save current moto server state? For example, I created a lots of different resources (s3 buckets, etc) and everything is gone after stadalone (or docker container) server restart.

dtsvirkun avatar Apr 09 '20 19:04 dtsvirkun

Hi @dtsvirkun, not at the moment. Will mark this as an enhancement, as it would be a useful feature!

bblommers avatar Apr 14 '20 06:04 bblommers

In case anyone wants to work on this, all of the resources are referenced from a relatively centralized place: https://github.com/spulec/moto/blob/master/moto/core/models.py#L510-L523

Given that, I don't think it would be too much work to try to pickle or json.dump them somewhere.

spulec avatar Apr 26 '20 20:04 spulec

This is a very interesting idea. If anyone else is looking at this issue, then I think the lines of code to look at are now these:

https://github.com/spulec/moto/blob/master/moto/core/models.py#L555-L576

Looks like saving model_data is the key.

lllama avatar Feb 16 '21 16:02 lllama

I have a use case, where an application exists exclusively as a consumer. of some resources - namely Secrets Manager and SQS.

In our E2E test suite - which uses docker-compose - I need a way to initialize moto with those resources (a secret in Secrets Manager, and a queue in SQS).

Loading an initial state would be a very nice way to go about it. But with this feature not being implemented (yet :smile:), is there some other neat way to achieve this?

aksel avatar Oct 29 '21 08:10 aksel

Hi @aksel - if you're using IaC (e.g. Terraform Ansible, etc.) you could point them to Moto, and initialize the infrastructure as part of the E2E tests? There are no neater ways to achieve this yet unfortunately 🙁

bblommers avatar Oct 29 '21 09:10 bblommers

Hi @spulec , @lllama I am interested in some experiments for import/export the state. But the file you are referencing here :

  • https://github.com/spulec/moto/blob/master/moto/core/models.py#L510-L523
  • https://github.com/spulec/moto/blob/master/moto/core/models.py#L555-L576

has changed and it has only 405 lines now. So the point in code suggested by you to look at is not there anymore (lines between 510 and 576) Could you suggest the new place to get/inject the imported state?

Thanks a lot.

mnesarco avatar Jun 08 '22 15:06 mnesarco

Please correct me if I am wrong. Maybe I can do just this to export all the state:

from moto.core.base_backend import model_data
import json
save_state_somewhere( json.dumps(model_data) )

mnesarco avatar Jun 08 '22 15:06 mnesarco

Moto 4.0.6 (just released) has the option to record requests, and replay them on another system/at another time, to ensure that the initial set of resources is the same. It is also possible to seed Moto now, which means that the identifiers are deterministic given a specific seed.

See the documentation for this here: http://docs.getmoto.org/en/latest/docs/configuration/recorder/index.html

I'll close this, but do let us know if there's any questions or feedback on this feature.

bblommers avatar Oct 02 '22 14:10 bblommers