-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1297 from aligent/feature/DO-1607-feature-environ…
…ments Feature/do 1607 feature environments
- Loading branch information
Showing
10 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Feature Environment Lambda@Edge Handlers | ||
|
||
A collection of Lambda@Edge functions to handle routing in feature environments. | ||
|
||
Feature environments are temporary environments that are created on a pull request. They function almost the same as regular statically hosted environments, however, there are multiple frontends hosted in the same bucket. Therefore, we require a method to route to the correct sub-path depending on which subdomain the user has visited. | ||
|
||
# Common issues | ||
|
||
Error config must be **disabled** for these functions to work. See the below diagram for the reasoning. | ||
|
||
![error config diagram](docs/error_config.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { handler as OriginRequest } from "./lib/origin-request"; | ||
import { handler as ViwerRequest } from "./lib/viewer-request"; | ||
|
||
export { OriginRequest, ViwerRequest }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { CloudFrontRequestEvent, CloudFrontRequest } from "aws-lambda"; | ||
import "source-map-support/register"; | ||
|
||
const RETURN_INDEX = /(^.\w*$)|(\/$)|(\.html$)/i; | ||
|
||
export const handler = async ( | ||
event: CloudFrontRequestEvent | ||
): Promise<CloudFrontRequest> => { | ||
const { request } = event.Records[0].cf; | ||
|
||
const host = request.headers["x-forwarded-host"][0].value; | ||
const matches = host.match(/^([^.]+)/); | ||
|
||
if (matches) { | ||
const branch = matches.pop(); | ||
if (request.origin?.s3) { | ||
request.origin.s3.path = `/${branch}`; | ||
} | ||
} | ||
|
||
if (RETURN_INDEX.test(request.uri)) { | ||
request.uri = "/index.html"; | ||
} | ||
|
||
return request; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { CloudFrontRequest, CloudFrontRequestEvent } from "aws-lambda"; | ||
import "source-map-support/register"; | ||
|
||
export const handler = async ( | ||
event: CloudFrontRequestEvent | ||
): Promise<CloudFrontRequest> => { | ||
const { request } = event.Records[0].cf; | ||
|
||
request.headers["x-forwarded-host"] = [ | ||
{ | ||
value: request.headers.host[0].value, | ||
key: "X-Forwarded-Host", | ||
}, | ||
]; | ||
|
||
return request; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@aligent/cdk-feature-env-handlers", | ||
"version": "0.1.0", | ||
"description": "Cloudfront Lambda@Edge handlers to allow feature environments to function", | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "tsc", | ||
"watch": "tsc -w", | ||
"test": "echo \"No test specified\" && exit 0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/aligent/aws-cdk-constructs.git" | ||
}, | ||
"license": "GPL-3.0-only", | ||
"bugs": { | ||
"url": "https://github.com/aligent/cdk-constructs/issues" | ||
}, | ||
"homepage": "https://github.com/aligent/cdk-constructs#readme", | ||
"devDependencies": { | ||
"@types/aws-lambda": "^8.10.122" | ||
}, | ||
"dependencies": { | ||
"source-map-support": "^0.5.21" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../tsconfig.json" | ||
} |