Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions packages/geoip-redirect/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
# 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
![TypeScript version](https://img.shields.io/github/package-json/dependency-version/aligent/cdk-constructs/dev/typescript?filename=packages/geoip-redirect/package.json&color=red) ![AWS CDK version](https://img.shields.io/github/package-json/dependency-version/aligent/cdk-constructs/dev/aws-cdk?filename=packages/geoip-redirect/package.json) ![NPM version](https://img.shields.io/npm/v/%40aligent%2Fcdk-geoip-redirect?color=green)

## Overview

This library provides a construct which creates a Lambda@Edge function to perform GeoIP redirects. The Aligent Esbuild CDK construct is used to bundle the handler function from `handler/redirect.ts`.

These functions are intended to be added to an existing Cloudfront distribution. When the Lambda@Edge function is triggered, the function is passed the CloudFrontRequestEvent.

The Lambda@Edge function will check if the viewer's country code matches any supported regions. The country code from each request is pulled from the `cloudfront-viewer-country` header:
- if they match, they are redirected to `${redirectURL}${countryCode.toLowerCase()}${request.uri}`
- if they do not match, they are redirected to `${redirectURL}${DEFAULT_REGION.toLowerCase()}${request.uri}`

The `DEFAULT_REGION` is set as an option in the Lambda@Edge handler code

## Diagram

![geoip lambda@edge diagram](docs/geoip-redirect.drawio.png)

## Usage and Default Geo-IP Redirect options
### `redirectHost` (string)
- Base hostname used for redirects

### `supportedRegionsExpression` (string)
- Case-sensitive regular expression matching cloudfront-viewer-country

### `defaultRegion` (string)
- Fallback region code when viewer's country does not match the supported pattern

## Example code
When defining the Lambda@Edge function, this construct serves one default host to redirect too:
```
define: {
"process.env.REDIRECT_HOST": options.redirectHost,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor point but the formatting looks a bit funky here

"process.env.SUPPORTED_REGIONS":
options.supportedRegionsExpression,
"process.env.DEFAULT_REGION": options.defaultRegion,
},
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading