Skip to content

Commit 3490cb5

Browse files
authored
Fix: [AEA-4864] - Fix Cloudfront site routing (#257)
## Summary - Routine Change - 🤖 Operational or Infrastructure Change ### Details - Updates CF routing - Updates CF function logic for static files - Updates CF functions to deploy sequentially
1 parent 35e317d commit 3490cb5

File tree

3 files changed

+89
-9
lines changed

3 files changed

+89
-9
lines changed

packages/cdk/resources/CloudfrontBehaviors.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ export class CloudfrontBehaviors extends Construct{
9494
sourceFileName: "s3404ModifyStatusCode.js",
9595
keyValueStore: keyValueStore
9696
})
97+
/* Add dependency on previous function to force them to build one by one to avoid aws limits
98+
on how many can be created simultaneously */
99+
s3404ModifyStatusCodeFunction.node.addDependency(s3404UriRewriteFunction)
97100

98101
const s3500UriRewriteFunction = new CloudfrontFunction(this, "S3500UriRewriteFunction", {
99102
functionName: `${props.serviceName}-S3500UriRewriteFunction`,
@@ -106,6 +109,9 @@ export class CloudfrontBehaviors extends Construct{
106109
}
107110
]
108111
})
112+
/* Add dependency on previous function to force them to build one by one to avoid aws limits
113+
on how many can be created simultaneously */
114+
s3500UriRewriteFunction.node.addDependency(s3404ModifyStatusCodeFunction)
109115

110116
const s3StaticContentUriRewriteFunction = new CloudfrontFunction(this, "S3StaticContentUriRewriteFunction", {
111117
functionName: `${props.serviceName}-S3StaticContentUriRewriteFunction`,
@@ -122,6 +128,9 @@ export class CloudfrontBehaviors extends Construct{
122128
}
123129
]
124130
})
131+
/* Add dependency on previous function to force them to build one by one to avoid aws limits
132+
on how many can be created simultaneously */
133+
s3StaticContentUriRewriteFunction.node.addDependency(s3500UriRewriteFunction)
125134

126135
const apiGatewayStripPathFunction = new CloudfrontFunction(this, "ApiGatewayStripPathFunction", {
127136
functionName: `${props.serviceName}-ApiGatewayStripPathFunction`,
@@ -134,6 +143,9 @@ export class CloudfrontBehaviors extends Construct{
134143
}
135144
]
136145
})
146+
/* Add dependency on previous function to force them to build one by one to avoid aws limits
147+
on how many can be created simultaneously */
148+
apiGatewayStripPathFunction.node.addDependency(s3StaticContentUriRewriteFunction)
137149

138150
const s3JwksUriRewriteFunction = new CloudfrontFunction(this, "s3JwksUriRewriteFunction", {
139151
functionName: `${props.serviceName}-s3JwksUriRewriteFunction`,
@@ -146,6 +158,9 @@ export class CloudfrontBehaviors extends Construct{
146158
}
147159
]
148160
})
161+
/* Add dependency on previous function to force them to build one by one to avoid aws limits
162+
on how many can be created simultaneously */
163+
s3JwksUriRewriteFunction.node.addDependency(apiGatewayStripPathFunction)
149164

150165
// eslint-disable-next-line max-len
151166
const authDemoStaticContentUriRewriteFunction = new CloudfrontFunction(this, "authDemoStaticContentUriRewriteFunction", {
@@ -163,9 +178,12 @@ export class CloudfrontBehaviors extends Construct{
163178
}
164179
]
165180
})
181+
/* Add dependency on previous function to force them to build one by one to avoid aws limits
182+
on how many can be created simultaneously */
183+
authDemoStaticContentUriRewriteFunction.node.addDependency(s3JwksUriRewriteFunction)
166184

