-
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.
DO-1535: upgrade geoip redirect construct
- Loading branch information
Showing
9 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
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,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 |
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 @@ | ||
*.ts | ||
!lib/handlers/*.ts | ||
!*.d.ts | ||
!*.js | ||
|
||
# CDK asset staging directory | ||
.cdk.staging | ||
cdk.out | ||
|
||
# Samples | ||
sample/ |
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 @@ | ||
# 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 |
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 @@ | ||
import { RedirectFunction } from "./lib/redirect-construct"; | ||
|
||
export { RedirectFunction }; |
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,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; | ||
}; |
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,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 | ||
); | ||
} | ||
} |
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,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" | ||
} | ||
} |
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" | ||
} |