Skip to content

aws_ecs: Cannot change EC2 ASG Instance Types #36563

@AdamJHall

Description

@AdamJHall

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-ecsRelated to Amazon Elastic ContainerbugThis issue is a bug.effort/smallSmall work item – less than a day of effortgood first issueRelated to contributions. See CONTRIBUTING.mdp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions