/
http-origin.ts
89 lines (78 loc) · 3.09 KB
/
http-origin.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import * as cloudfront from '@aws-cdk/aws-cloudfront';
import * as cdk from '@aws-cdk/core';
/**
* Properties for an Origin backed by an S3 website-configured bucket, load balancer, or custom HTTP server.
*/
export interface HttpOriginProps extends cloudfront.OriginProps {
/**
* Specifies the protocol (HTTP or HTTPS) that CloudFront uses to connect to the origin.
*
* @default OriginProtocolPolicy.HTTPS_ONLY
*/
readonly protocolPolicy?: cloudfront.OriginProtocolPolicy;
/**
* The SSL versions to use when interacting with the origin.
*
* @default OriginSslPolicy.TLS_V1_2
*/
readonly originSslProtocols?: cloudfront.OriginSslPolicy[];
/**
* The HTTP port that CloudFront uses to connect to the origin.
*
* @default 80
*/
readonly httpPort?: number;
/**
* The HTTPS port that CloudFront uses to connect to the origin.
*
* @default 443
*/
readonly httpsPort?: number;
/**
* Specifies how long, in seconds, CloudFront waits for a response from the origin, also known as the origin response timeout.
* The valid range is from 1 to 180 seconds, inclusive.
*
* Note that values over 60 seconds are possible only after a limit increase request for the origin response timeout quota
* has been approved in the target account; otherwise, values over 60 seconds will produce an error at deploy time.
*
* @default Duration.seconds(30)
*/
readonly readTimeout?: cdk.Duration;
/**
* Specifies how long, in seconds, CloudFront persists its connection to the origin.
* The valid range is from 1 to 60 seconds, inclusive.
*
* @default Duration.seconds(5)
*/
readonly keepaliveTimeout?: cdk.Duration;
}
/**
* An Origin for an HTTP server or S3 bucket configured for website hosting.
*/
export class HttpOrigin extends cloudfront.OriginBase {
constructor(domainName: string, private readonly props: HttpOriginProps = {}) {
super(domainName, props);
validateSecondsInRangeOrUndefined('readTimeout', 1, 180, props.readTimeout);
validateSecondsInRangeOrUndefined('keepaliveTimeout', 1, 60, props.keepaliveTimeout);
}
protected renderCustomOriginConfig(): cloudfront.CfnDistribution.CustomOriginConfigProperty | undefined {
return {
originSslProtocols: this.props.originSslProtocols ?? [cloudfront.OriginSslPolicy.TLS_V1_2],
originProtocolPolicy: this.props.protocolPolicy ?? cloudfront.OriginProtocolPolicy.HTTPS_ONLY,
httpPort: this.props.httpPort,
httpsPort: this.props.httpsPort,
originReadTimeout: this.props.readTimeout?.toSeconds(),
originKeepaliveTimeout: this.props.keepaliveTimeout?.toSeconds(),
};
}
}
/**
* Throws an error if a duration is defined and not an integer number of seconds within a range.
*/
function validateSecondsInRangeOrUndefined(name: string, min: number, max: number, duration?: cdk.Duration) {
if (duration === undefined) { return; }
const value = duration.toSeconds();
if (!Number.isInteger(value) || value < min || value > max) {
throw new Error(`${name}: Must be an int between ${min} and ${max} seconds (inclusive); received ${value}.`);
}
}