How to mount Amazon EFS as data volume in ECS task?
Userdata on launch configuration:
#!/bin/bash
echo ECS_CLUSTER=prodcluster >> /etc/ecs/ecs.config
sudo yum install -y nfs-utils
sudo mkdir /home/ec2-user/web_file_uploads
sudo chmod 777 /home/ec2-user/web_file_uploads
sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-abcdef.efs.ap-southeast-2.amazonaws.com:/ /home/ec2-user/web_file_uploads
Task definition:
{
"networkMode": "bridge",
"containerDefinitions": [
{
"portMappings": [
...
],
"essential": true,
"mountPoints": [
{
"containerPath": "/src/temp_uploads/",
"sourceVolume": "web_file_uploads",
"readOnly": null
}
],
"name": "webapp",
"readonlyRootFilesystem": null,
"image": "911911911.dkr.ecr.ap-southeast-2.amazonaws.com/webapp:release6",
"memoryReservation": 150
}
],
"volumes": [
{
"host": {
"sourcePath": "/home/ec2-user/web_file_uploads"
},
"name": "web_file_uploads"
}
],
"family": "webapp-task"
}
Note: I have added required ports to security group. src/temp_uploads folder is required to have within all other container instances launched with this task definition.
Problem:
If I mount it manually using sudo mount ... folder1
, put some files there and mount the same to sudo mount ... folder2
folder 2 contains the changes which shows it mounts correctly.
The problem is the volume mapped through the task definition doesn't map efs
. Instead it puts/use content actually inside /home/ec2-user/web_file_uploads
.
How to make this volume actually mapped to efs
Modify the lunch configuration to restart docker service right after mounting EFS. Then only ECS will use the mounted EFS as volume. Otherwise it will use the original directory (mount will be ignored).
#!/bin/bash
echo ECS_CLUSTER=prodcluster >> /etc/ecs/ecs.config
sudo yum install -y nfs-utils
sudo stop ecs
sudo mkdir /home/ec2-user/web_file_uploads
sudo chmod 777 /home/ec2-user/web_file_uploads
sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-abcdef.efs.ap-southeast-2.amazonaws.com:/ /home/ec2-user/web_file_uploads
sudo service docker restart
sudo start ecs
Note: ECS service will stop after restarting docker service as ECS Agent runs inside docker. You need to start ECS Service afterwards.