diff --git a/packages/@aws-cdk/aws-ecs-patterns/README.md b/packages/@aws-cdk/aws-ecs-patterns/README.md index 593421abeaa1a..e92c81ed43b95 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/README.md +++ b/packages/@aws-cdk/aws-ecs-patterns/README.md @@ -644,6 +644,23 @@ const scheduledFargateTask = new ecsPatterns.ScheduledFargateTask(this, 'Schedul }); ``` +### Set a custom container-level Healthcheck for NetworkLoadBalancedFargateService + +```ts +declare const vpc: ec2.Vpc; +declare const securityGroup: ec2.SecurityGroup; +const queueProcessingFargateService = new ecsPatterns.NetworkLoadBalancedFargateService(this, 'Service', { + vpc, + memoryLimitMiB: 512, + image: ecs.ContainerImage.fromRegistry('amazon/aws-example-app'), + healthCheck: { + command: ['CMD-SHELL', 'curl -f http://localhost:8080/ping || exit 1'], + interval: Duration.minutes(1), + retries: 3, + }, +}); +``` + ### Use the REMOVE_DEFAULT_DESIRED_COUNT feature flag The REMOVE_DEFAULT_DESIRED_COUNT feature flag is used to override the default desiredCount that is autogenerated by the CDK. This will set the desiredCount of any service created by any of the following constructs to be undefined. diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts index 9095be5cf2cd1..4f558b4c543b6 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts @@ -1,5 +1,5 @@ import { SubnetSelection } 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 { NetworkLoadBalancedServiceBase, NetworkLoadBalancedServiceBaseProps } from '../base/network-load-balanced-service-base'; @@ -84,6 +84,13 @@ export interface NetworkLoadBalancedFargateServiceProps extends NetworkLoadBalan * @default Latest */ readonly platformVersion?: FargatePlatformVersion; + + /** + * The health check command and associated configuration parameters for the container. + * + * @default - Default container level healthcheck + */ + readonly healthCheck?: HealthCheck; } /** @@ -134,6 +141,7 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic environment: taskImageOptions.environment, secrets: taskImageOptions.secrets, dockerLabels: taskImageOptions.dockerLabels, + healthCheck: props.healthCheck, }); container.addPortMappings({ containerPort: taskImageOptions.containerPort || 80, diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts index a00b676894c83..aa8c8b923ad96 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts @@ -431,6 +431,39 @@ test('setting NLB deployment controller', () => { }); }); +test('setting custom healthcheck in NLBFargateService', () => { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'VPC'); + const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); + + // WHEN + new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { + cluster, + taskImageOptions: { + image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'), + }, + healthCheck: { + command: ['CMD-SHELL', 'curl -f http://localhost:8080/ping || exit 1'], + interval: cdk.Duration.minutes(1), + retries: 3, + }, + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', { + ContainerDefinitions: [ + Match.objectLike({ + HealthCheck: { + Command: ['CMD-SHELL', 'curl -f http://localhost:8080/ping || exit 1'], + Interval: 60, + Retries: 3, + }, + }), + ], + }); +}); + test('setting ALB circuitBreaker works', () => { // GIVEN const stack = new cdk.Stack();