创建应用程序 AutoScaling 目标时出错:ValidationException:不支持的服务命名空间、资源类型或可扩展维度

Aar*_*ver 4 module amazon-ecs autoscaling terraform aws-fargate

我正在尝试为某些 Fargate 服务启用 ECS 自动缩放,但遇到标题中的错误:

error creating Application AutoScaling Target: ValidationException: Unsupported service namespace, resource type or scalable dimension

错误发生在第 4 行:

resource "aws_appautoscaling_target" "autoscaling" {
  max_capacity = var.max_capacity
  min_capacity = 1
  resource_id  = var.resource_id
  //  <snip... a bunch of other vars not relevant to question>
Run Code Online (Sandbox Code Playgroud)

我这样调用自定义自动缩放模块:

module "myservice_autoscaling" {
  source = "../autoscaling"
  resource_id = aws_ecs_service.myservice_worker.id
  //  <snip... a bunch of other vars not relevant to question>
Run Code Online (Sandbox Code Playgroud)

我的服务是一个普通的 ECS 服务块,开头为:

resource "aws_ecs_service" "myservice_worker" {

在网上浏览后,我想也许我应该“手动”构建“service/clusterName/serviceName”,如下所示:

resource_id = "service/${var.cluster_name}/${aws_ecs_service.myservice_worker.name}"

但这会导致不同的错误:

The argument "cluster_name" is required, but no definition was found.

cluster_name在我的调用模块(即调用我的新自动缩放模块的 myservice ECS 内容)中创建了variables.tf。我cluster_nameoutputs.tf集群模块中设置了 ECS 集群。我一定还缺少一些链接。

有任何想法吗?谢谢!

编辑:这是让它为我工作的解决方案

  1. 是的,您确实需要以"service/yourClusterName/yourServiceName". 我的最终看起来像:"service/${var.cluster_name}/${aws_ecs_service.myservice_worker.name}"
  2. 您需要确保您有权访问集群名称和服务名称变量。就我而言,尽管我在 ECS 服务中定义了变量variables.tf,并将其添加到集群模块中outputs.tf,但我无法从根模块传递到服务模块。这修复了:
module "myservice" {
  source = "./modules/myservice"
  cluster_name = module.cluster.cluster_name // the line I added
Run Code Online (Sandbox Code Playgroud)

(前面的代码片段位于main.tf根模块的(高于服务模块的级别)

Mar*_*k B 5

您构建字符串的方向是正确的"service/${var.cluster_name}/${aws_ecs_service.myservice_worker.name}"。看起来您根本没有正确引用集群名称。

我在集群模块的outputs.tf中有cluster_name

因此,您需要引用该模块输出,而不是引用不存在的变量:

"service/${module.my_cluster_module.cluster_name}/${aws_ecs_service.myservice_worker.name}"
Run Code Online (Sandbox Code Playgroud)

将“my_cluster_module”更改为您为创建 ECS 集群的模块指定的任何名称。