Skip to content

Commit ea8da46

Browse files
committed
fix(ecs): added a workaround to switch from launchType to capacityProviderStrategy without replacement"
1 parent fa332bc commit ea8da46

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

packages/aws-cdk-lib/aws-ecs/README.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,9 +1661,9 @@ new ecs.Ec2Service(this, 'EC2Service', {
16611661

16621662
### Managed Instances Capacity Providers
16631663

1664-
Managed Instances Capacity Providers allow you to use AWS-managed EC2 instances for your ECS tasks while providing more control over instance selection than standard Fargate. AWS handles the instance lifecycle, patching, and maintenance while you can specify detailed instance requirements.
1664+
Managed Instances Capacity Providers allow you to use AWS-managed EC2 instances for your ECS tasks while providing more control over instance selection than standard Fargate. AWS handles the instance lifecycle, patching, and maintenance while you can specify detailed instance requirements. You can define detailed instance requirements to control which types of instances are used for your workloads.
16651665

1666-
To create a Managed Instances Capacity Provider, you need to specify the required EC2 instance profile, and networking configuration. You can also define detailed instance requirements to control which types of instances are used for your workloads.
1666+
See [ECS documentation for Managed Instances Capacity Provider](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/managed-instances-capacity-providers-concept.html) for more documentation.
16671667

16681668
```ts
16691669
declare const vpc: ec2.Vpc;
@@ -1693,14 +1693,19 @@ miCapacityProvider.connections.allowFrom(ec2.Peer.ipv4(vpc.vpcCidrBlock), ec2.Po
16931693
// Add the capacity provider to the cluster
16941694
cluster.addManagedInstancesCapacityProvider(miCapacityProvider);
16951695

1696-
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef');
1696+
const taskDefinition = new ecs.TaskDefinition(this, 'TaskDef', {
1697+
memoryMiB: '512',
1698+
cpu: '256',
1699+
networkMode: ecs.NetworkMode.AWS_VPC,
1700+
compatibility: ecs.Compatibility.MANAGED_INSTANCES,
1701+
});
16971702

16981703
taskDefinition.addContainer('web', {
16991704
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
17001705
memoryReservationMiB: 256,
17011706
});
17021707

1703-
new ecs.Ec2Service(this, 'EC2Service', {
1708+
new ecs.FargateService(this, 'FargateService', {
17041709
cluster,
17051710
taskDefinition,
17061711
minHealthyPercent: 100,
@@ -1761,6 +1766,40 @@ const miCapacityProvider = new ecs.ManagedInstancesCapacityProvider(this, 'MICap
17611766
onDemandMaxPricePercentageOverLowestPrice: 10,
17621767
},
17631768
});
1769+
1770+
```
1771+
#### Note: Service Replacement When Migrating from LaunchType to CapacityProviderStrategy
1772+
1773+
**Understanding the Limitation**
1774+
1775+
The ECS [CreateService API](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_CreateService.html#ECS-CreateService-request-launchType) does not allow specifying both `launchType` and `capacityProviderStrategies` simultaneously. When you specify `capacityProviderStrategies`, the CDK uses those capacity providers instead of a launch type. This is a limitation of the ECS API and CloudFormation, not a CDK bug.
1776+
1777+
**Impact on Updates**
1778+
1779+
Because `launchType` is immutable during updates, switching from `launchType` to `capacityProviderStrategies` requires CloudFormation to replace the service. This means your existing service will be deleted and recreated with the new configuration. This behavior is expected and reflects the underlying API constraints.
1780+
1781+
**Workaround**
1782+
1783+
While we work on a long-term solution, you can use the following [escape hatch](https://docs.aws.amazon.com/cdk/v2/guide/cfn-layer.html) to preserve your service during the migration:
1784+
1785+
```ts
1786+
declare const cluster: ecs.Cluster;
1787+
declare const taskDefinition: ecs.TaskDefinition;
1788+
1789+
const service = new ecs.FargateService(this, 'Service', {
1790+
cluster,
1791+
taskDefinition,
1792+
capacityProviderStrategies: [
1793+
{
1794+
capacityProvider: miCapacityProvider.capacityProviderName,
1795+
weight: 1,
1796+
},
1797+
],
1798+
});
1799+
1800+
// Escape hatch: Force launchType at the CloudFormation level to prevent service replacement
1801+
const cfnService = service.node.defaultChild as ecs.CfnService;
1802+
cfnService.launchType = 'FARGATE'; // or 'FARGATE_SPOT' depending on your capacity provider
17641803
```
17651804

17661805
### Cluster Default Provider Strategy

0 commit comments

Comments
 (0)