In AWS Fargate, you can stop a service by setting the desired count to 0. This is useful when you want to temporarily stop a service, for example, during maintenance. However, you can’t set the desired count of a load balanced Fargate Service to 0 using CDK.

This won’t work:

web_service = aws_ecs_patterns.ApplicationLoadBalancedFargateService(
    self,
    ...
    desired_count=0,
)

You get an error like RuntimeError: You must specify a desiredCount greater than 0.

But there is a workaround. Instead of setting the property directly you can set it after the service objects has been created:

web_service = aws_ecs_patterns.ApplicationLoadBalancedFargateService(
    self,
    ...
    desired_count=1,
)
service = web_service.service.node.default_child
service.desired_count = 0