Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure the healthcheck for a Target Group is legal #762

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ Object {
"HealthCheckIntervalSeconds": 10,
"HealthCheckPath": "/healthcheck",
"HealthCheckProtocol": "HTTP",
"HealthCheckTimeoutSeconds": 10,
"HealthCheckTimeoutSeconds": 5,
"HealthyThresholdCount": 5,
"Port": 80,
"Protocol": "HTTP",
Expand Down
21 changes: 18 additions & 3 deletions src/constructs/loadbalancing/alb/application-target-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "@aws-cdk/assert/jest";
import "../../../utils/test/jest";
import { Vpc } from "@aws-cdk/aws-ec2";
import { ApplicationProtocol } from "@aws-cdk/aws-elasticloadbalancingv2";
import { Stack } from "@aws-cdk/core";
import { Duration, Stack } from "@aws-cdk/core";
import { simpleGuStackForTesting } from "../../../utils/test";
import type { AppIdentity } from "../../core/identity";
import { GuApplicationTargetGroup } from "./application-target-group";
Expand Down Expand Up @@ -69,7 +69,7 @@ describe("The GuApplicationTargetGroup class", () => {
HealthCheckIntervalSeconds: 10,
HealthCheckPath: "/healthcheck",
HealthCheckProtocol: "HTTP",
HealthCheckTimeoutSeconds: 10,
HealthCheckTimeoutSeconds: 5,
HealthyThresholdCount: 5,
UnhealthyThresholdCount: 2,
});
Expand All @@ -91,7 +91,7 @@ describe("The GuApplicationTargetGroup class", () => {
HealthCheckPath: "/test",
HealthCheckPort: "9000",
HealthCheckProtocol: "HTTP",
HealthCheckTimeoutSeconds: 10,
HealthCheckTimeoutSeconds: 5,
HealthyThresholdCount: 5,
UnhealthyThresholdCount: 2,
});
Expand Down Expand Up @@ -120,4 +120,19 @@ describe("The GuApplicationTargetGroup class", () => {
Protocol: "HTTPS",
});
});

test("An illegal healthcheck is flagged", () => {
const stack = simpleGuStackForTesting();

expect(() => {
new GuApplicationTargetGroup(stack, "TargetGroup", {
...app,
vpc,
healthCheck: {
interval: Duration.seconds(10),
timeout: Duration.seconds(10),
},
});
}).toThrow(new Error("Illegal healthcheck configuration: timeout (10) must be lower than interval (10)"));
});
});
28 changes: 22 additions & 6 deletions src/constructs/loadbalancing/alb/application-target-group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApplicationProtocol, ApplicationTargetGroup, Protocol } from "@aws-cdk/aws-elasticloadbalancingv2";
import type { ApplicationTargetGroupProps } from "@aws-cdk/aws-elasticloadbalancingv2";
import { Duration } from "@aws-cdk/core";
import type { ApplicationTargetGroupProps, HealthCheck } from "@aws-cdk/aws-elasticloadbalancingv2";
import { Annotations, Duration } from "@aws-cdk/core";
import { GuStatefulMigratableConstruct } from "../../../utils/mixin";
import type { GuStack } from "../../core";
import { AppIdentity } from "../../core/identity";
Expand Down Expand Up @@ -34,19 +34,22 @@ export interface GuApplicationTargetGroupProps extends ApplicationTargetGroupPro
* ```
*/
export class GuApplicationTargetGroup extends GuStatefulMigratableConstruct(ApplicationTargetGroup) {
static DefaultHealthCheck = {
private static defaultHealthcheckInterval = Duration.seconds(10);
private static defaultHealthcheckTimeout = Duration.seconds(5);

static DefaultHealthCheck: HealthCheck = {
path: "/healthcheck",
protocol: Protocol.HTTP,
healthyThresholdCount: 5,
unhealthyThresholdCount: 2,
interval: Duration.seconds(10),
timeout: Duration.seconds(10),
interval: GuApplicationTargetGroup.defaultHealthcheckInterval,
timeout: GuApplicationTargetGroup.defaultHealthcheckTimeout,
};

constructor(scope: GuStack, id: string, props: GuApplicationTargetGroupProps) {
const { app } = props;

const mergedProps = {
const mergedProps: ApplicationTargetGroupProps = {
protocol: ApplicationProtocol.HTTP, // We terminate HTTPS at the load balancer level, so load balancer to ASG/EC2 traffic can be over HTTP
deregistrationDelay: Duration.seconds(30),
...props,
Expand All @@ -55,6 +58,19 @@ export class GuApplicationTargetGroup extends GuStatefulMigratableConstruct(Appl

super(scope, AppIdentity.suffixText({ app }, id), mergedProps);

const interval = mergedProps.healthCheck?.interval ?? GuApplicationTargetGroup.defaultHealthcheckInterval;
const timeout = mergedProps.healthCheck?.timeout ?? GuApplicationTargetGroup.defaultHealthcheckTimeout;

/*
The healthcheck `timeout` must be lower than `interval`
See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-elb-health-check.html#cfn-elb-healthcheck-timeout
*/
if (timeout.toSeconds() >= interval.toSeconds()) {
const message = `Illegal healthcheck configuration: timeout (${timeout.toSeconds()}) must be lower than interval (${interval.toSeconds()})`;
Annotations.of(this).addError(message); // adds a useful message to the console to aid debugging
throw new Error(message);
}

AppIdentity.taggedConstruct({ app }, this);
}
}
4 changes: 2 additions & 2 deletions src/patterns/__snapshots__/ec2-app.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ Object {
"HealthCheckIntervalSeconds": 10,
"HealthCheckPath": "/healthcheck",
"HealthCheckProtocol": "HTTP",
"HealthCheckTimeoutSeconds": 10,
"HealthCheckTimeoutSeconds": 5,
"HealthyThresholdCount": 5,
"Port": 3000,
"Protocol": "HTTP",
Expand Down Expand Up @@ -1562,7 +1562,7 @@ Object {
"HealthCheckIntervalSeconds": 10,
"HealthCheckPath": "/healthcheck",
"HealthCheckProtocol": "HTTP",
"HealthCheckTimeoutSeconds": 10,
"HealthCheckTimeoutSeconds": 5,
"HealthyThresholdCount": 5,
"Port": 3000,
"Protocol": "HTTP",
Expand Down