diff --git a/src/constructs/loadbalancing/elb.test.ts b/src/constructs/loadbalancing/elb.test.ts index 5862c5c3d8..cc58491258 100644 --- a/src/constructs/loadbalancing/elb.test.ts +++ b/src/constructs/loadbalancing/elb.test.ts @@ -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", () => { diff --git a/src/constructs/loadbalancing/elb.ts b/src/constructs/loadbalancing/elb.ts index c65fcd4e99..2a8773214f 100644 --- a/src/constructs/loadbalancing/elb.ts +++ b/src/constructs/loadbalancing/elb.ts @@ -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 { @@ -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); } }