Search code examples
moduleterraformamazon-ecsautoscalingaws-fargate

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


I'm trying to enable ECS autoscaling for some Fargate services and run into the error in the title:

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

The error happens on line 4 here:

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>

I call the custom autoscaling module like so:

module "myservice_autoscaling" {
  source = "../autoscaling"
  resource_id = aws_ecs_service.myservice_worker.id
  //  <snip... a bunch of other vars not relevant to question>

My service is a normal ECS service block starting with:

resource "aws_ecs_service" "myservice_worker" {

After poking around online, I thought maybe I should construct the "service/clusterName/serviceName" sort of "manually", like so:

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

But that leads to a different error:

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

I created cluster_name in my calling module (i.e. myservice ECS stuff that calls my new autoscaling module) variables.tf. And I have cluster_name in the outputs.tf of our cluster module where we're setting up the ECS cluster. I must be missing some linking still.

Any ideas? Thanks!

Edit: here's the solution that got it working for me

  1. Yes, you do need to construct the resource_id in the form of "service/yourClusterName/yourServiceName". Mine ended up looking like: "service/${var.cluster_name}/${aws_ecs_service.myservice_worker.name}"
  2. You need to make sure you have access to the cluster name and service name variables. In my case, though I had the variable defined in my ECS service's variables.tf, and I added it my cluster module's outputs.tf, I was failing to pass down from the root module to the service module. This fixed that:
module "myservice" {
  source = "./modules/myservice"
  cluster_name = module.cluster.cluster_name // the line I added

(the preceding snippet goes in the main.tf of your root module (a level above your service module)


Solution

  • You are on the right track constructing the "service/${var.cluster_name}/${aws_ecs_service.myservice_worker.name}" string. It looks like you simply aren't referencing the cluster name correctly.

    And I have cluster_name in the outputs.tf of our cluster module

    So you need to reference that module output, instead of referencing a not-existent variable:

    "service/${module.my_cluster_module.cluster_name}/${aws_ecs_service.myservice_worker.name}"
    

    Change "my_cluster_module" to whatever name you gave the module that is creating your ECS cluster.