0

I am trying to host a MongoDB in AWS ECS Service with permanent storage using Terraform. I have gone through the following links:

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?

Moravas
  • 79
  • 6
  • 1
    You probably don't need to format it, just create the file system if one doesn't already exist. Regardless, you just need to update your launch template script to have a bit of if/else logic in there to perform some checks on the mounted EBS volume before doing anything like formatting it. – Mark B Jun 18 '23 at 12:47
  • 1
    Whenever I see someone trying to create a database system on top of ECS I always encourage you to look at the managed services for databases on AWS before building your own. In this case I would highly suggest evaluating AWS DocumentDB, and MongoDB Atlas before building your own MongoDB system on top of ECS. For example your current setup using an EBS volume means your database can only operate in a single Availability Zone, so an AZ outage would completely take down your database. Those are the type of things the managed DB services handle for you automatically. – Mark B Jun 18 '23 at 12:50
  • Thank you, both of your comments are useful. Regarding to the second: business knows better... :) – Moravas Jun 19 '23 at 04:00

0 Answers0