I am trying to host a MongoDB in AWS ECS Service with permanent storage using Terraform. I have gone through the following links:
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html
- https://devopscube.com/mount-ebs-volume-ec2-instance/
- How do I use EBS volume with ECS container
Some key facts:
- I want to host multiple MongoDB services, not correlated to each other, with no cluster/shardening.
- I can create as many EC2 instances as I need and register them into my ECS Cluster, and start the service from the particular task definition by setting a custom attribute in each EC2 instance:
ECS_INSTANCE_ATTRIBUTES={"target":"<my_unique_str>"}
- And I refer to this string as a placement constraint during service creation.
My problem is that if I have an EBS volume (via Terraform I have 1 EBS attached 1 EC2 instance), I need to format and mount it into EC2, and after that, I can refer to it as a bind mount volume in my service configuration. However, formatting the volume on each EC2 instance start will delete the data, which is obviously no-way to lose our customers' stuff. The EBS volumes must survive in any circumstances.
Can anyone help me clarify what I did wrong or what I am missing?
Alternatively, as I use AWS ECS EC2 launch type, is there some way to configure the service itself to mount EBS automatically so that I do not have to take care of which EC2 instance my service is running, but set during service deployment that the given EBS volume is auto-attached to the hosting EC2 instance?