cn-terraform / terraform-aws-ecs-fargate-service

AWS ECS Fargate Service Terraform Module
https://registry.terraform.io/modules/cn-terraform/ecs-fargate-service
Apache License 2.0
78 stars 47 forks source link

enable_autoscaling: Issue with CloudWatch Metric alarm_actions Handling Null Values #102

Open adrian-slowik-zaven opened 4 months ago

adrian-slowik-zaven commented 4 months ago

Provider and Module Versions AWS Provider Version: ~> 5.0 Module Version: 2.0.43

Description I am experiencing an issue with the terraform-aws-ecs-fargate-service module, specifically with the handling of alarm_actions in CloudWatch metric alarms. The module is calling another module (cn-terraform/ecs-service-autoscaling/aws) which is responsible for setting up auto-scaling.

Every time I execute terraform apply, I see the following changes in the plan, even though no actual changes are made to the infrastructure:

# module.ecs_service_web-api.module.ecs-fargate-service.module.ecs-autoscaling[0].aws_cloudwatch_metric_alarm.api-cpu_high will be updated in-place
~ resource "aws_cloudwatch_metric_alarm" "api-cpu_high" {
    ~ alarm_actions                         = [
        + null,
          # (1 unchanged element hidden)
      ]
      id                                    = "api-cpu-high"
      tags                                  = {
          "Environment" = "dev"
          "Project"     = "example-project"
      }
      # (21 unchanged attributes hidden)
  }

Module Call Here is how the ecs-autoscaling module is being called:

module "ecs-autoscaling" {
  count = var.enable_autoscaling ? 1 : 0

  source  = "cn-terraform/ecs-service-autoscaling/aws"
  version = "1.0.9"

  name_prefix               = var.name_prefix
  ecs_cluster_name          = var.ecs_cluster_name
  ecs_service_name          = aws_ecs_service.service.name
  max_cpu_threshold         = var.max_cpu_threshold
  min_cpu_threshold         = var.min_cpu_threshold
  max_cpu_evaluation_period = var.max_cpu_evaluation_period
  min_cpu_evaluation_period = var.min_cpu_evaluation_period
  max_cpu_period            = var.max_cpu_period
  min_cpu_period            = var.min_cpu_period
  scale_target_max_capacity = var.scale_target_max_capacity
  scale_target_min_capacity = var.scale_target_min_capacity
  tags                      = var.tags
}

Problematic Section in the ecs-autoscaling Module

The issue seems to be originating from the following part of the ecs-autoscaling module:

resource "aws_cloudwatch_metric_alarm" "cpu_low" {
  alarm_name          = "${var.name_prefix}-cpu-low"
  comparison_operator = "LessThanOrEqualToThreshold"
  evaluation_periods  = var.min_cpu_evaluation_period
  metric_name         = "CPUUtilization"
  namespace           = "AWS/ECS"
  period              = var.min_cpu_period
  statistic           = "Average"
  threshold           = var.min_cpu_threshold
  dimensions = {
    ClusterName = var.ecs_cluster_name
    ServiceName = var.ecs_service_name
  }
  alarm_actions = [
    aws_appautoscaling_policy.scale_down_policy.arn,
    var.sns_topic_arn != "" ? var.sns_topic_arn : ""
  ]
  tags = var.tags
}

Issue The alarm_actions attribute is supposed to include the ARN of the scaling policy and optionally an SNS topic ARN if it is provided. However, it ends up including null instead of an empty string, leading to unnecessary in-place updates in the Terraform plan.

Question Why does the conditional expression for alarm_actions end with a null value instead of an empty string? How can this be resolved to avoid unnecessary updates?

Any help or guidance on this issue would be greatly appreciated.

stevie- commented 1 month ago

probably solved with https://github.com/cn-terraform/terraform-aws-ecs-service-autoscaling/issues/37