Skip to content

Commit

Permalink
Merge pull request #201 from guardian/jl/elb-default-healthcheck
Browse files Browse the repository at this point in the history
Add default health check values for GuTargetGroup
  • Loading branch information
Jamie Lynch authored Jan 26, 2021
2 parents e3211d7 + b730859 commit c40f390
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/constructs/loadbalancing/elb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,43 @@ describe("The GuApplicationTargetGroup class", () => {
const json = SynthUtils.toCloudFormation(stack) as SynthedStack;
expect(Object.keys(json.Resources)).not.toContain("ApplicationTargetGroup");
});

test("uses default health check properties", () => {
const stack = simpleGuStackForTesting();
new GuApplicationTargetGroup(stack, "ApplicationTargetGroup", {
vpc,
});

expect(stack).toHaveResource("AWS::ElasticLoadBalancingV2::TargetGroup", {
HealthCheckIntervalSeconds: 30,
HealthCheckPath: "/healthcheck",
HealthCheckPort: "9000",
HealthCheckProtocol: "HTTP",
HealthCheckTimeoutSeconds: 10,
HealthyThresholdCount: 2,
UnhealthyThresholdCount: 5,
});
});

test("merges any health check properties provided", () => {
const stack = simpleGuStackForTesting();
new GuApplicationTargetGroup(stack, "ApplicationTargetGroup", {
vpc,
healthCheck: {
path: "/test",
},
});

expect(stack).toHaveResource("AWS::ElasticLoadBalancingV2::TargetGroup", {
HealthCheckIntervalSeconds: 30,
HealthCheckPath: "/test",
HealthCheckPort: "9000",
HealthCheckProtocol: "HTTP",
HealthCheckTimeoutSeconds: 10,
HealthyThresholdCount: 2,
UnhealthyThresholdCount: 5,
});
});
});

describe("The GuApplicationListener class", () => {
Expand Down
21 changes: 19 additions & 2 deletions src/constructs/loadbalancing/elb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
ApplicationLoadBalancer,
ApplicationProtocol,
ApplicationTargetGroup,
Protocol,
} from "@aws-cdk/aws-elasticloadbalancingv2";
import { Duration } from "@aws-cdk/core";
import type { GuStack } from "../core";

interface GuApplicationLoadBalancerProps extends ApplicationLoadBalancerProps {
Expand All @@ -36,10 +38,25 @@ export interface GuApplicationTargetGroupProps extends ApplicationTargetGroupPro
}

export class GuApplicationTargetGroup extends ApplicationTargetGroup {
static DefaultHealthCheck = {
port: "9000",
path: "/healthcheck",
protocol: Protocol.HTTP,
healthyThresholdCount: 2,
unhealthyThresholdCount: 5,
interval: Duration.seconds(30),
timeout: Duration.seconds(10),
};

constructor(scope: GuStack, id: string, props: GuApplicationTargetGroupProps) {
super(scope, id, props);
const mergedProps = {
...props,
healthCheck: { ...GuApplicationTargetGroup.DefaultHealthCheck, ...props.healthCheck },
};

if (props.overrideId || (scope.migratedFromCloudFormation && props.overrideId !== false))
super(scope, id, mergedProps);

if (mergedProps.overrideId || (scope.migratedFromCloudFormation && mergedProps.overrideId !== false))
(this.node.defaultChild as CfnTargetGroup).overrideLogicalId(id);
}
}
Expand Down

0 comments on commit c40f390

Please sign in to comment.