Skip to content

[Bug]: Cannot add tags to aws_appautoscaling_target resource #31839

@imanAzi

Description

@imanAzi

Terraform Core Version

v1.4.6

AWS Provider Version

v4.67.0

Affected Resource(s)

aws_appautoscaling_target

Expected Behavior

Terraform should create these resources and manage the tags automatically by seeing the default_tags configuration on the provider

Actual Behavior

When creating the these resources for the first time, we receive the following error:

aws_appautoscaling_target.service_scaling: Creating...
╷
│ Error: creating Application AutoScaling Target (service/content-available-service-nonprod-ecs-cluster/content-available-service-nonprod-ecs-service): ValidationException: The scalable target that you tried to tag already exists. To update tags on an existing scalable target, use the TagResource API.
│ 
│   with aws_appautoscaling_target.service_scaling,
│   on autoscaling.tf line 1, in resource "aws_appautoscaling_target" "service_scaling":
│    1: resource "aws_appautoscaling_target" "service_scaling" {
│ 
╵

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

terraform {
  backend "s3" {
    bucket         = "xxxx"
    key            = "content-available-service"
    dynamodb_table = "terraform-state"
    region         = "xxxx"
  }
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

#Provider configuration
provider "aws" {
  region = var.aws_region
  default_tags {
    tags = {
      application = var.project
      stage       = var.env
      squad       = var.squad
      terraform   = "true"
      repository  = var.repository_name
    }
  }
}

resource "aws_appautoscaling_target" "service_scaling" {
  min_capacity       = var.min_container_count
  max_capacity       = var.max_container_count
  resource_id        = "service/${aws_ecs_cluster.ecs_cluster.name}/${aws_ecs_service.ecs_service.name}"
  role_arn           = aws_iam_role.autoscaling_role.arn
  scalable_dimension = "ecs:service:DesiredCount"
  service_namespace  = "ecs"

  depends_on = [aws_ecs_service.ecs_service, aws_iam_role.autoscaling_role]
}

resource "aws_appautoscaling_policy" "service_scale_out" {
  name               = "${var.project}-${var.env}-scale-out-policy"
  policy_type        = "StepScaling"
  resource_id        = aws_appautoscaling_target.service_scaling.resource_id
  scalable_dimension = aws_appautoscaling_target.service_scaling.scalable_dimension
  service_namespace  = aws_appautoscaling_target.service_scaling.service_namespace

  step_scaling_policy_configuration {
    adjustment_type         = "ChangeInCapacity"
    cooldown                = 60
    metric_aggregation_type = "Average"

    step_adjustment {
      scaling_adjustment          = 1
      metric_interval_lower_bound = 0
    }
  }

  depends_on = [aws_appautoscaling_target.service_scaling]
}

Steps to Reproduce

Terraform apply

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAddresses a defect in current functionality.service/appautoscalingIssues and PRs that pertain to the appautoscaling service.tagsPertains to resource tagging.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions