将标签添加到ECS服务-InvalidParameterException

时间:2018-12-04 02:52:23

标签: amazon-web-services terraform

我有一个可以正常运行的Fargate应用程序,并且已在AWS中运行。我回去向所有资源添加标签,以更好地监控微服务架构中的成本。将标签添加到我的aws_ecs_service资源后,运行terraform apply时出现以下异常:

aws_ecs_service.main: error tagging ECS Cluster (arn:aws:ecs:*region*:*account_number*:service/*service_name*): InvalidParameterException: Long arn format must be used for tagging operations

经过研究,我发现11月15日,AWS引入了一种新的ARN和ID格式:https://aws.amazon.com/ecs/faqs/#Transition_to_new_ARN_and_ID_format

我知道我需要将设置应用于已分配给我的服务的IAM角色,但是我不知道该如何做。以下是帐户设置的AWS文档的链接:https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_Setting.html

下面是ecs服务资源的片段以及任务定义:

resource "aws_ecs_task_definition" "app" {
  family                   = "${var.app_name}"
  network_mode             = "awsvpc"
  requires_compatibilities = ["FARGATE"]
  cpu                      = "${var.app_cpu}"
  memory                   = "${var.app_memory}"
  execution_role_arn       = "${var.execution_role_arn}"
  task_role_arn            = "${var.task_role_arn}"

  tags {
    Name        = "${var.app_name}-ecs-task-definition-${var.environment}"
    Service     = "${var.app_name}"
    Environment = "${var.environment}"
    Cost_Center = "${var.tag_cost_center}"
    Cost_Code   = "${var.tag_cost_code}"
  }

  container_definitions = <<DEFINITION
[
  {
    "cpu": ${var.app_cpu},
    "image": "${var.app_image}",
    "memory": ${var.app_memory},
    "name": "${var.app_name}",
    "networkMode": "awsvpc",
    "logConfiguration": {
      "logDriver": "awslogs",
      "options": {
        "awslogs-group": "stash-${var.app_name}",
        "awslogs-region": "${var.aws_region}",
        "awslogs-stream-prefix": "${var.app_name}"
      }
    },
    "portMappings": [
      {
        "containerPort": ${var.app_port},
        "hostPort": ${var.app_port}
      }
    ]
  }
]
DEFINITION
}

resource "aws_ecs_service" "main" {
  name            = "${var.app_name}-service"
  cluster         = "${var.cluster_id}"
  task_definition = "${aws_ecs_task_definition.app.arn}"
  desired_count   = "1"
  launch_type     = "FARGATE"

  network_configuration {
    security_groups = ["${var.security_groups}"]
    subnets         = ["${var.subnets}"]
  }

  load_balancer {
    target_group_arn = "${var.target_group_arn}"
    container_name   = "${var.app_name}"
    container_port   = "${var.app_port}"
  }

  lifecycle {
    ignore_changes = ["desired_count"]
  }

  tags {
    Name        = "${var.app_name}-ecs-service-${var.environment}"
    Service     = "${var.app_name}"
    Environment = "${var.environment}"
    Cost_Center = "${var.tag_cost_center}"
    Cost_Code   = "${var.tag_cost_code}"
  }
}

这里是我的安全资源的一面:

resource "aws_iam_role" "task_role" {
  name = "${var.app_name}-task-${var.environment}"

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

我正在使用terraform版本0.11.8。

2 个答案:

答案 0 :(得分:4)

按照online documentation for opting in到新的ARN格式,您需要具有root帐户访问权才能选择加入特定的IAM角色。

上述链接中详细说明的步骤应为

  • 为您的集群创建一个IAM角色(您已完成此操作)
  • 以root用户身份登录
  • 前往opt in page并选择要加入的IAM角色
  • 希望获利!

请注意,您也可以选择加入整个帐户,直到2020年1月,此更改将成为强制性要求。

答案 1 :(得分:1)

自从您提到terraform以来,让我添加一下(我也使用terraform并遇到了非常类似的问题)。您可以将AWS CLI与ECS子命令put-account-setting一起使用,以设置三个LongArnFormat

aws ecs put-account-setting --name containerInstanceLongArnFormat --value enabled --region _yourRegion_
aws ecs put-account-setting --name serviceLongArnFormat           --value enabled --region _yourRegion_
aws ecs put-account-setting --name taskLongArnFormat              --value enabled --region _yourRegion_

参考:AWS Doc