【发布时间】:2019-05-05 09:54:36
【问题描述】:
我在 AWS 中启动并运行了一个功能齐全的 Fargate 应用程序。我回去为我的所有资源添加标签,以更好地监控微服务架构中的成本。将标签添加到我的 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服务资源的sn-p以及任务定义:
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。
【问题讨论】:
标签: amazon-web-services terraform