-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapplication-target-group.ts
69 lines (62 loc) · 3.23 KB
/
application-target-group.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Annotations, Duration } from "aws-cdk-lib";
import { ApplicationProtocol, ApplicationTargetGroup, Protocol } from "aws-cdk-lib/aws-elasticloadbalancingv2";
import type { ApplicationTargetGroupProps, HealthCheck } from "aws-cdk-lib/aws-elasticloadbalancingv2";
import { GuAppAwareConstruct } from "../../../utils/mixin/app-aware-construct";
import type { AppIdentity, GuStack } from "../../core";
export interface GuApplicationTargetGroupProps extends ApplicationTargetGroupProps, AppIdentity {}
/**
* Construct which creates a Target Group.
*
* This construct should be used in conjunction with [[`GuApplicationLoadBalancer`]] and [[`GuApplicationListener`]]
* to route traffic to your application. For more details on these three components, see the
* [AWS documentation](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html#application-load-balancer-components).
*
* By default, Target Groups created via this construct will perform a healthcheck against `/healthcheck` on your application's
* traffic port (as specified via the `port` prop). All healthcheck defaults can be overridden via the `healthcheck` prop.
*
* For example, to use `/test` for the healthcheck path use:
*
* ```typescript
* new GuApplicationTargetGroup(stack, "TargetGroup", {
* // other props
* healthCheck: {
* path: "/test",
* },
* });
* ```
*
* This resource is stateful.
* @see https://github.com/guardian/cdk/blob/main/docs/stateful-resources.md
*/
export class GuApplicationTargetGroup extends GuAppAwareConstruct(ApplicationTargetGroup) {
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: GuApplicationTargetGroup.defaultHealthcheckInterval,
timeout: GuApplicationTargetGroup.defaultHealthcheckTimeout,
};
constructor(scope: GuStack, id: string, props: GuApplicationTargetGroupProps) {
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,
healthCheck: { ...GuApplicationTargetGroup.DefaultHealthCheck, ...props.healthCheck },
};
super(scope, 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);
}
}
}