CloudFront functions are lightweight JavaScript functions that can be used to manipulate HTTP requests between your CloudFront distributions and your clients. It's as simple as that!
One use case is that when you're using S3 buckets and CloudFront to host your static website, you have the power to tailor and manage the requests and responses that traverse CloudFront. For instance, you can easily add additional HTTP headers like the Cache-Control header.
It's straightforward: create a CloudFront function to add to the Cache-Control HTTP header.
setCacheControl
.
cloudfront-js-2.0
.
function handler(event) {
const response = event.response;
const headers = response.headers;
// Set the cache-control header
headers['cache-control'] = {value: 'public,
max-age=31536000'};
// Return response to viewers
return response;
}
/images