Skip to content

Commit

Permalink
DO-1535: upgrade geoip redirect construct
Browse files Browse the repository at this point in the history
  • Loading branch information
gowrizrh committed Sep 27, 2023
1 parent ff3c5a3 commit 80cb9d5
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 0 deletions.
23 changes: 23 additions & 0 deletions package-lock.json

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

58 changes: 58 additions & 0 deletions packages/geoip-redirect/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# These are some examples of commonly ignored file patterns.
# You should customize this list as applicable to your project.
# Learn more about .gitignore:
# https://www.atlassian.com/git/tutorials/saving-changes/gitignore

# Node artifact files
node_modules/
dist/

# Compiled Java class files
*.class

# Compiled Python bytecode
*.py[cod]

# Log files
*.log

# Package files
*.jar

# Maven
target/
dist/

# JetBrains IDE
.idea/

# Unit test reports
TEST*.xml

# Generated by MacOS
.DS_Store

# Generated by Windows
Thumbs.db

# Applications
*.app
*.exe
*.war

# Large media files
*.mp4
*.tiff
*.avi
*.flv
*.mov
*.wmv

!jest.config.js

# CDK asset staging directory
.cdk.staging
cdk.out

*.d.ts
*.js
11 changes: 11 additions & 0 deletions packages/geoip-redirect/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.ts
!lib/handlers/*.ts
!*.d.ts
!*.js

# CDK asset staging directory
.cdk.staging
cdk.out

# Samples
sample/
4 changes: 4 additions & 0 deletions packages/geoip-redirect/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Geo-IP Redirect
This library provides a construct which creates a Lambda@Edge functions to perform GeoIP redirects.

These functions are intended to be added to an existing Cloudfront distribution
3 changes: 3 additions & 0 deletions packages/geoip-redirect/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { RedirectFunction } from "./lib/redirect-construct";

export { RedirectFunction };
43 changes: 43 additions & 0 deletions packages/geoip-redirect/lib/handlers/redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import "source-map-support/register";
import {
CloudFrontRequestEvent,
CloudFrontResponse,
CloudFrontRequest,
} from "aws-lambda";

const REDIRECT_HOST = process.env.REDIRECT_HOST;
const SUPPORTED_REGIONS = new RegExp(process.env.SUPPORTED_REGIONS);
const DEFAULT_REGION = process.env.DEFAULT_REGION;

export const handler = async (
event: CloudFrontRequestEvent
): Promise<CloudFrontResponse | CloudFrontRequest> => {
const request = event.Records[0].cf.request;

let redirectURL = `https://${REDIRECT_HOST}/`;
if (request.headers["cloudfront-viewer-country"]) {
const countryCode = request.headers["cloudfront-viewer-country"][0].value;
if (SUPPORTED_REGIONS.test(countryCode)) {
redirectURL = `${redirectURL}${countryCode.toLowerCase()}${request.uri}`;
} else {
redirectURL = `${redirectURL}${DEFAULT_REGION.toLowerCase()}${
request.uri
}`;
}

return {
status: "302",
statusDescription: "Found",
headers: {
location: [
{
key: "Location",
value: redirectURL,
},
],
},
};
}

return request;
};
57 changes: 57 additions & 0 deletions packages/geoip-redirect/lib/redirect-construct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { AssetHashType, DockerImage } from "aws-cdk-lib";
import { EdgeFunction } from "aws-cdk-lib/aws-cloudfront/lib/experimental";
import { Code, IVersion, Runtime, Version } from "aws-cdk-lib/aws-lambda";
import { Construct } from "constructs";
import { join } from "path";
import { Esbuild } from "@aligent/esbuild";

export interface RedirectFunctionOptions {
redirectHost: string;
// Case-sensitive regular expression matching cloudfront-viewer-country
supportedRegionsExpression: string;
// default region code to use when not matched
defaultRegion: string;
}

export class RedirectFunction extends Construct {
readonly edgeFunction: EdgeFunction;

constructor(scope: Construct, id: string, options: RedirectFunctionOptions) {
super(scope, id);

const command = [
"sh",
"-c",
'echo "Docker build not supported. Please install esbuild."',
];

this.edgeFunction = new EdgeFunction(this, `${id}-redirect-fn`, {
code: Code.fromAsset(join(__dirname, "handlers"), {
assetHashType: AssetHashType.OUTPUT,
bundling: {
command,
image: DockerImage.fromRegistry("busybox"),
local: new Esbuild({
entryPoints: [join(__dirname, "handlers/redirect.ts")],
define: {
"process.env.REDIRECT_HOST": options.redirectHost,
"process.env.SUPPORTED_REGIONS":
options.supportedRegionsExpression,
"process.env.DEFAULT_REGION": options.defaultRegion,
},
}),
},
}),
runtime: Runtime.NODEJS_18_X,
handler: "redirect.handler",
});
}

public getFunctionVersion(): IVersion {
return Version.fromVersionArn(
this,
"redirect-fn-version",
this.edgeFunction.currentVersion.edgeArn
);
}
}
34 changes: 34 additions & 0 deletions packages/geoip-redirect/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@aligent/cdk-geoip-redirect",
"version": "0.1.0",
"description": "A Cloudfront Lambda@Edge stack for performing redirection based on CloudFront-Viewer-Country",
"main": "index.js",
"scripts": {
"build": "tsc && cd ./lib/handlers && npm ci",
"prepublish": "tsc && cd ./lib/handlers && npm ci"
},
"repository": {
"type": "git",
"url": "git+https://github.com/aligent/aws-cdk-prerender-proxy-stack.git"
},
"license": "GPL-3.0-only",
"bugs": {
"url": "https://github.com/aligent/aws-cdk-prerender-proxy-stack/issues"
},
"homepage": "https://github.com/aligent/aws-cdk-prerender-proxy-stack#readme",
"devDependencies": {
"@types/jest": "^29.5.5",
"@types/node": "20.6.3",
"aws-cdk": "2.97.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "~5.2.2"
},
"dependencies": {
"@types/aws-lambda": "^8.10.122",
"aws-cdk-lib": "2.97.0",
"constructs": "^10.0.0",
"source-map-support": "^0.5.21"
}
}
3 changes: 3 additions & 0 deletions packages/geoip-redirect/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.json"
}

0 comments on commit 80cb9d5

Please sign in to comment.