Skip to content

Commit 6fe923f

Browse files
committed
fix(ecs): added note for work around from LaunchType to CapacityProviderStrategy
1 parent fa332bc commit 6fe923f

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

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

Lines changed: 33 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,30 @@ 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+
When migrating from `launchType` to `capacityProviderStrategies` in a `FargateService`, CDK will replace the service during synthesis. 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) workaround to preserve the service:
1774+
1775+
```ts
1776+
declare const cluster: ecs.Cluster;
1777+
declare const taskDefinition: ecs.TaskDefinition;
1778+
1779+
const service = new ecs.FargateService(this, 'Service', {
1780+
cluster,
1781+
taskDefinition,
1782+
capacityProviderStrategies: [
1783+
{
1784+
capacityProvider: miCapacityProvider.capacityProviderName,
1785+
weight: 1,
1786+
},
1787+
],
1788+
});
1789+
1790+
// Escape hatch: Force launchType at the CloudFormation level to prevent service replacement
1791+
const cfnService = service.node.defaultChild as ecs.CfnService;
1792+
cfnService.launchType = 'FARGATE'; // or 'FARGATE_SPOT' depending on your capacity provider
17641793
```
17651794

17661795
### Cluster Default Provider Strategy

0 commit comments

Comments
 (0)