【问题标题】:Upload folder using Terraforms file provisioner使用 Terraform 文件配置器上传文件夹
【发布时间】:2020-11-06 19:53:54
【问题描述】:

我正在使用 ECS 运行一些容器,我需要将一个包含一些配置的目录加载到其中一个容器中。使用 docker compose,我可以执行以下操作将目录从我的主机安装到容器中。

  go-1:
    image: link to my ecr repo here
    command: run -c /app/node/
    volumes:
      - ./go/nodes/node_0:/app/node

我想用 Terraform 做一些类似的事情,并将 nodes_0 目录从我的主机上传到我的容器中,我目前的方法是像这样使用文件配置器:

resource "aws_ecs_task_definition" "app" {
  family = var.app_task_family
  execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
  network_mode = "awsvpc"
  requires_compatibilities = [
    "FARGATE",
  ]
  cpu = var.fargate_cpu
  memory = var.fargate_memory
  container_definitions = data.template_file.app.rendered

  provisioner "file" {
    source      = "../go/nodes/node_0"
    destination = "/app/node"
  }
}

但是,当我运行 Terraform 应用时,这给了我以下错误:

Error: timeout - last error: dial tcp :22: connect: connection refused

我的 ECS 集群位于私有子网中,所以我认为这可能是问题所在。

有没有更好的方法将文件夹上传到我在 ECS 上运行的容器中?

【问题讨论】:

    标签: amazon-web-services terraform amazon-ecs


    【解决方案1】:

    按照你的建议

    我的 ECS 集群位于私有子网中,所以我认为这可能是 问题。

    是的,因为它位于私有子网中,您需要确保运行 terraform 代码的服务器或 PC 位于同一子网中,或者您可以进行 VPC 对等互连并确保两个子网之间都有路由。

    如果您使用 git 之类的版本控制系统,您可以从存储库执行 git clone 到实例,但您还需要确保实例启用了 nat 网关。

    然后,您需要像这样将存储库克隆到您的实例后挂载该文件夹。

    resource "aws_ecs_task_definition" "app" {
      family             = var.app_task_family
      execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
      network_mode       = "awsvpc"
      requires_compatibilities = [
        "FARGATE",
      ]
      cpu                   = var.fargate_cpu
      memory                = var.fargate_memory
      container_definitions = data.template_file.app.rendered
    
      volume {
        name      = "node-storage"
        host_path = "/home/user_name_replaced/go/nodes/node_0" #You declare the host path here
      }
    
    }
    

    你的 data.template_file.app.rendered

    [
      {
          "memory": 128,
          "portMappings": [
              {
                  "hostPort": 80,
                  "containerPort": 80,
                  "protocol": "tcp"
              }
          ],
          "essential": true,
          "mountPoints": [
              {
                  "containerPath": "/your_path_/",
                  "sourceVolume": "node-storage" #the host folder gets mounted to the container path
              }
          ],
          "name": "your_image",
          "image": "your_image"
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-16
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      • 2017-04-27
      • 2017-05-17
      相关资源
      最近更新 更多