user1050817
user1050817

Reputation: 965

How can I add a persistent volume to my API running in ECS?

I'm trying to persist the data stored in my static folder because the users can upload images and store in that folder, generally when I work with docker is just use a volume mounting the /static folder and pointing to some host folder, but seems that I'm not doing this correctly.

enter image description here

Inside my ECS task definition I created the volume /static with name static...I run my service using this task definition, then I entered the corresponding docker

docker exec -it <id for my service> bash

I navigate across the folders but I can't found any /static folder in any path

what am I ignoring? what is the simplest way to add a folder where I can persist data in my ECS

thanks guy and sorry for this super noob question

Upvotes: 1

Views: 2933

Answers (2)

Gurpreet Singh
Gurpreet Singh

Reputation: 301

By the looks of it, you are using the EC2 launch type. ECS tasks using EFS will automatically mount the file systems specified by the customer in the task definition and make them available to the containers in the task across all availability zones in the region. This enables persistent, shared storage to be defined and used at the task and container level in ECS.

{
    "containerDefinitions": [
        {
            "memory": 128,
            "portMappings": [
                {
                    "hostPort": 80,
                    "containerPort": 80,
                    "protocol": "tcp"
                }
            ],
            "essential": true,
            "mountPoints": [
                {
                    "containerPath": "/usr/share/nginx/html",
                    "sourceVolume": "efs-html"
                }
            ],
            "name": "nginx",
            "image": "nginx"
        }
    ],
    "volumes": [
        {
            "name": "efs-html",
            "efsVolumeConfiguration": {
                "fileSystemId": "fs-1324abcd",
                "transitEncryption": "ENABLED"
            }
        }
    ],
    "family": "efs-tutorial"
}

You can connect the EFS to multiple Tasks as well. [This documentation talks about how you can implement it][1]

Plus, EFS works with Fargate as well which take cares about the resource management as well.

Using bind mounts the data will be persistence on the container instance which is running the task. So using EFS does provides an advantage here. [1]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/tutorial-efs-volumes.html

Upvotes: 0

Fermin
Fermin

Reputation: 36081

It looks like you've only added the volume to the TaskDefinition and not referenced it in any ContainerDefinitions. If you hover over the information icon beside "Name" in your screenshot the info message will read:

The name of the volume to use in the container definition Mount points as Source volume.

So, you need to create to reference the above mount in the "mountPoints" of your ContainerDefinition, this will add the static folder to your container.

E.g. The following is an JSON task-definition with everything except mounts removed:

{
  "containerDefinitions": [
    {
      "mountPoints": [
        {
          "readOnly": null,
          "containerPath": "/static",
          "sourceVolume": "static"
        }
      ]
    }
  ],
  "volumes": [
    {
      "fsxWindowsFileServerVolumeConfiguration": null,
      "efsVolumeConfiguration": null,
      "name": "static",
      "host": {
        "sourcePath": "/static"
      },
      "dockerVolumeConfiguration": null
    }
  ]
}

Upvotes: 1

Related Questions