ECS Fargate Node.js child_process NoCredentialProviders:链中没有有效的提供者。不推荐使用

时间:2018-11-01 19:23:30

标签: node.js child-process amazon-ecs aws-fargate

我正在尝试使用ECS自动化基础架构和应用程序部署。部署逻辑相当复杂,并且随着时间的流逝可能会变得更加复杂,因此我选择使用Node.js来处理业务流程。部署中的某些步骤要求我在容器中执行另一个二进制文件,特别是terraform。问题是由Node.js创建的子进程似乎没有与Node.js进程相同的权限。

一些详细信息

  • Fargate正在启动ECS容器。
  • “任务定义”具有“任务角色”,该角色赋予它执行所有需要的事情的权限,包括terraform执行的所有操作。
  • 我已经通过创建具有相同权限的用户来测试权限是否足够,并且能够在本地成功执行部署。
  • 我知道已将权限授予Node.js进程,因为它能够在私有存储桶上执行诸如s3:GetObject之类的操作,并被任务角色授予访问权限
  • terraform尝试调用AWS api时,产生以下错误“ NoCredentialProviders:链中没有有效的提供程序。已弃用。”
  • 我尝试使用child_process.spawn和child_process.exec执行terraform命令。两者都给出相同的错误。

解决了一段时间后,我非常有信心这个问题与任务角色没有移交给子进程有关。可以这样做吗?如果可以,怎么办?

如果有帮助,以下是经过修改的Terraform脚本,以ECS设置进行说明。主要区别是任务角色在我正在使用的任务角色中具有更多权限。

resource "aws_ecs_task_definition" "deploy" {
  family = "deploy-task"
  container_definitions = <<EOF
[
  {
    "name": "deploy",
    "image": "<ecr image uri>:latest",
    "essential": true,
    "memoryReservation": 128,
    "logConfiguration": {
      "logDriver": "awslogs",
      "options": {
        "awslogs-group": "/ecs/deploy-task",
        "awslogs-region": "us-east-1",
        "awslogs-stream-prefix": "ecs"
      }
    }
  }
]
EOF
  task_role_arn = "${aws_iam_role.task.arn}"
  execution_role_arn = "${aws_iam_role.execution.arn}"
  network_mode = "awsvpc"
  cpu = 256
  memory = 512
  requires_compatibilities = ["FARGATE"]
}

resource "aws_ecs_cluster" "deploy" {
  name = "deploy-cluster"
}

resource "aws_iam_role" "task" {
  name = "ecs-deploy-task"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "ecs-tasks.amazonaws.com"
      },
      "Effect": "Allow"
    }
  ]
}
EOF
}

resource "aws_iam_role_policy" "s3-policy" {
  name = "s3-policy"
  role = "${aws_iam_role.task.id}"

  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:GetObject",
      "Resource": "<private s3 bucket>/*"
    }
  ]
}
EOF
}

resource "aws_iam_role" "execution" {
  name = "ecs-deploy-execution"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "ecs-tasks.amazonaws.com"
      },
      "Effect": "Allow"
    }
  ]
}
EOF
}

resource "aws_iam_role_policy_attachment" "execution" {
  role = "${aws_iam_role.execution.id}"
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}

0 个答案:

没有答案