Amazon Cloudfront supports lambda functions.
- Sign in to the AWS Management Console and open the AWS Lambda console
- Create a new lambda function
- Choose 'Edge Nodge.js 4.3' for the language
- Choose the cloudfront-modify-response-header template.
- Specify your CloudFront distribution and event to apply the function to.
Example code for a Tk header field for a resource that claims not to be tracking:
'use strict';
exports.handler = (event, context, callback) => {
const response = event.Records[0].cf.response;
response.headers['Tk'] = 'N';
callback(null, response);
};
Example code for a Tk header field for a resource that claims to be tracking with consent:
'use strict';
exports.handler = (event, context, callback) => {
const response = event.Records[0].cf.response;
response.headers['Tk'] = 'C';
callback(null, response);
};
Example code for conditional Tracking Status Value (TSV):
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const response = event.Records[0].cf.response;
if (request.headers['DNT'] = '0') {
response.headers['Tk'] = 'T';
} else {
response.headers['Tk'] = 'N';
}
callback(null, response);
};
PS: the answer is loosely based on (1) HSTS on Amazon CloudFront from S3 origin, https://serverfault.com/questions/820939/hsts-on-amazon-cloudfront-from-s3-origin and (2) AWS Lambda@Edge (Preview) http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html. See also https://www.w3.org/TR/tracking-dnt/#tracking-status-value.