Skip to content

Commit ec4be21

Browse files
committed
add support for custom domain names
1 parent 4b4bdd5 commit ec4be21

File tree

5 files changed

+125
-75
lines changed

5 files changed

+125
-75
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ integration_test_directory_root = tests/live_integration/
1111
# CHANGE ME (as needed)
1212
application_key=infra-core-api
1313
application_name="InfraCoreApi"
14-
techlead="dsingh14@illinois.edu"
14+
techlead="tarasha2@illinois.edu"
1515
region="us-east-1"
1616

1717
# DO NOT CHANGE

cloudformation/custom-domain.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Parameters:
2+
GWCertArn:
3+
Description: Certificate ARN
4+
Type: String
5+
GWBaseDomainName:
6+
Description: Base domain name
7+
Type: String
8+
GWApiId:
9+
Description: API ID
10+
Type: String
11+
GWHostedZoneId:
12+
Description: Hosted Zone ID
13+
Type: String
14+
RunEnvironment:
15+
Type: String
16+
AllowedValues: [ 'dev', 'prod' ]
17+
RecordName:
18+
Type: String
19+
20+
Conditions:
21+
IsDev: !Equals [!Ref RunEnvironment, 'dev']
22+
23+
Resources:
24+
CustomDomainName:
25+
Type: AWS::ApiGateway::DomainName
26+
Properties:
27+
RegionalCertificateArn: !Ref GWCertArn
28+
EndpointConfiguration:
29+
Types:
30+
- REGIONAL
31+
DomainName: !Sub "${RecordName}.${GWBaseDomainName}"
32+
SecurityPolicy: TLS_1_2
33+
34+
CDApiMapping:
35+
Type: 'AWS::ApiGatewayV2::ApiMapping'
36+
Properties:
37+
DomainName: !Ref CustomDomainName
38+
ApiId: !Ref GWApiId
39+
Stage: default
40+
41+
CDRoute53RecordSetDev:
42+
Condition: IsDev
43+
Type: AWS::Route53::RecordSet
44+
Properties:
45+
HostedZoneId: !Ref GWHostedZoneId
46+
Name: !Sub "${RecordName}.${GWBaseDomainName}"
47+
Type: A
48+
AliasTarget:
49+
DNSName: !GetAtt CustomDomainName.RegionalDomainName
50+
HostedZoneId: !GetAtt CustomDomainName.RegionalHostedZoneId

cloudformation/main.yml

Lines changed: 36 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,42 @@ Resources:
8181
LambdaFunctionName: !Sub ${ApplicationPrefix}-lambda
8282
LogRetentionDays: !FindInMap [General, !Ref RunEnvironment, LogRetentionDays]
8383

