如何在 EFS 上创建文件夹?

How do I create a folder on EFS?

我使用 Terraform 设置了一个 ECS 集群。一切正常,但我有几个问题。

1. 据我了解,EFS 卷不需要挂载到 ECS 实例。 AWS 允许我们将 EFS 卷文件夹直接挂载到容器中。我说得对吗?

resource "aws_ecs_task_definition" "Task" {
  family                = var.ServiceName
  container_definitions = file("service.json")
  tags = {
    Name    = data.terraform_remote_state.Cluster.outputs.TagName
    Project = data.terraform_remote_state.Cluster.outputs.TagName
  }
  volume {
    name = "service-storage"
    efs_volume_configuration {
      file_system_id = data.terraform_remote_state.Cluster.outputs.EfsVolumeId
      root_directory = "/"
    }
  }
}

root_directory这里是EFS卷里面到文件夹的路径,会挂载到容器中

service.json

[
  {
    "name": "nginx13",
    "image": "nginx",
    "memory": 256,
    "mountPoints": [
      {
        "containerPath": "/usr/share/nginx/html",
        "sourceVolume": "service-storage"
      }
    ],
    "portMappings": [
      {
        "containerPort": 80
      }
    ]
  }
]

containerPath 这里是容器内部到挂载点 root_directory 文件夹的路径。所以没有与ECS实例挂载点或路径相关的参数。

2. 在创建新任务之前,我需要在 EFS 卷上创建一个文件夹,以便稍后将容器安装到其中。现在,我只能使用 EFS 卷的根文件夹,因为它是空的。因此,我正在寻找一种方法来管理使用 terraform 模板在 EFS 卷上创建和删除文件夹。这是问题的第一部分,第二部分是将文件放在该文件夹中。 最佳实践是什么?我应该使用某种部署解决方案,例如 Jenkins,还是仅使用 Terraform 就可以完成? EFS 文件夹权限如何?他们需要改变吗?

使用 EFS 访问点: https://docs.aws.amazon.com/efs/latest/ug/efs-access-points.html

访问点通过在 EFS 中创建目录来工作。然后您可以设置该目录的访问权限。无论如何,这可能对您正在做的事情更好,因为它为您提供了访问控制。

If a root directory path for an access point doesn't exist on the file system, Amazon EFS automatically creates that root directory with configurable ownership and permissions.

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/efs_access_point

如果这不合适:

我会推荐使用 lambda。

https://aws.amazon.com/blogs/compute/using-amazon-efs-for-aws-lambda-in-your-serverless-applications/

您可以使用任何您熟悉的可以挂载 EFS 的语言编写 lambda。然后让它创建一个目录。然后您可以使用空资源 local-exec.

调用此 lambda