Tutorial: Mount a file system from outside its VPC using EFS Endpoints - Amazon Elastic File System
Services or capabilities described in AWS documentation might vary by Region. To see the differences applicable to the AWS European Sovereign Cloud Region, see the AWS European Sovereign Cloud User Guide.

Tutorial: Mount a file system from outside its VPC using EFS Endpoints

In this tutorial, you mount an EFS file system on an EC2 Linux instance that runs in a different VPC from the file system. The two VPCs aren't connected, and the instance's VPC has no mount target for the file system. The instance connects through EFS Endpoints on port 443, over TLS, and authenticates with the IAM role that is attached to the instance. For an overview of EFS Endpoints, see Configuring your network.

When to use this tutorial

Follow this tutorial when your client meets both of the following conditions:

  • It runs on AWS compute in the same AWS Region as the file system.

  • It can't reach a mount target for the file system. For example, it runs in a VPC, or in an AWS account, that has no network connection to the file system's VPC.

Use a mount target instead in the following cases:

This tutorial grants access to a role in the file system's AWS account. To grant access to a role in another AWS account, you also need a file system policy. For an example, see Policy examples for network access.

Prerequisites

To complete this tutorial, you must have the following:

  • An EFS file system that doesn't use the Max I/O performance mode, and its file system ID (for example, fs-0123456789abcdef0). For more information, see Performance modes.

  • An EC2 Linux instance in the same AWS Region as the file system, in a VPC that can't reach a mount target for the file system.

  • Outbound access from the instance to TCP port 443. For example, the instance has a public IP address in a subnet with a route to an internet gateway, or its subnet has a route to a NAT gateway. The instance's security group and network ACL must allow this traffic. The default security group allows all outbound traffic. NAT gateway data processing charges apply to traffic through a NAT gateway.

  • amazon-efs-utils version 4.0.0 or later installed on the instance. For more information, see Installing the Amazon EFS client.

  • Permissions to create an IAM role and attach it to the instance, and the AWS Command Line Interface (AWS CLI) configured with those permissions.

Step 1: Grant the instance access through EFS Endpoints

In this step, you create an IAM role with the AmazonElasticFileSystemEndpointClientReadWriteAccess managed policy and attach it to the instance. This policy grants elasticfilesystem:CreateMountSession and the read and write client actions in both the elasticfilesystem and elasticfilesystem-endpoint IAM names. For more information, see Permissions for EFS Endpoints.

If the instance already has an IAM role, attach the policy to that role (substep 3) and skip the remaining substeps.

To grant the instance access
  1. Save the following trust policy as ec2-trust.json. It lets EC2 assume the role.

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
  2. Create the role.

    $ aws iam create-role --role-name efs-endpoints-client \ --assume-role-policy-document file://ec2-trust.json
  3. Attach the managed policy to the role.

    $ aws iam attach-role-policy --role-name efs-endpoints-client \ --policy-arn arn:aws:iam::aws:policy/AmazonElasticFileSystemEndpointClientReadWriteAccess
  4. Create an instance profile and add the role to it.

    $ aws iam create-instance-profile --instance-profile-name efs-endpoints-client $ aws iam add-role-to-instance-profile --instance-profile-name efs-endpoints-client \ --role-name efs-endpoints-client
  5. Attach the instance profile to the instance. Replace instance-id with the ID of your instance.

    $ aws ec2 associate-iam-instance-profile --instance-id instance-id \ --iam-instance-profile Name=efs-endpoints-client
Note

If your file system has a file system policy, the policy must also allow the access, and must not deny it. The default file system policy allows access only through mount targets, but it doesn't deny access that an identity policy in the file system's AWS account grants. For more information, see Permissions for EFS Endpoints.

Step 2: Mount the file system

Connect to the instance, and then mount the file system with the EFS mount helper and the tls and iam mount options.

To mount the file system
  1. Create a mount directory.

    $ sudo mkdir -p /mnt/efs
  2. Mount the file system. Replace file-system-id with the ID of your file system.

    $ sudo mount -t efs -o tls,iam file-system-id /mnt/efs/

The mount helper first looks for a mount target that the instance can reach. When it finds none, it connects through EFS Endpoints. To make the mount helper always use EFS Endpoints, even when a mount target is reachable, set connection_mode = endpoint in the [mount] section of /etc/amazon/efs/efs-utils.conf.

Step 3: Verify the mount

To verify the mount
  1. Confirm that the mount helper used EFS Endpoints. The mount log has a line that starts with Using endpoint mode to mount filesystem.

    $ sudo grep "Using endpoint mode" /var/log/amazon/efs/mount.log
  2. Write a file to the file system and list it.

    $ sudo touch /mnt/efs/test-file.txt $ ls -l /mnt/efs

To mount the file system automatically when the instance starts, add the following line to /etc/fstab. For more information, see Enabling automatic mounting on existing EC2 Linux instances.

file-system-id:/ /mnt/efs efs _netdev,tls,iam 0 0

Step 4: Clean up resources

When you no longer need the mount, unmount the file system. If you created the role for this tutorial, detach it from the instance and delete it.

To clean up
  1. Unmount the file system, and remove its line from /etc/fstab if you added one.

    $ sudo umount /mnt/efs
  2. Find the instance profile association, and then remove it. Replace association-id with the AssociationId from the first command.

    $ aws ec2 describe-iam-instance-profile-associations \ --filters Name=instance-id,Values=instance-id $ aws ec2 disassociate-iam-instance-profile --association-id association-id
  3. Delete the instance profile and the role.

    $ aws iam remove-role-from-instance-profile --instance-profile-name efs-endpoints-client \ --role-name efs-endpoints-client $ aws iam delete-instance-profile --instance-profile-name efs-endpoints-client $ aws iam detach-role-policy --role-name efs-endpoints-client \ --policy-arn arn:aws:iam::aws:policy/AmazonElasticFileSystemEndpointClientReadWriteAccess $ aws iam delete-role --role-name efs-endpoints-client

Troubleshooting

Common problems when you mount through EFS Endpoints
Problem What to check
The mount fails with Mounting through an endpoint requires the "tls" and "iam" mount options Add -o tls,iam to the mount command.
The mount fails with Mounting via endpoint from outside of the AWS region of your file system is unsupported due to performance reasons. Run the client on AWS compute in the same AWS Region as the file system, or use a mount target.
The mount fails with an access denied error Check that the instance's role has elasticfilesystem:CreateMountSession and each client action in both the elasticfilesystem and elasticfilesystem-endpoint IAM names, and that no identity policy, file system policy, or service control policy denies them.
The mount stops responding or times out Check that the instance has outbound access to TCP port 443: its route table, security group, and network ACL.

For more troubleshooting information, see Troubleshooting mount issues.