Search code examples
amazon-vpcamazon-ecsaws-cloudformationamazon-efs

Specify correct AZ mount point for EFS volume on ECS Cluster - CloudFormation


Looking at this example of mounting an EFS volume for persisting docker volumes in ECS, I'm unsure how to provide the correct mount point for the availability zone that the instance is in. I have two availability zones in my stack and need the correct mount point to insert in this section of the cfn-init:

01_mount:
    command: !Join [ "", [ "mount -t nfs4 -o nfsvers=4.1 ", !ImportValue '!Ref FileSystem', ".efs.", !Ref 'AWS::Region', ".amazonaws.com:/ /", !Ref MountPoint ] ]
02_fstab:
    command: !Join [ "", [ "echo \"", !ImportValue '!Ref FileSystem', ".efs.", !Ref 'AWS::Region', ".amazonaws.com:/ /", !Ref MountPoint, " nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 0 0\" >> /etc/fstab" ] ]
03_permissions:
    command: !Sub "chown -R ec2-user:ec2-user /${MountPoint}"

Solution

  • It is no longer necessary to use the availability-zone-specific mount target when mounting an EFS filesystem, if you are using the DNS settings in your VPC and have other necessary prerequisites in place in the VPC configuration.

    File system DNS name – Using the file system's DNS name is your simplest mounting option. The file system DNS name will automatically resolve to the mount target’s IP address in the Availability Zone of the connecting Amazon EC2 instance. You can get this DNS name from the console, or if you have the file system ID, you can construct it using the following convention:

    file-system-id.efs.aws-region.amazonaws.com

    (emphasis added)

    http://docs.aws.amazon.com/efs/latest/ug/mounting-fs-mount-cmd-dns-name.html

    This feature was introduced in December, 2016, several months after the service was released from preview. Formerly, the hostname style shown above had to be prepended with the availability zone you wanted. That option is still supported, but this option effectively eliminates this awkward configuration requirement, both in docker and on ordinary instances with fstab mounts.

    See the referenced page for the VPC configuration elements that must be in place for this solution to work in your VPC.