moto
moto copied to clipboard
Question: Is there any way to save moto server state?
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.
Hi @dtsvirkun, not at the moment. Will mark this as an enhancement, as it would be a useful feature!
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.
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.
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?
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 🙁
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.
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) )
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.