Skip to content

Commit 0cd43d3

Browse files
authored
Merge pull request #51 from aws-samples/origin-selection
Adding origin selection using CloudFront Functions example
2 parents 640e3d5 + c662e10 commit 0cd43d3

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

.doc_gen/metadata/cloudfront-functions_metadata.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,20 @@ cloudfront_functions_url_rewrite_single_page_apps:
185185
- url-rewrite-single-page-apps/index.js
186186
services:
187187
cloudfront:
188+
cloudfront_functions_select_origin_based_on_country:
189+
title: Route requests to an origin closer to the viewer in a &CF; Functions viewer request event
190+
title_abbrev: Select origin closer to the viewer
191+
synopsis: route requests to an origin closer to the viewer in a &CF; Functions viewer request event.
188192

193+
category: CloudFront Functions examples
194+
languages:
195+
JavaScript:
196+
versions:
197+
- sdk_version: 102
198+
github: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/select-origin-based-on-country
199+
excerpts:
200+
- description:
201+
snippet_files:
202+
- select-origin-based-on-country/index.js
203+
services:
204+
cloudfront:

amazon-cloudfront-functions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 640e3d5cd9a587c3fbfd29138533f998547abe7c
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Route requests to an origin closer to the viewer’s country
2+
3+
**CloudFront Functions event type: viewer request**
4+
5+
This function makes use of the `Cloudfront-Viewer-Country` [geolocation header](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-cloudfront-headers.html#cloudfront-headers-viewer-location) which performs a lookup on the request to determine the user's country and includes that value in the `Cloudfront-Viewer-Country` request header. For the geolocation or device detection headers to appear in the request object within a function, you must allow these headers (or allow all viewer headers) in a CloudFront [origin request policy](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/controlling-origin-requests.html#origin-request-understand-origin-request-policy) or [cache policy](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/controlling-the-cache-key.html#cache-key-understand-cache-policy).
6+
7+
This function assumes content is replicated to five geographically dispersed Amazon S3 buckets. Based on the `Cloudfront-Viewer-Country` header, this function modifies the origin of the request to the geographically closest Amazon S3 bucket to the user. Each S3 bucket is private and protected using CloudFront's [origin access control](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html). If the requested content is not found in the CloudFront cache, CloudFront will fetch the content from the S3 bucket closest to the viewer.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import cf from 'cloudfront';
2+
3+
function handler(event) {
4+
const request = event.request;
5+
const headers = request.headers;
6+
const country = headers['cloudfront-viewer-country'] &&
7+
headers['cloudfront-viewer-country'].value;
8+
9+
//List of Regions with S3 buckets containing content
10+
const countryToRegion = {
11+
'DE': 'eu-central-1',
12+
'IE': 'eu-west-1',
13+
'GB': 'eu-west-2',
14+
'FR': 'eu-west-3',
15+
'JP': 'ap-northeast-1',
16+
'IN': 'ap-south-1'
17+
};
18+
19+
const DEFAULT_REGION = 'us-east-1';
20+
21+
const selectedRegion = (country && countryToRegion[country]) || DEFAULT_REGION;
22+
23+
const domainName =
24+
`cloudfront-functions-demo-bucket-in-${selectedRegion}.s3.${selectedRegion}.amazonaws.com`;
25+
26+
cf.updateRequestOrigin({
27+
"domainName": domainName,
28+
"originAccessControlConfig": {
29+
"enabled": true,
30+
"region": selectedRegion,
31+
"signingBehavior": "always",
32+
"signingProtocol": "sigv4",
33+
"originType": "s3"
34+
},
35+
});
36+
37+
return request;
38+
}

0 commit comments

Comments
 (0)