aws/aws-cdk

aws_ecs: Cannot change EC2 ASG Instance Types

Open

#36563 opened on Jan 5, 2026

View on GitHub
 (7 comments) (0 reactions) (0 assignees)TypeScript (10,710 stars) (3,530 forks)batch import
@aws-cdk/aws-ecsbugeffort/smallgood first issuep2

Description

Describe the bug

When providing capacityProviderStrategies to a Ec2Service on a cluster using an AsgCapacityProvider with a Code deploy deployment strategy we are unable to deploy a change to the ASG instance type.

Expected Behavior

Be able to update the ASG instance size and force a new deployment.

Current Behavior

Receive the below error and stack rolls back:

Invalid request provided: When switching from launch type to capacity provider strategy on an existing service, or making a change to a capacity provider strategy on a service that is already using one, you must force a new deployment

Reproduction Steps

cluster = new Cluster(this, "Cluster", { vpc });

const autoScalingGroup = this.cluster.addCapacity("NextJSCapacity", {
  instanceType: new InstanceType("t3.medium"),
  machineImage: EcsOptimizedImage.amazonLinux2023(AmiHardwareType.STANDARD),
  minCapacity: 2,
  maxCapacity:10,
  azCapacityDistributionStrategy:
    CapacityDistributionStrategy.BALANCED_BEST_EFFORT,
  capacityRebalance: true,
  defaultInstanceWarmup: cdk.Duration.seconds(15),
});

const capacityProvider = new AsgCapacityProvider(
  this, 
  'AsgCapacityProvider', 
  { 
    autoScalingGroup: autoScalingGroup,
    enableManagedTerminationProtection: true,
    enableManagedDraining: true
  }
);

cluster.addAsgCapacityProvider(capacityProvider);
cluster.addDefaultCapacityProviderStrategy([{capacityProvider: capacityProvider.capacityProviderName, weight: 1}])

// configure task def, etc

this.service = new Ec2Service(this, "Service", {
  cluster: this.cluster,
  taskDefinition,
  desiredCount: 2,
  placementStrategies: [
    PlacementStrategy.packedByCpu()
  ],
  capacityProviderStrategies: [{
    capacityProvider: capacityProvider.capacityProviderName,
    weight: 1
  }],

  // Using Canary deployments via Code deploy w/ codepipeline
  deploymentController: {
    type: DeploymentControllerType.CODE_DEPLOY,
  },
});

// Attempt to force a new deployment
const cfnService = this.service.node.defaultChild as CfnService;
cfnService.forceNewDeployment = {
  forceNewDeploymentNonce: props.forceNewDeploymentNonce ?? 'DefaultValue',
  enableForceNewDeployment: true
}
  1. Deploy above stack with t3.medium
  2. Attempt to redeploy with t3.large
  3. Receive above error

Possible Solution

I've attempted adding the forceNewDeployment to the underlying CfnService with no luck, I also attempted removing the capacityProviderStrategies from the service and just defining the strategy as the default for the cluster but then the service deploys with an EC2 launch type and does not use the clusters default strategy.

Additional Information/Context

No response

AWS CDK Library version (aws-cdk-lib)

.233.0

AWS CDK CLI version

2.1024.0

Node.js Version

24.7.0

OS

Mac OS

Language

TypeScript

Language Version

5.6.3

Other information

No response

Contributor guide