-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalAcceleratorWithApplicationLoadBalancerConstruct.ts
More file actions
195 lines (174 loc) · 6.31 KB
/
GlobalAcceleratorWithApplicationLoadBalancerConstruct.ts
File metadata and controls
195 lines (174 loc) · 6.31 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import * as globalaccelerator from "aws-cdk-lib/aws-globalaccelerator";
import * as elbv2 from "aws-cdk-lib/aws-elasticloadbalancingv2";
import * as ga_endpoints from "aws-cdk-lib/aws-globalaccelerator-endpoints";
import * as route53 from "aws-cdk-lib/aws-route53";
import * as route53_targets from "aws-cdk-lib/aws-route53-targets";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import * as acm from "aws-cdk-lib/aws-certificatemanager";
import { StringParameter } from "aws-cdk-lib/aws-ssm";
/*
1. Global Accelerator
2. Application Load Balancer
*/
export interface GlobalAcceleratorWithApplicationLoadBalancerConstructProps extends cdk.StackProps {
appName: string;
vpc: ec2.IVpc;
tagEnv: string;
domainName: string;
apiSubdomainName: string;
}
export class GlobalAcceleratorWithApplicationLoadBalancerConstruct extends Construct {
public readonly loadBalancer: elbv2.ApplicationLoadBalancer;
public readonly httpListener: elbv2.ApplicationListener;
public readonly httpsListener?: elbv2.ApplicationListener;
constructor(scope: Construct, id: string, props: GlobalAcceleratorWithApplicationLoadBalancerConstructProps) {
super(scope, id);
const region = cdk.Stack.of(this).region;
/* Lookup the hosted zone */
const hostedZone = route53.HostedZone.fromLookup(
this,
`${props.appName}-hosted-zone-lookup`,
{
domainName: `${props.tagEnv}.${props.domainName}`,
}
);
/* Create Global Accelerator */
const accelerator = new globalaccelerator.Accelerator(
this,
`${props.appName}-global-accelerator`,
{
acceleratorName: `${props.appName}-global-accelerator`,
}
);
/* Create an HTTP Listener for the Accelerator */
const httpListener = accelerator.addListener(
`${props.appName}-http-listener`,
{
listenerName: `${props.appName}-http-listener`,
portRanges: [{ fromPort: 80 }],
}
);
/* Create a Listener for the Accelerator */
const httpsListener = accelerator.addListener(
`${props.appName}-https-listener`,
{
listenerName: `${props.appName}-https-listener`,
portRanges: [{ fromPort: 443 }],
}
);
/* Create an A record in Route 53 that points to the Global Accelerator's DNS name */
new route53.ARecord(this, `${props.appName}-alias-record`, {
zone: hostedZone,
target: route53.RecordTarget.fromAlias(
new route53_targets.GlobalAcceleratorTarget(accelerator)
),
recordName: `${props.apiSubdomainName}.${props.tagEnv}.${props.domainName}`,
});
/* Create the Application Load Balancer */
this.loadBalancer = new elbv2.ApplicationLoadBalancer(
this,
`${props.appName}-${props.tagEnv}-load-balancer-construct`,
{
vpc: props.vpc,
internetFacing: true,
loadBalancerName: `${props.appName}-${props.tagEnv}-alb`,
vpcSubnets: {
subnets: props.vpc.selectSubnets({
onePerAz: true,
subnetType: ec2.SubnetType.PUBLIC,
}).subnets,
},
}
);
/* Create a certificate for the load balancer */
const certificate = new acm.Certificate(
this,
`${props.appName}-certificate`,
{
domainName: `${region}.${props.apiSubdomainName}.${props.tagEnv}.${props.domainName}`,
subjectAlternativeNames: [
`${props.apiSubdomainName}.${props.tagEnv}.${props.domainName}`,
],
validation: acm.CertificateValidation.fromDns(hostedZone),
}
);
/* Create a listener for the load balancer */
this.httpsListener = this.loadBalancer.addListener(
`${props.appName}-https-listener`,
{
port: 443,
certificates: [certificate],
sslPolicy: elbv2.SslPolicy.RECOMMENDED_TLS,
defaultAction: elbv2.ListenerAction.fixedResponse(404, {
contentType: "text/plain",
messageBody: "Not Found",
}),
}
);
/* Create a listener rule for HTTP port 80 to HTTPS port 443 redirection */
this.httpListener = this.loadBalancer.addListener(
`${props.appName}-http-listener`,
{
port: 80,
defaultAction: elbv2.ListenerAction.redirect({
protocol: "HTTPS",
port: "443",
}),
}
);
/* Add Alias A record to the load balancer */
new route53.ARecord(this, `${props.appName}-alias-record-${region}`, {
zone: hostedZone,
target: route53.RecordTarget.fromAlias(
new route53_targets.LoadBalancerTarget(this.loadBalancer)
),
recordName: `${region}.${props.apiSubdomainName}.${props.tagEnv}.${props.domainName}`,
});
/* Add load balancer arn to SSM parameter store */
new StringParameter(this, `${props.appName}-${region}-load-balancer-arn`, {
parameterName: `/${props.appName}/${props.tagEnv}/shared/load-balancer-arn`,
stringValue: this.loadBalancer.loadBalancerArn,
});
const endpoints: ga_endpoints.ApplicationLoadBalancerEndpoint[] = [];
const lbArn = this.loadBalancer.loadBalancerArn;
const lbSecurityGroupId =
this.loadBalancer.connections.securityGroups[0].securityGroupId;
if (lbArn && lbSecurityGroupId) {
const endpoint = new ga_endpoints.ApplicationLoadBalancerEndpoint(
elbv2.ApplicationLoadBalancer.fromApplicationLoadBalancerAttributes(
this,
`${props.appName}-load-balancer-lookup-${region}`,
{
loadBalancerArn: lbArn,
securityGroupId: lbSecurityGroupId as string,
}
),
{
weight: 128,
preserveClientIp: true,
}
);
endpoints.push(endpoint);
/* https endpoint group (via port 443)*/
httpsListener.addEndpointGroup(
`${props.appName}-${region}-https-endpoint-group`,
{
endpoints: [endpoint],
healthCheckPath: "/health-check",
endpointGroupName: `${props.appName}-${region}-https-endpoint-group`,
}
);
/* http endpoint group (via port 80)*/
httpListener.addEndpointGroup(
`${props.appName}-${region}-http-endpoint-group`,
{
endpoints: [endpoint],
healthCheckPath: "/health-check",
endpointGroupName: `${props.appName}-${region}-http-endpoint-group`,
}
);
}
}
}