From 2e7be05b1272dc31ce94d3031baf9e348b25f03e Mon Sep 17 00:00:00 2001 From: VarshaVid <96799940+VarshaVid@users.noreply.github.com> Date: Mon, 24 Jan 2022 17:08:07 -0800 Subject: [PATCH] feat(aws-ecs-patterns): adding support for custom HealthCheck while creating QueueProcessingFargateService (#18219) The `QueueProcessingFargateService` construct internally creates a service Container with the default healthcheck that cannot currently be overriden by the user. This change allows users to optionally provide a custom healthcheck that can be passed on to the internal ECS container definition. fixes #15636 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-ecs-patterns/README.md | 20 +++++++++++++++++++ .../queue-processing-fargate-service.ts | 10 +++++++++- ...ssing-fargate-service-public.expected.json | 9 +++++++++ ...queue-processing-fargate-service-public.ts | 7 ++++++- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/README.md b/packages/@aws-cdk/aws-ecs-patterns/README.md index 386682e0b75d9..593421abeaa1a 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/README.md +++ b/packages/@aws-cdk/aws-ecs-patterns/README.md @@ -544,6 +544,26 @@ const queueProcessingFargateService = new ecsPatterns.QueueProcessingFargateServ }); ``` +### Set a custom container-level Healthcheck for QueueProcessingFargateService + +```ts +declare const vpc: ec2.Vpc; +declare const securityGroup: ec2.SecurityGroup; +const queueProcessingFargateService = new ecsPatterns.QueueProcessingFargateService(this, 'Service', { + vpc, + memoryLimitMiB: 512, + image: ecs.ContainerImage.fromRegistry('test'), + healthCheck: { + command: [ "CMD-SHELL", "curl -f http://localhost/ || exit 1" ], + // the properties below are optional + interval: Duration.minutes(30), + retries: 123, + startPeriod: Duration.minutes(30), + timeout: Duration.minutes(30), + }, +}); +``` + ### Set capacityProviderStrategies for QueueProcessingEc2Service ```ts diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts index 033d19dc39612..ff63c4a3502d8 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts @@ -1,5 +1,5 @@ import * as ec2 from '@aws-cdk/aws-ec2'; -import { FargatePlatformVersion, FargateService, FargateTaskDefinition } from '@aws-cdk/aws-ecs'; +import { FargatePlatformVersion, FargateService, FargateTaskDefinition, HealthCheck } from '@aws-cdk/aws-ecs'; import * as cxapi from '@aws-cdk/cx-api'; import { Construct } from 'constructs'; import { QueueProcessingServiceBase, QueueProcessingServiceBaseProps } from '../base/queue-processing-service-base'; @@ -69,6 +69,13 @@ export interface QueueProcessingFargateServiceProps extends QueueProcessingServi */ readonly containerName?: string; + /** + * The health check command and associated configuration parameters for the container. + * + * @default - Health check configuration from container. + */ + readonly healthCheck?: HealthCheck; + /** * The subnets to associate with the service. * @@ -127,6 +134,7 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase { environment: this.environment, secrets: this.secrets, logging: this.logDriver, + healthCheck: props.healthCheck, }); // The desiredCount should be removed from the fargate service when the feature flag is removed. diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.queue-processing-fargate-service-public.expected.json b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.queue-processing-fargate-service-public.expected.json index ae18adac13bbf..8af962023d1d2 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.queue-processing-fargate-service-public.expected.json +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.queue-processing-fargate-service-public.expected.json @@ -604,6 +604,15 @@ } ], "Essential": true, + "HealthCheck": { + "Command": [ + "CMD-SHELL", + "curl -f http://localhost/ || exit 1" + ], + "Interval": 720, + "Retries": 34, + "Timeout": 5 + }, "Image": { "Fn::Join": [ "", diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.queue-processing-fargate-service-public.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.queue-processing-fargate-service-public.ts index 5b0d74f9e3ceb..4877a7d211747 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.queue-processing-fargate-service-public.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.queue-processing-fargate-service-public.ts @@ -1,7 +1,7 @@ import * as path from 'path'; import * as ec2 from '@aws-cdk/aws-ec2'; import * as ecs from '@aws-cdk/aws-ecs'; -import { App, Stack } from '@aws-cdk/core'; +import { App, Stack, Duration } from '@aws-cdk/core'; import { QueueProcessingFargateService } from '../../lib'; @@ -14,6 +14,11 @@ new QueueProcessingFargateService(stack, 'PublicQueueService', { memoryLimitMiB: 512, image: new ecs.AssetImage(path.join(__dirname, '..', 'sqs-reader')), assignPublicIp: true, + healthCheck: { + command: ['CMD-SHELL', 'curl -f http://localhost/ || exit 1'], + interval: Duration.minutes(12), + retries: 34, + }, }); app.synth();