167185
const additionalBehaviors = {
168-
"/site/*": {
186+
"/site*": {
169187
origin: props.staticContentBucketOrigin,
170188
allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
171189
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,

packages/cloudfrontFunctions/src/s3StaticContentUriRewrite.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,26 @@ export async function handler(event) {
1717
const versionMatches = uri.match(versionPattern)
1818
const prMatches = uri.match(prPattern)
1919

20+
let originUri
2021
let version
22+
let remaining_uri
2123
if (versionMatches && versionMatches.length === 1){
2224
version = versionMatches[0]
25+
remaining_uri = uri.split(versionMatches[0])[1]
2326
} else if (prMatches && prMatches.length === 1) {
2427
version = prMatches[0]
28+
remaining_uri = uri.split(prMatches[0])[1]
2529
} else {
2630
version = currentVersion
31+
remaining_uri = uri
2732
}
2833

29-
let originUri
30-
if (uri.endsWith("/")) {
31-
originUri = `/${version}/index.html`
34+
if (!remaining_uri.startsWith("/") && remaining_uri !== "") {
35+
originUri = "/404.html"
36+
} else if (remaining_uri.includes(".")){
37+
originUri = `/${version}${remaining_uri}`
3238
} else {
33-
if (version === currentVersion) {
34-
originUri = `/${version}${uri}`
35-
} else {
36-
originUri = uri
37-
}
39+
originUri = `/${version}/index.html`
3840
}
3941
request.uri = originUri
4042

packages/cloudfrontFunctions/tests/testS3StaticContentUriRewrite.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,91 @@ describe("S3 Content URI Rewrite", () => {
55
const testCases = [
66
{
77
description: "Current version site root",
8+
requestUri: "/site",
9+
expectedOriginUri: "/v1.0.0/index.html"
10+
},
11+
{
12+
description: "Current version site root with trailing /",
813
requestUri: "/site/",
914
expectedOriginUri: "/v1.0.0/index.html"
1015
},
1116
{
1217
description: "Current version site nested page",
18+
requestUri: "/site/page",
19+
expectedOriginUri: "/v1.0.0/index.html"
20+
},
21+
{
22+
description: "Current version site nested page with trailing /",
1323
requestUri: "/site/page/",
1424
expectedOriginUri: "/v1.0.0/index.html"
1525
},
1626
{
1727
description: "Current version site deeply nested page",
28+
requestUri: "/site/area1/area2/page",
29+
expectedOriginUri: "/v1.0.0/index.html"
30+
},
31+
{
32+
description: "Current version site deeply nested page with trailing /",
1833
requestUri: "/site/area1/area2/page/",
1934
expectedOriginUri: "/v1.0.0/index.html"
2035
},
2136
{
2237
description: "Specified version site root",
38+
requestUri: "/site/v0.9.9",
39+
expectedOriginUri: "/v0.9.9/index.html"
40+
},
41+
{
42+
description: "Specified version site root with trailing /",
2343
requestUri: "/site/v0.9.9/",
2444
expectedOriginUri: "/v0.9.9/index.html"
2545
},
2646
{
2747
description: "Specified version site nested page",
48+
requestUri: "/site/v0.9.9/page",
49+
expectedOriginUri: "/v0.9.9/index.html"
50+
},
51+
{
52+
description: "Specified version site nested page with trailing /",
2853
requestUri: "/site/v0.9.9/page/",
2954
expectedOriginUri: "/v0.9.9/index.html"
3055
},
3156
{
3257
description: "Specified version site deeply nested page",
58+
requestUri: "/site/v0.9.9/area1/area2/page",
59+
expectedOriginUri: "/v0.9.9/index.html"
60+
},
61+
{
62+
description: "Specified version site deeply nested page with trailing /",
3363
requestUri: "/site/v0.9.9/area1/area2/page/",
3464
expectedOriginUri: "/v0.9.9/index.html"
3565
},
3666
{
3767
description: "Specified PR site root",
68+
requestUri: "/site/pr-1234",
69+
expectedOriginUri: "/pr-1234/index.html"
70+
},
71+
{
72+
description: "Specified PR site root with trailing /",
3873
requestUri: "/site/pr-1234/",
3974
expectedOriginUri: "/pr-1234/index.html"
4075
},
4176
{
4277
description: "Specified PR site nested page",
78+
requestUri: "/site/pr-1234/page",
79+
expectedOriginUri: "/pr-1234/index.html"
80+
},
81+
{
82+
description: "Specified PR site nested page with trailing /",
4383
requestUri: "/site/pr-1234/page/",
4484
expectedOriginUri: "/pr-1234/index.html"
4585
},
4686
{
4787
description: "Specified PR site deeply nested page",
88+
requestUri: "/site/pr-1234/area1/area2/page",
89+
expectedOriginUri: "/pr-1234/index.html"
90+
},
91+
{
92+
description: "Specified PR site deeply nested page with trailing /",
4893
requestUri: "/site/pr-1234/area1/area2/page/",
4994
expectedOriginUri: "/pr-1234/index.html"
5095
},
@@ -77,6 +122,21 @@ describe("S3 Content URI Rewrite", () => {
77122
description: "Specified PR nested static file",
78123
requestUri: "/site/pr-1234/files/file.ext",
79124
expectedOriginUri: "/pr-1234/files/file.ext"
125+
},
126+
{
127+
description: "Malformed uri",
128+
requestUri: "/sitepage",
129+
expectedOriginUri: "/404.html"
130+
},
131+
{
132+
description: "Malformed version",
133+
requestUri: "/site/v0.9.9page",
134+
expectedOriginUri: "/404.html"
135+
},
136+
{
137+
description: "Malformed pr",
138+
requestUri: "/site/pr-1234page",
139+
expectedOriginUri: "/404.html"
80140
}
81141
]
82142

0 commit comments

Comments
 (0)