Skip to content

Commit

Permalink
Merge pull request #1297 from aligent/feature/DO-1607-feature-environ…
Browse files Browse the repository at this point in the history
…ments

Feature/do 1607 feature environments
  • Loading branch information
TheOrangePuff authored May 10, 2024
2 parents b61f391 + 00a889d commit 1dbe700
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Construct | Description
-- | --
[basic-auth](packages/basic-auth) |
[cloudfront-security-headers](packages/cloudfront-security-headers) |
[feature-env-handlers](packages/feature-env-handlers) | Lambda@Edge handlers to support feature environments
[geoip-redirect](packages/geoip-redirect) |
[graphql-server](packages/graphql-server) |
[lambda-at-edge-handlers](packages/lambda-at-edge-handlers) |
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/feature-env-handlers/README.md
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.
4 changes: 4 additions & 0 deletions packages/feature-env-handlers/index.ts
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 };
26 changes: 26 additions & 0 deletions packages/feature-env-handlers/lib/origin-request.ts
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;
};
17 changes: 17 additions & 0 deletions packages/feature-env-handlers/lib/viewer-request.ts
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;
};
85 changes: 85 additions & 0 deletions packages/feature-env-handlers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions packages/feature-env-handlers/package.json
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"
}
}
3 changes: 3 additions & 0 deletions packages/feature-env-handlers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.json"
}

0 comments on commit 1dbe700

Please sign in to comment.