I want to mount an Amazon Elastic File System (Amazon EFS) on an Amazon Elastic Container Service (Amazon ECS) container or task. The container or task uses an Amazon Elastic Compute Cloud (Amazon EC2) launch type.
Resolution
Create a task definition that provides the file system ID in the volume task definition parameters. This allows the EFS file system to automatically mount to the tasks that you specify in your task definition.
Network requirements
Confirm that you have the following configurations:
- The EFS file system and ECS cluster for the Amazon EC2 launch type must be in the same virtual private cloud (VPC).
- The security groups of the EFS file system must allow inbound connections on port 2049 from the ECS container instance and the ECS task.
- The security groups of the ECS instance or tasks must allow outbound connections on port 2049 to the EFS file system's security group.
Create a task definition
Complete the following steps:
- Open the Amazon ECS console, and then choose Task Definitions, Create new Task Definition.
- For Launch type, choose EC2, and then choose Next step.
- For Configure task and container definitions, enter a name for your task definition.
- In the Volume section, choose Add volume.
- Enter a name for the volume, and then choose EFS from the Volume types dropdown list.
- For the File system ID, select the ID of the file system to use with the ECS tasks.
- (Optional) Specify the Root directory, Encryption in transit, and EFS IAM authorization. If you don't specify anything, then the default root directory "/" is used.
- Choose Add.
- When you're creating the container, under Container definitions, choose Add container to use the new volume. Then, in the Mount points subsection, under Storage and Logging, select the volume.
- For Container path, choose the directory path that's in the container for your application, and then choose Add.
- Complete the required fields in the task definition wizard, and then choose Create.
In the following example, the task definition creates a data volume that's named efs-ec2-test. The nginx container mounts the host data volume at the /usr/share/nginx/html path:
{ "containerDefinitions": [
{
"memory": 128,
"portMappings": [
{
"hostPort": 80,
"containerPort": 80,
"protocol": "tcp"
}
],
"essential": true,
"mountPoints": [
{
"containerPath": "/usr/share/nginx/html",
"sourceVolume": "efs-ec2-test"
}
],
"name": "nginx",
"image": "nginx"
}
],
"volumes": [
{
"name": "efs-ec2-test",
"efsVolumeConfiguration": {
"fileSystemId": "fs-1324abcd",
"transitEncryption": "ENABLED"
}
}
],
"family": "efs-test"
}
Note: Replace the fileSystemid, containerPath, and other task definition parameters with your values.
In the preceding example, you can create a sample index.html file in the file system's root directory that looks similar to the following:
<html> <body>
<h1>You are using an Amazon EFS file system for persistent container storage.</h1>
</body>
</html>
Run an Amazon ECS task
Complete the following steps:
- To run your ECS task, use the task definition.
- To verify that the EFS file system mounts to the ECS container, use the instance's IP address to access the website.