Skip to content

Commit bdd5249

Browse files
committed
feat: support healthCheckGracePeriod in QueueProcessingFargateService
Resolves #34220
1 parent e7a6e14 commit bdd5249

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as path from 'path';
2+
import * as ec2 from 'aws-cdk-lib/aws-ec2';
3+
import * as ecs from 'aws-cdk-lib/aws-ecs';
4+
import { App, Duration, Stack } from 'aws-cdk-lib';
5+
import * as integ from '@aws-cdk/integ-tests-alpha';
6+
import { QueueProcessingFargateService } from 'aws-cdk-lib/aws-ecs-patterns';
7+
8+
const app = new App();
9+
const stack = new Stack(app, 'aws-ecs-patterns-queue-grace-period');
10+
const vpc = new ec2.Vpc(stack, 'VPC', {
11+
restrictDefaultSecurityGroup: false,
12+
maxAzs: 2,
13+
});
14+
15+
new QueueProcessingFargateService(stack, 'QueueProcessingService', {
16+
vpc,
17+
memoryLimitMiB: 512,
18+
image: new ecs.AssetImage(path.join(__dirname, '..', 'sqs-reader')),
19+
minScalingCapacity: 0,
20+
healthCheckGracePeriod: Duration.seconds(120),
21+
});
22+
23+
new integ.IntegTest(app, 'queueProcessingFargateServiceGracePeriodTest', {
24+
testCases: [stack],
25+
});
26+
27+
app.synth();

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,19 @@ const networkLoadBalancedFargateService = new ecsPatterns.NetworkLoadBalancedFar
11431143
});
11441144
```
11451145

1146+
### Set healthCheckGracePeriod for QueueProcessingFargateService
1147+
1148+
```ts
1149+
declare const vpc: ec2.Vpc;
1150+
const queueProcessingFargateService = new ecsPatterns.QueueProcessingFargateService(this, 'Service', {
1151+
vpc,
1152+
memoryLimitMiB: 512,
1153+
image: ecs.ContainerImage.fromRegistry('test'),
1154+
minHealthyPercent: 100,
1155+
healthCheckGracePeriod: Duration.seconds(120),
1156+
});
1157+
```
1158+
11461159
### Set securityGroups for NetworkLoadBalancedFargateService
11471160

11481161
```ts

packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Construct } from 'constructs';
22
import * as ec2 from '../../../aws-ec2';
33
import { FargateService, FargateTaskDefinition, HealthCheck } from '../../../aws-ecs';
4-
import { FeatureFlags } from '../../../core';
4+
import { FeatureFlags, Duration } from '../../../core';
55
import * as cxapi from '../../../cx-api';
66
import { FargateServiceBaseProps } from '../base/fargate-service-base';
77
import { QueueProcessingServiceBase, QueueProcessingServiceBaseProps } from '../base/queue-processing-service-base';
@@ -47,6 +47,14 @@ export interface QueueProcessingFargateServiceProps extends QueueProcessingServi
4747
* @default false
4848
*/
4949
readonly assignPublicIp?: boolean;
50+
51+
/**
52+
* The period of time, in seconds, that the Amazon ECS service scheduler ignores unhealthy
53+
* Elastic Load Balancing target health checks after a task has first started.
54+
*
55+
* @default - defaults to 60 seconds if at least one load balancer is in-use and it is not already set
56+
*/
57+
readonly healthCheckGracePeriod?: Duration;
5058
}
5159

5260
/**
@@ -117,6 +125,7 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase {
117125
circuitBreaker: props.circuitBreaker,
118126
capacityProviderStrategies: props.capacityProviderStrategies,
119127
enableExecuteCommand: props.enableExecuteCommand,
128+
healthCheckGracePeriod: props.healthCheckGracePeriod,
120129
});
121130

122131
this.configureAutoscalingForService(this.service);

packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/queue-processing-fargate-service.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,3 +1005,24 @@ test('test Fargate queue worker service construct - with no taskDefinition or im
10051005
}).toThrow(new Error('You must specify one of: taskDefinition or image'));
10061006
});
10071007

1008+
test('test Fargate queue worker service construct - with healthCheckGracePeriod', () => {
1009+
// GIVEN
1010+
const stack = new cdk.Stack();
1011+
const vpc = new ec2.Vpc(stack, 'VPC');
1012+
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
1013+
const queue = new sqs.Queue(stack, 'Queue');
1014+
1015+
// WHEN
1016+
new ecsPatterns.QueueProcessingFargateService(stack, 'Service', {
1017+
cluster,
1018+
queue,
1019+
image: ecs.ContainerImage.fromRegistry('test'),
1020+
healthCheckGracePeriod: cdk.Duration.seconds(120),
1021+
});
1022+
1023+
// THEN
1024+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
1025+
HealthCheckGracePeriodSeconds: 120,
1026+
});
1027+
});
1028+

0 commit comments

Comments
 (0)