84+
IcalDomainProxy:
85+
Type: AWS::Serverless::Application
86+
Properties:
87+
Location: ./custom-domain.yml
88+
Parameters:
89+
RunEnvironment: !Ref RunEnvironment
90+
RecordName: ical
91+
GWBaseDomainName: !FindInMap
92+
- ApiGwConfig
93+
- !Ref RunEnvironment
94+
- LinkryApiDomainName
95+
GWCertArn: !FindInMap
96+
- ApiGwConfig
97+
- !Ref RunEnvironment
98+
- LinkryApiCertificateArn
99+
GWApiId: !Ref AppApiGateway
100+
GWHostedZoneId: !FindInMap [ApiGwConfig, !Ref RunEnvironment, HostedZoneId]
101+
102+
LinkryDomainProxy:
103+
Type: AWS::Serverless::Application
104+
Properties:
105+
Location: ./custom-domain.yml
106+
Parameters:
107+
RunEnvironment: !Ref RunEnvironment
108+
RecordName: go
109+
GWBaseDomainName: !FindInMap
110+
- ApiGwConfig
111+
- !Ref RunEnvironment
112+
- LinkryApiDomainName
113+
GWCertArn: !FindInMap
114+
- ApiGwConfig
115+
- !Ref RunEnvironment
116+
- LinkryApiCertificateArn
117+
GWApiId: !Ref AppApiGateway
118+
GWHostedZoneId: !FindInMap [ApiGwConfig, !Ref RunEnvironment, HostedZoneId]
119+
84120
AppApiLambdaFunction:
85121
Type: AWS::Serverless::Function
86122
DependsOn:
@@ -302,75 +338,3 @@ Resources:
302338
- ":"
303339
- !Ref AppApiGateway
304340
- "/*/*/*"
305-
306-
LinkryCustomDomainName:
307-
Type: AWS::ApiGateway::DomainName
308-
Properties:
309-
RegionalCertificateArn: !FindInMap [ApiGwConfig, !Ref RunEnvironment, LinkryApiCertificateArn]
310-
EndpointConfiguration:
311-
Types:
312-
- REGIONAL
313-
DomainName: !Sub
314-
- "go.${BaseDomainName}"
315-
- BaseDomainName: !FindInMap
316-
- ApiGwConfig
317-
- !Ref RunEnvironment
318-
- LinkryApiDomainName
319-
SecurityPolicy: TLS_1_2
320-
321-
LinkryRoute53RecordSetDev:
322-
Condition: IsDev
323-
Type: AWS::Route53::RecordSet
324-
Properties:
325-
HostedZoneId: !FindInMap [ApiGwConfig, !Ref RunEnvironment, HostedZoneId]
326-
Name: !Sub
327-
- "go.${BaseDomainName}"
328-
- BaseDomainName: !FindInMap
329-
- ApiGwConfig
330-
- !Ref RunEnvironment
331-
- LinkryApiDomainName
332-
Type: A
333-
AliasTarget:
334-
DNSName: !GetAtt LinkryCustomDomainName.RegionalDomainName
335-
HostedZoneId: !GetAtt LinkryCustomDomainName.RegionalHostedZoneId
336-
337-
LinkryApiMapping:
338-
Type: 'AWS::ApiGatewayV2::ApiMapping'
339-
Properties:
340-
DomainName: !Sub
341-
- "go.${BaseDomainName}"
342-
- BaseDomainName: !FindInMap
343-
- ApiGwConfig
344-
- !Ref RunEnvironment
345-
- LinkryApiDomainName
346-
ApiId: !Ref AppApiGateway
347-
Stage: default
348-
349-
IcalCustomDomainName:
350-
Type: AWS::ApiGateway::DomainName
351-
Condition: IsProd
352-
Properties:
353-
RegionalCertificateArn: !FindInMap [ApiGwConfig, !Ref RunEnvironment, LinkryApiCertificateArn]
354-
EndpointConfiguration:
355-
Types:
356-
- REGIONAL
357-
DomainName: !Sub
358-
- "ical.${BaseDomainName}"
359-
- BaseDomainName: !FindInMap
360-
- ApiGwConfig
361-
- !Ref RunEnvironment
362-
- LinkryApiDomainName
363-
SecurityPolicy: TLS_1_2
364-
365-
IcalApiMapping:
366-
Type: 'AWS::ApiGatewayV2::ApiMapping'
367-
Condition: IsProd
368-
Properties:
369-
DomainName: !Sub
370-
- "ical.${BaseDomainName}"
371-
- BaseDomainName: !FindInMap
372-
- ApiGwConfig
373-
- !Ref RunEnvironment
374-
- LinkryApiDomainName
375-
ApiId: !Ref AppApiGateway
376-
Stage: default

cloudformation/phony-swagger.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ info:
77
88

99
paths:
10+
/:
11+
x-amazon-apigateway-any-method:
12+
responses:
13+
200:
14+
description: OK
15+
16+
x-amazon-apigateway-auth:
17+
type: NONE
18+
19+
x-amazon-apigateway-integration:
20+
responses:
21+
default:
22+
statusCode: 200
23+
passthroughBehavior: when_no_match
24+
httpMethod: POST
25+
contentHandling: CONVERT_TO_TEXT
26+
type: aws_proxy
27+
uri:
28+
Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${ApplicationPrefix}-lambda/invocations"
29+
1030
/{proxy+}:
1131
x-amazon-apigateway-any-method:
1232
responses:
@@ -25,4 +45,4 @@ paths:
2545
contentHandling: CONVERT_TO_TEXT
2646
type: aws_proxy
2747
uri:
28-
Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${ApplicationPrefix}-lambda/invocations"
48+
Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${ApplicationPrefix}-lambda/invocations"

src/api/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ async function init() {
2828
logger: {
2929
level: process.env.LOG_LEVEL || "info",
3030
},
31+
rewriteUrl: (req) => {
32+
const url = req.url;
33+
const hostname = req.headers.host || "";
34+
const customDomainBaseMappers: Record<string, string> = {
35+
"ical.acm.illinois.edu": `/api/v1/ical${url}`,
36+
"ical.aws.qa.acmuiuc.org": `/api/v1/ical${url}`,
37+
"go.acm.illinois.edu": `/api/v1/linkry/redir${url}`,
38+
"go.aws.qa.acmuiuc.org": `/api/v1/linkry/redir${url}`,
39+
};
40+
if (hostname in customDomainBaseMappers) {
41+
return customDomainBaseMappers[hostname];
42+
}
43+
return url || "/";
44+
},
3145
disableRequestLogging: true,
3246
genReqId: (request) => {
3347
const header = request.headers["x-apigateway-event"];
@@ -56,7 +70,9 @@ async function init() {
5670
environmentConfig[app.runEnvironment as RunEnvironment];
5771
app.addHook("onRequest", (req, _, done) => {
5872
req.startTime = now();
59-
req.log.info({ url: req.raw.url }, "received request");
73+
const hostname = req.hostname;
74+
const url = req.raw.url;
75+
req.log.info({ hostname, url }, "received request");
6076
done();
6177
});
6278

0 commit comments

Comments
 (0)