配置ECS Fargate服务应用自动扩缩容时遇资源ID错误求助
ECS Fargate服务配置自动扩缩容报错及解决方法
问题场景
为ECS Fargate服务配置Application AutoScaling时,首次触发如下错误:
error creating Application AutoScaling Target: ValidationException: Unsupported service namespace, resource type or scalable dimension
错误出现在aws_appautoscaling_target资源定义的第4行:
resource "aws_appautoscaling_target" "autoscaling" { max_capacity = var.max_capacity min_capacity = 1 resource_id = var.resource_id // <snip... 其他无关变量> }
调用自定义扩缩容模块的代码:
module "myservice_autoscaling" { source = "../autoscaling" resource_id = aws_ecs_service.myservice_worker.id // <snip... 其他无关变量> }
对应的ECS服务定义片段:
resource "aws_ecs_service" "myservice_worker" {
二次尝试与新错误
查阅资料后,尝试手动构造service/clusterName/serviceName格式的resource_id,修改代码为:
resource_id = "service/${var.cluster_name}/${aws_ecs_service.myservice_worker.name}"
但触发新错误:
The argument "cluster_name" is required, but no definition was found.
已在ECS服务模块的variables.tf中定义cluster_name,也在ECS集群模块的outputs.tf中输出该变量,但变量传递链路存在缺失。
最终解决方法
- ECS服务的AutoScaling Target必须使用
"service/集群名称/服务名称"格式的resource_id,最终写法为:resource_id = "service/${var.cluster_name}/${aws_ecs_service.myservice_worker.name}" - 补充变量传递:在根模块的
main.tf中,向ECS服务模块传递集群名称变量,新增代码如下:module "myservice" { source = "./modules/myservice" cluster_name = module.cluster.cluster_name // 新增行 }
内容的提问来源于stack exchange,提问作者Aaron
相关产品推荐
相关产品推荐

