-
Notifications
You must be signed in to change notification settings - Fork 5
/
application-listener.ts
69 lines (63 loc) · 2.68 KB
/
application-listener.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 {
ApplicationListener,
ApplicationProtocol,
ListenerAction,
SslPolicy,
} from "aws-cdk-lib/aws-elasticloadbalancingv2";
import type { ApplicationListenerProps } from "aws-cdk-lib/aws-elasticloadbalancingv2";
import { GuAppAwareConstruct } from "../../../utils/mixin/app-aware-construct";
import type { GuCertificate } from "../../acm";
import type { AppIdentity, GuStack } from "../../core";
import type { GuApplicationTargetGroup } from "./application-target-group";
export interface GuApplicationListenerProps extends ApplicationListenerProps, AppIdentity {}
/**
* Construct which creates a Listener.
*
* This construct should be used in conjunction with [[`GuApplicationLoadBalancer`]] and [[`GuApplicationTargetGroup`]]
* 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).
*
* If you are running an application which only accepts traffic over HTTPS, consider using [[`GuHttpsApplicationListener`]]
* to reduce the amount of boilerplate needed when configuring your Listener.
*
* This resource is stateful.
* @see https://github.com/guardian/cdk/blob/main/docs/stateful-resources.md
*/
export class GuApplicationListener extends GuAppAwareConstruct(ApplicationListener) {
constructor(scope: GuStack, id: string, props: GuApplicationListenerProps) {
super(scope, id, { port: 443, protocol: ApplicationProtocol.HTTPS, ...props });
}
}
export interface GuHttpsApplicationListenerProps
extends Omit<GuApplicationListenerProps, "defaultAction" | "certificates">,
AppIdentity {
targetGroup: GuApplicationTargetGroup;
certificate?: GuCertificate;
}
/**
* Construct which creates a Listener which accepts HTTPS traffic.
*
* You must pass a [[`GuCertificate`]] to this Listener via the `certificate` prop.
*
* For general details about Listeners, see [[`GuApplicationListener`]].
*/
export class GuHttpsApplicationListener extends GuAppAwareConstruct(ApplicationListener) {
constructor(scope: GuStack, id: string, props: GuHttpsApplicationListenerProps) {
const { certificate, targetGroup } = props;
const mergedProps: GuApplicationListenerProps = {
port: certificate ? 443 : 8080,
protocol: certificate ? ApplicationProtocol.HTTPS : ApplicationProtocol.HTTP,
sslPolicy: SslPolicy.RECOMMENDED_TLS,
...props,
certificates: certificate
? [
{
certificateArn: certificate.certificateArn,
},
]
: [],
defaultAction: ListenerAction.forward([targetGroup]),
};
super(scope, id, mergedProps);
}
}