无法扮演角色并验证指定的目标群



我想使用Terraform ecs_service创建和部署群集,但无法做到。我的terraform apply S总是围绕IAM角色失败,我不明确地理解。具体来说,错误消息是:

InvalidParameterSexception:无法扮演角色并验证指定的目标群。请验证ECS服务角色通过具有适当的许可。

我发现:

  1. 当我在ECS_Service中指定iam_role时,ECS抱怨我需要使用服务链接的角色。
  2. 当我在ECS_Service中评论iam_role时,ECS抱怨假设角色无法验证目标群。

我的Terraform跨越了许多文件。我拉下了下面的相关部分。尽管我已经看到了一些类似的问题,但没有一个提供了可行的解决方案,可以解决上述困境。

## ALB
resource "aws_alb" "frankly_internal_alb" {
    name = "frankly-internal-alb"
    internal = false
    security_groups = ["${aws_security_group.frankly_internal_alb_sg.id}"]
    subnets = ["${aws_subnet.frankly_public_subnet_a.id}", "${aws_subnet.frankly_public_subnet_b.id}"]
}
resource "aws_alb_listener" "frankly_alb_listener" {
    load_balancer_arn = "${aws_alb.frankly_internal_alb.arn}"
    port = "8080"
    protocol = "HTTP"
    default_action {
        target_group_arn = "${aws_alb_target_group.frankly_internal_target_group.arn}"
        type = "forward"
    }
}
## Target Group
resource "aws_alb_target_group" "frankly_internal_target_group" {
    name = "internal-target-group"
    port = 8080
    protocol = "HTTP"
    vpc_id = "${aws_vpc.frankly_vpc.id}"
    health_check {
        healthy_threshold = 5
        unhealthy_threshold = 2
        timeout = 5
    }
}
## IAM
resource "aws_iam_role" "frankly_ec2_role" {
  name               = "franklyec2role"
  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" "frankly_ecs_role" {
  name = "frankly_ecs_role"
  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "ecs.amazonaws.com"
      },
      "Effect": "Allow",
      "Sid": ""
    }
  ]
}
EOF
}
# aggresively add permissions...
resource "aws_iam_policy" "frankly_ecs_policy" {
  name        = "frankly_ecs_policy"
  description = "A test policy"
  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "ec2:*",
        "ecs:*",
        "ecr:*",
        "autoscaling:*",
        "elasticloadbalancing:*",
        "application-autoscaling:*",
        "logs:*",
        "tag:*",
        "resource-groups:*"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
EOF
}
resource "aws_iam_role_policy_attachment" "frankly_ecs_attach" {
  role       = "${aws_iam_role.frankly_ecs_role.name}"
  policy_arn = "${aws_iam_policy.frankly_ecs_policy.arn}"
}
## ECS
resource "aws_ecs_cluster" "frankly_ec2" {
    name = "frankly_ec2_cluster"
}
resource "aws_ecs_task_definition" "frankly_ecs_task" {
  family                = "service"
  container_definitions = "${file("terraform/task-definitions/search.json")}"
  volume {
    name      = "service-storage"
    docker_volume_configuration {
      scope         = "shared"
      autoprovision = true
    }
  }
  placement_constraints {
    type       = "memberOf"
    expression = "attribute:ecs.availability-zone in [us-east-1]"
  }
}
resource "aws_ecs_service" "frankly_ecs_service" {
  name            = "frankly_ecs_service"
  cluster         = "${aws_ecs_cluster.frankly_ec2.id}"
  task_definition = "${aws_ecs_task_definition.frankly_ecs_task.arn}"
  desired_count   = 2
  iam_role        = "${aws_iam_role.frankly_ecs_role.arn}"
  depends_on      = ["aws_iam_role.frankly_ecs_role", "aws_alb.frankly_internal_alb", "aws_alb_target_group.frankly_internal_target_group"]
  # network_configuration = {
  #   subnets = ["${aws_subnet.frankly_private_subnet_a.id}", "${aws_subnet.frankly_private_subnet_b}"]
  #   security_groups = ["${aws_security_group.frankly_internal_alb_sg}", "${aws_security_group.frankly_service_sg}"]
  #   # assign_public_ip = true
  # }
  ordered_placement_strategy {
    type  = "binpack"
    field = "cpu"
  }
  load_balancer {
    target_group_arn = "${aws_alb_target_group.frankly_internal_target_group.arn}"
    container_name   = "search-svc"
    container_port   = 8080
  }
  placement_constraints {
    type       = "memberOf"
    expression = "attribute:ecs.availability-zone in [us-east-1]"
  }
}

