Created
October 6, 2016 05:15
-
-
Save hideokamoto/06896d1ac6033767933a900f9c5e6810 to your computer and use it in GitHub Desktop.
CloudFrontの設定を一括でアップデートするJavaScriptを書いてみた ref: http://qiita.com/motchi0214/items/872ef8c67587330dc0bb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS = require('aws-sdk'); | |
var conf = require('./config.js'); | |
var cloudfront = new AWS.CloudFront({ | |
apiVersion: '2016-09-07', | |
accessKeyId: AWS_ACCESS_KEY, | |
secretAccessKey: AWS_SECRET_KEY, | |
}); | |
function updateCloudFrontDistribution( distribution ) { | |
var params = { | |
Id: distribution.Id | |
}; | |
cloudfront.getDistributionConfig(params).promise() | |
.then( function(data){ | |
// Write CloudFront Distribution Setting | |
data.DistributionConfig.HttpVersion = 'http2'; | |
// Do update distribution setting | |
doUpdateCloudFront( data, distribution ); | |
}).catch( function(err) { | |
console.log(err); | |
}); | |
} | |
function updateCloudFrontDistributions( Marker ) { | |
if ( Marker === undefined ) { | |
console.log('end'); | |
return; | |
} | |
console.log('start to ' + Marker); | |
var params = { | |
Marker: Marker, | |
MaxItems: '3' | |
}; | |
cloudfront.listDistributions(params).promise() | |
.then( function(data) { | |
i = 0; | |
while(i < data.DistributionList.Items.length) { | |
Marker = data.DistributionList.Items[i].Id; | |
updateCloudFrontDistribution( data.DistributionList.Items[i] ); | |
i=(i+1)|0; | |
} | |
console.log(i); | |
if ( i !== 0 ){ | |
return Marker; | |
} | |
}).then( function(Marker){ | |
setTimeout(function(){ | |
console.log('Next start is ' + Marker); | |
updateCloudFrontDistributions( Marker ); | |
}, 10000); | |
}).catch( function(err) { | |
console.log(err); | |
}); | |
} | |
function doUpdateCloudFront( data, distribution ) { | |
var params = { | |
Id: distribution.Id, | |
IfMatch: data.ETag, | |
DistributionConfig: data.DistributionConfig | |
}; | |
cloudfront.updateDistribution(params).promise() | |
.then( function(data){ | |
console.log(data); | |
}).catch( function(err) { | |
console.log(err); | |
}); | |
} | |
updateCloudFrontDistributions( '' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment