aws / aws/aws-cdk

aws_ecs: Cannot change EC2 ASG Instance Types

Open
#36,563 8 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-ecs bug effort/small good first issue p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

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

```typescript
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

Open the contributing guide

Research direction

Start with the Ec2Service and its underlying CfnService configuration, especially capacityProviderStrategies, deploymentController, and forceNewDeployment. Reproduce the t3.medium-to-t3.large change with the CodeDeploy deployment strategy and compare the synthesized service configuration with the reported ECS error. Done means the ASG instance type can change and a new deployment is forced without the stack rolling back.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.