Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform | AWS ECS service | desired count value changes for every tf apply

When I do terraform apply every time the desired count is changing from 1 -> 2. Please suggest. But in variables.tf, I declared as desired_count=2.

~ resource "aws_ecs_service" "ecs_service" {
         cluster                            = "arn:aws:ecs::cluster/"
         deployment_maximum_percent         = 200
         deployment_minimum_healthy_percent = 100
       ~ desired_count                      = 1 -> 2
         enable_ecs_managed_tags            = false
         force_new_deployment               = true
         health_check_grace_period_seconds  = 0
         iam_role                           = "aws-service-role"
         id                                 = "arn:aws:ecs:-ecs-service"
         launch_type                        = "FARGATE"
         name                               = "ecs-service"
         platform_version                   = "LATEST"
         propagate_tags                     = "NONE"
         scheduling_strategy                = "REPLICA"
         tags                               = {}
         task_definition                    = "arn:aws:ecs:"
         deployment_controller {
             type = "ECS"
         }
         load_balancer {
             container_name   = "abcd"
             container_port   = 80
             target_group_arn = "arn.*"
         }
         network_configuration {
             assign_public_ip = false
             security_groups  = []
             subnets          = []
         }
     }

Here is the main.tf:- Note:- I am using modules to invoke main.tf.

############ ECS Service ##############
resource "aws_ecs_service" "ecs_service" {
  name                 = "${var.name}-ecs-service"
  cluster              = var.cluster_id
  task_definition      = var.task_definition_arn
  desired_count        = var.desired_count
  iam_role             = var.ecs_iam_role
  force_new_deployment = var.force_new_deployment
  launch_type          = "FARGATE"
  deployment_controller {
    type = "ECS"
  }
  deployment_maximum_percent         = "200"
  deployment_minimum_healthy_percent = "100"
  load_balancer {
    target_group_arn = var.lb_tg_arn
    container_name   = var.name
    container_port   = var.port
  }
  network_configuration {
    security_groups  = var.security_group_id
    subnets          = var.subnets
    assign_public_ip = var.assign_public_ip
  }
  depends_on = [var.aws_alb]
}

Variables are assigned on the requirement of my project by making default=null

like image 324
asur Avatar asked Nov 30 '25 02:11

asur


1 Answers

Not sure what is really causing the change, but if you only want to mute the change you can go with ignoring the change as described in the terraform docs

resource "aws_ecs_service" "example" {
  # ... other configurations ...

  # Example: Create service with 2 instances to start
  desired_count = 2

  # Optional: Allow external changes without Terraform plan difference
  lifecycle {
    ignore_changes = [desired_count]
  }
}
like image 56
Falk Tandetzky Avatar answered Dec 01 '25 15:12

Falk Tandetzky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!