Terraform:ECS服务 - InvalidParameterException

时间:2018-01-18 16:23:32

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

我正在尝试使用terraform配置ECS群集,在创建ecs服务之前,一切似乎都运行良好:

resource "aws_ecs_service" "ecs-service" {
  name            = "ecs-service"
  iam_role        = "${aws_iam_role.ecs-service-role.name}"
  cluster         = "${aws_ecs_cluster.ecs-cluster.id}"
  task_definition = "${aws_ecs_task_definition.my_cluster.family}"
  desired_count   = 1

  load_balancer {
    target_group_arn  = "${aws_alb_target_group.ecs-target-group.arn}"
    container_port    = 80
    container_name    = "my_cluster"
  }
}

,IAM角色是:

resource "aws_iam_role" "ecs-service-role" {
  name = "ecs-service-role"

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

resource "aws_iam_role_policy_attachment" "ecs-service-role-attachment" {
    role       = "${aws_iam_role.ecs-service-role.name}"
    policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole"
}

我收到以下错误消息:

  
      
  • aws_ecs_service.ecs-service:发生1个错误:

  •   
  • aws_ecs_service.ecs-service:InvalidParameterException:无法承担角色并验证指定的targetGroupArn。请验证   传递的ECS服务角色具有适当的权限。

  •   

1 个答案:

答案 0 :(得分:2)

在assume_role_policy中,您可以将“Principal”行更改为如下所述:您正在使用ec2.amazonaws.com

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