我看到了一条相同的错误消息,我在做其他错误:

我已经指定了LoadBalancer的ARN和 loadBalancer的target_group arn。

对我来说,问题是我忘了将正确的政策附加到服务角色。附加此AWS管理的政策帮助:arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole

对我来说,我正在使用上一个命令的输出。但是输出是空的,因此目标组ARN在创建服务调用中为空。

我的角色错误。

resource "aws_ecs_service" "ECSService" {
  name    = "stage-quotation"
  cluster = aws_ecs_cluster.ECSCluster2.id
  load_balancer {
    target_group_arn = aws_lb_target_group.ElasticLoadBalancingV2TargetGroup2.arn
    container_name   = "stage-quotation"
    container_port   = 8000
  }
  desired_count                      = 1
  task_definition                    = aws_ecs_task_definition.ECSTaskDefinition.arn
  deployment_maximum_percent         = 200
  deployment_minimum_healthy_percent = 100
  iam_role                           = aws_iam_service_linked_role.IAMServiceLinkedRole4.arn #
  ordered_placement_strategy {
    type  = "spread"
    field = "instanceId"
  }
  health_check_grace_period_seconds = 0
  scheduling_strategy               = "REPLICA"
}
resource "aws_iam_service_linked_role" "IAMServiceLinkedRole2" {
  aws_service_name = "ecs.application-autoscaling.amazonaws.com"
}
resource "aws_iam_service_linked_role" "IAMServiceLinkedRole4" {
  aws_service_name = "ecs.amazonaws.com"
  description      = "Role to enable Amazon ECS to manage your cluster."
}

由于命名差不当,我不小心将自己的角色用于应用自动镜。我们需要使用的正确角色以上定义为IAMServiceLinkedRole4

以防止错误:

Error: creating ECS Service (*****): InvalidParameterException: Unable to assume role and validate the specified targetGroupArn. Please verify that the ECS service role being passed has the proper permissions.

我身边正在使用以下配置:

  1. 角色可信关系:向受信任的政策添加陈述
{
            "Sid": "ECSpermission",
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "ecs.amazonaws.com",
                    "ecs-tasks.amazonaws.com"
                ]
            },
            "Action": "sts:AssumeRole"
}
  1. 角色权限:

添加AWS管理政策:

  • Amazonec2ContainerRegistryfullaccess
  • Amazonec2ContainerServiceForec2role
  • 添加自定义内联策略:(我知道权限非常广泛(
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "autoscaling:*",
                "elasticloadbalancing:*",
                "application-autoscaling:*",
                "resource-groups:*"
            ],
            "Effect": "Allow",
            "Resource": "*"
        }
    ]
}
  1. 在资源中使用参数iam_role声明您的自定义角色。
resource "aws_ecs_service" "team_deployment" {
  name            = local.ecs_task
  cluster         = data.terraform_remote_state.common_resources.outputs.ecs_cluster.id
  task_definition = aws_ecs_task_definition.team_deployment.arn
  launch_type     = "EC2"
  iam_role        = "arn:aws:iam::****:role/my_custom_role"
  desired_count = 3
  enable_ecs_managed_tags = true
  force_new_deployment    = true
  scheduling_strategy     = "REPLICA"
  wait_for_steady_state   = false

  load_balancer {
    target_group_arn = data.terraform_remote_state.common_resources.outputs.target_group_api.arn
    container_name   = var.ecr_image_tag
    container_port   = var.ecr_image_port
  }
}

当然要小心参数target_group_arn值。必须是目标群体。然后现在工作正常!

Releasing state lock. This may take a few moments...
Apply complete! Resources: 1 added, 2 changed, 0 destroyed.

通过破坏我的堆栈并重新删除来解决。

相关内容

  • 没有找到相关文章

最新更新