Skip to content

Commit

Permalink
feat(ecs-patterns): add capacity provider strategies to queue process…
Browse files Browse the repository at this point in the history
…ing service pattern (#15684)

closes #14781

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
tandfy authored Aug 26, 2021
1 parent 5cec2f8 commit f40e8d6
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 1 deletion.
51 changes: 51 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,57 @@ const queueProcessingFargateService = new QueueProcessingFargateService(stack, '
});
```

### Set capacityProviderStrategies for QueueProcessingFargateService

```ts
const vpc = new ec2.Vpc(stack, 'Vpc', { maxAzs: 1 });
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.enableFargateCapacityProviders();

const queueProcessingFargateService = new QueueProcessingFargateService(stack, 'Service', {
cluster,
memoryLimitMiB: 512,
image: ecs.ContainerImage.fromRegistry('test'),
capacityProviderStrategies: [
{
capacityProvider: 'FARGATE_SPOT',
weight: 2,
},
{
capacityProvider: 'FARGATE',
weight: 1,
},
],
});
```

### Set capacityProviderStrategies for QueueProcessingEc2Service

```ts
const vpc = new ec2.Vpc(stack, 'Vpc', { maxAzs: 1 });
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const autoScalingGroup = new autoscaling.AutoScalingGroup(stack, 'asg', {
vpc,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
machineImage: ecs.EcsOptimizedImage.amazonLinux2(),
});
const capacityProvider = new ecs.AsgCapacityProvider(stack, 'provider', {
autoScalingGroup,
});
cluster.addAsgCapacityProvider(capacityProvider);

const queueProcessingFargateService = new QueueProcessingFargateService(stack, 'Service', {
cluster,
memoryLimitMiB: 512,
image: ecs.ContainerImage.fromRegistry('test'),
capacityProviderStrategies: [
{
capacityProvider: capacityProvider.capacityProviderName,
},
],
});
```

### Select specific vpc subnets for ApplicationLoadBalancedFargateService

```ts
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ScalingInterval } from '@aws-cdk/aws-applicationautoscaling';
import { IVpc } from '@aws-cdk/aws-ec2';
import {
AwsLogDriver, BaseService, Cluster, ContainerImage, DeploymentController, DeploymentCircuitBreaker,
AwsLogDriver, BaseService, CapacityProviderStrategy, Cluster, ContainerImage, DeploymentController, DeploymentCircuitBreaker,
ICluster, LogDriver, PropagatedTagSource, Secret,
} from '@aws-cdk/aws-ecs';
import { IQueue, Queue } from '@aws-cdk/aws-sqs';
Expand Down Expand Up @@ -207,6 +207,14 @@ export interface QueueProcessingServiceBaseProps {
* @default - disabled
*/
readonly circuitBreaker?: DeploymentCircuitBreaker;

/**
* A list of Capacity Provider strategies used to place a service.
*
* @default - undefined
*
*/
readonly capacityProviderStrategies?: CapacityProviderStrategy[];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class QueueProcessingEc2Service extends QueueProcessingServiceBase {
enableECSManagedTags: props.enableECSManagedTags,
deploymentController: props.deploymentController,
circuitBreaker: props.circuitBreaker,
capacityProviderStrategies: props.capacityProviderStrategies,
});

this.configureAutoscalingForService(this.service);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase {
vpcSubnets: props.taskSubnets,
assignPublicIp: props.assignPublicIp,
circuitBreaker: props.circuitBreaker,
capacityProviderStrategies: props.capacityProviderStrategies,
});

this.configureAutoscalingForService(this.service);
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
},
"dependencies": {
"@aws-cdk/aws-applicationautoscaling": "0.0.0",
"@aws-cdk/aws-autoscaling": "0.0.0",
"@aws-cdk/aws-certificatemanager": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-ecs": "0.0.0",
Expand All @@ -94,6 +95,7 @@
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-applicationautoscaling": "0.0.0",
"@aws-cdk/aws-autoscaling": "0.0.0",
"@aws-cdk/aws-certificatemanager": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-ecs": "0.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ABSENT, expect, haveResource, haveResourceLike } from '@aws-cdk/assert-internal';
import * as autoscaling from '@aws-cdk/aws-autoscaling';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as ecs from '@aws-cdk/aws-ecs';
import * as sqs from '@aws-cdk/aws-sqs';
Expand Down Expand Up @@ -351,4 +352,48 @@ export = {

test.done();
},

'can set capacity provider strategies'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
const autoScalingGroup = new autoscaling.AutoScalingGroup(stack, 'asg', {
vpc,
instanceType: new ec2.InstanceType('bogus'),
machineImage: ecs.EcsOptimizedImage.amazonLinux2(),
});
const capacityProvider = new ecs.AsgCapacityProvider(stack, 'provider', {
autoScalingGroup,
});
cluster.addAsgCapacityProvider(capacityProvider);

// WHEN
new ecsPatterns.QueueProcessingEc2Service(stack, 'Service', {
cluster,
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 512,
capacityProviderStrategies: [
{
capacityProvider: capacityProvider.capacityProviderName,
},
],
});

// THEN
expect(stack).to(
haveResource('AWS::ECS::Service', {
LaunchType: ABSENT,
CapacityProviderStrategy: [
{
CapacityProvider: {
Ref: 'providerD3FF4D3A',
},
},
],
}),
);

test.done();
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,49 @@ export = {

test.done();
},

'can set capacity provider strategies'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', {
vpc,
});
cluster.enableFargateCapacityProviders();

// WHEN
new ecsPatterns.QueueProcessingFargateService(stack, 'Service', {
cluster,
image: ecs.ContainerImage.fromRegistry('test'),
capacityProviderStrategies: [
{
capacityProvider: 'FARGATE_SPOT',
weight: 2,
},
{
capacityProvider: 'FARGATE',
weight: 1,
},
],
});

// THEN
expect(stack).to(
haveResource('AWS::ECS::Service', {
LaunchType: ABSENT,
CapacityProviderStrategy: [
{
CapacityProvider: 'FARGATE_SPOT',
Weight: 2,
},
{
CapacityProvider: 'FARGATE',
Weight: 1,
},
],
}),
);

test.done();
},
};

0 comments on commit f40e8d6

Please sign in to comment.