Route requests to an origin closer to the viewer in a CloudFront Functions viewer request event
The following code example shows how to route requests to an origin closer to the viewer in a CloudFront Functions viewer request event.
- JavaScript
-
- JavaScript runtime 2.0 for CloudFront Functions
-
import cf from 'cloudfront';
function handler(event) {
const request = event.request;
const headers = request.headers;
const country = headers['cloudfront-viewer-country'] &&
headers['cloudfront-viewer-country'].value;
//List of Regions with S3 buckets containing content
const countryToRegion = {
'DE': 'eu-central-1',
'IE': 'eu-west-1',
'GB': 'eu-west-2',
'FR': 'eu-west-3',
'JP': 'ap-northeast-1',
'IN': 'ap-south-1'
};
const DEFAULT_REGION = 'us-east-1';
const selectedRegion = (country && countryToRegion[country]) || DEFAULT_REGION;
const domainName =
`cloudfront-functions-demo-bucket-in-${selectedRegion}.s3.${selectedRegion}.amazonaws.com`;
cf.updateRequestOrigin({
"domainName": domainName,
"originAccessControlConfig": {
"enabled": true,
"region": selectedRegion,
"signingBehavior": "always",
"signingProtocol": "sigv4",
"originType": "s3"
},
});
return request;
}
For a complete list of AWS SDK developer guides and code examples, see
Using CloudFront with an AWS SDK.
This topic also includes information about getting started and details about previous SDK versions.