Here is how you can do this programmatically. This should be a part of your deploy script. We will only invalidate index.html
since we are already versioning the other resources via their filenames:
const aws = require('aws-sdk')
function invalidateIndex () {
const client = new aws.CloudFront({
accessKeyId: process.env.AWS_ACCESS_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
})
const invalidation = client.createInvalidation({
DistributionId: process.env.AWS_CLOUDFRONT_DISTRIBUTION_ID, /* required */
InvalidationBatch: {
/* required */
CallerReference: Date.now() + '', /* required - request ID given by you, any string is okay*/
Paths: {
/* required */
Quantity: 1, /* required */
Items: [
'/',
/* more items */
]
}
}
}, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log('Index was invalidated with invalidation id: ', data.Invalidation.Id); // successful response
})
}
invalidateIndex()
You can read more in the API documentation here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFront.html#createInvalidation-property