Skip to content

Commit

Permalink
cloudfront invalidations
Browse files Browse the repository at this point in the history
  • Loading branch information
jrit committed Oct 20, 2014
1 parent 4dfeca2 commit a9e8841
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
11 changes: 11 additions & 0 deletions example/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ module.exports = function(grunt) {
cwd: "build",
src: "**"
}
},

cloudfront: {
options: {
accessKeyId: "<%= aws.accessKeyId %>",
secretAccessKey: "<%= aws.secretAccessKey %>",
distributionId: "...",
invalidations: [
"/index.html"
]
}
}
});

Expand Down
62 changes: 62 additions & 0 deletions tasks/services/cloudfront.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var AWS = require("aws-sdk"),
_ = require("lodash"),
async = require("async");

module.exports = function(grunt) {

//cloudfront description
var DESC = "grunt-aws's cloudfront";

//cloudfront defaults (none at the moment)
var DEFAULTS = {};

//cloudfront task
grunt.registerTask("cloudfront", DESC, function() {

//get options
var opts = this.options(DEFAULTS);

if(_.isEmpty(opts.distributionId))
return grunt.log.ok("No DistributionId specified");

if(_.isEmpty(opts.invalidations))
return grunt.log.ok("No invalidations specified");

//mark as async
var done = this.async();

//whitelist allowed keys
AWS.config.update(_.pick(opts,
'accessKeyId',
'secretAccessKey'
), true);

//cloudfront client
var cloudfront = new AWS.CloudFront();

//create records defined in opts.invalidations
createInvalidations(done);

//------------------------------------------------

function createInvalidations(callback) {
var params = {
DistributionId: opts.distributionId,
InvalidationBatch: {
CallerReference: Date.now().toString(),
Paths: {
Quantity: opts.invalidations.length,
Items: opts.invalidations
}
}
};
cloudfront.createInvalidation(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
callback(err);
});
}
});


};

0 comments on commit a9e8841

Please sign in to comment.