fix(cloudformation): keep an ECS service's desired count when an update leaves it out (#8279)

An AWS::ECS::Service update that left DesiredCount out set the service
back to 1. AWS omits it from the update ("For existing services, if a
desired count is not specified, it is omitted from the operation"), so a
count set through UpdateService now stays. A new service without it
still starts at 1, and a rollback puts the count back only when the
update sent one.
This commit is contained in:
Oleksandr Nikolaichuk authored and GitHub committed 2026-11-14 17:46:51 -04:00
1 parent ade406bac2
commit 4de81e8370
5 files changed
+94 -32

No files matched your search

@@ -291,7 +291,7 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
private void provisionService(StackResource r, JsonNode props, ProvisionContext ctx) {
String clusterRef = ctx.resolveOptional(props, "Cluster");
String taskDefinition = ctx.resolveOptional(props, "TaskDefinition");
int desiredCount = parseDesiredCount(ctx.resolveOptional(props, "DesiredCount"));
Integer desiredCount = parseDesiredCount(ctx.resolveOptional(props, "DesiredCount"));
LaunchType launchType = parseEnum(LaunchType.class, ctx.resolveOptional(props, "LaunchType"));
List<EcsLoadBalancer> loadBalancers =
parseLoadBalancers(props != null ? props.get("LoadBalancers") : null, ctx);
@@ -326,6 +326,7 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
update.setCluster(clusterRef);
update.setService(serviceName);
update.setTaskDefinition(taskDefinition);
// A missing count is left out of the update, as AWS omits it, so the stored count stays.
update.setDesiredCount(desiredCount);
update.setNetworkConfiguration(networkConfiguration);
update.setDeploymentConfiguration(deploymentConfiguration);
@@ -346,7 +347,7 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
create.setCluster(clusterRef);
create.setServiceName(serviceName);
create.setTaskDefinition(taskDefinition);
create.setDesiredCount(desiredCount);
create.setDesiredCount(desiredCount != null ? desiredCount : 1);
create.setLaunchType(launchType);
create.setLoadBalancers(loadBalancers);
create.setNetworkConfiguration(networkConfiguration);
@@ -389,13 +390,13 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
}
/**
* The update that puts {@code prior} back on what {@code update} changed: its task definition,
* desired count and network configuration, and each setting the update declared. A capacity
* provider strategy or service registries the service did not have are put back empty, so a
* registry the update added is deregistered; a setting the update left unchanged is left
* unchanged again, so the restore rolls no deployment for registries nobody touched. The
* deployment configuration is not here: updateService cannot put back none, so the caller
* restores it exactly through replaceServiceDeploymentConfiguration.
* The update that puts {@code prior} back on what {@code update} changed: its task definition
* and network configuration, and each setting the update declared, the desired count among
* them. A capacity provider strategy or service registries the service did not have are put
* back empty, so a registry the update added is deregistered; a setting the update left
* unchanged is left unchanged again, so the restore rolls no deployment for registries nobody
* touched. The deployment configuration is not here: updateService cannot put back none, so
* the caller restores it exactly through replaceServiceDeploymentConfiguration.
*/
private static UpdateServiceRequest restoreRequest(UpdateServiceRequest update, EcsServiceModel prior) {
UpdateServiceRequest restore = new UpdateServiceRequest();
@@ -405,7 +406,9 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
return restore;
}
restore.setTaskDefinition(prior.getTaskDefinition());
restore.setDesiredCount(prior.getDesiredCount());
if (update.getDesiredCount() != null) {
restore.setDesiredCount(prior.getDesiredCount());
}
restore.setNetworkConfiguration(prior.getNetworkConfiguration());
if (update.getCapacityProviderStrategy() != null) {
restore.setCapacityProviderStrategy(Objects.requireNonNullElse(prior.getCapacityProviderStrategy(), List.of()));
@@ -868,10 +871,10 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
}
}
/** DesiredCount defaults to 1 when absent; anything present has to be an integer. */
private static int parseDesiredCount(String value) {
/** An absent DesiredCount is null (1 at create, left out at update); anything present has to be an integer. */
private static Integer parseDesiredCount(String value) {
if (value == null || value.isBlank()) {
return 1;
return null;
}
try {
return Integer.parseInt(value.trim());