cn-terraform / terraform-aws-ecs-service-autoscaling

AWS ECS Service Autoscaling
https://registry.terraform.io/modules/cn-terraform/ecs-service-autoscaling/aws/
Apache License 2.0
15 stars 18 forks source link

bug: TF tries to apply null on each run. #37

Closed stevie- closed 1 month ago

stevie- commented 3 months ago

if sns_topic is not set, the default is empty string "". This leads to null as an element added to the list which is a NOOP change. On each run, TF detects a drift and tries to add it.

we are using https://github.com/cn-terraform/terraform-aws-ecs-fargate-service version "2.0.43"

which uses this module version "1.0.9"

tf plan

# module.ecs_service.module.ecs-autoscaling[0].aws_cloudwatch_metric_alarm.cpu_high will be updated in-place
  ~ resource "aws_cloudwatch_metric_alarm" "cpu_high" {
      ~ alarm_actions                         = [
          + null,
            # (1 unchanged element hidden)
        ]

better implemention would be this

  alarm_actions = compact([
    aws_appautoscaling_policy.scale_up_policy.arn,
    var.sns_topic_arn != "" ? var.sns_topic_arn : ""
  ])

or

  alarm_actions = concat(
  [
    aws_appautoscaling_policy.scale_up_policy.arn,
  ],
  var.sns_topic_arn != "" ? [var.sns_topic_arn] : [],
  )

PS: there is no way, to set sns_topic_arn within the service module. You may want to add the variable.