Skip to content

Commit

Permalink
feat(aws-ecs-patterns): Support custom healthCheck
Browse files Browse the repository at this point in the history
exposes a healthcheck prop in the NetworkLoadBalancedFargateService ecs pattern
that is passed through to the underlying TaskDefinition resource. Allows users
to specify a custom health check for the Fargate containers

fixes: aws#20233
  • Loading branch information
Samrat Jha committed May 6, 2022
1 parent a8694b9 commit 2f4fb48
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2f4fb48

Please sign in to comment.