Skip to content

Commit 3d0bf5c

Browse files
authored
docs(ecs): added note for work around from LaunchType to CapacityProvi… (#36088)
### Issue # (if applicable) Closes #35742 ### Reason for this change ### Description of changes - Corrected the doc to use `FargateService` when using Managed Instances CapacityProvider. - Added a work around for migrating existing service with LaunchType to use Managed Instances CapacityProvider without replacement. ### Describe any new or updated permissions being added ### Description of how you validated changes ### Checklist - [X] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 084b736 commit 3d0bf5c

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

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

Lines changed: 44 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,41 @@ 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+
declare const miCapacityProvider: ecs.ManagedInstancesCapacityProvider;
1789+
1790+
const service = new ecs.FargateService(this, 'Service', {
1791+
cluster,
1792+
taskDefinition,
1793+
capacityProviderStrategies: [
1794+
{
1795+
capacityProvider: miCapacityProvider.capacityProviderName,
1796+
weight: 1,
1797+
},
1798+
],
1799+
});
1800+
1801+
// Escape hatch: Force launchType at the CloudFormation level to prevent service replacement
1802+
const cfnService = service.node.defaultChild as ecs.CfnService;
1803+
cfnService.launchType = 'FARGATE'; // or 'FARGATE_SPOT' depending on your capacity provider
17641804
```
17651805

17661806
### Cluster Default Provider Strategy

0 commit comments

Comments
 (0)