Skip to content

Instantly share code, notes, and snippets.

@uhop
Last active October 19, 2020 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uhop/fb79b00d9cb12a9d451b9f18f6ffbb90 to your computer and use it in GitHub Desktop.
Save uhop/fb79b00d9cb12a9d451b9f18f6ffbb90 to your computer and use it in GitHub Desktop.
Useful scripts to deal with AWS CloudFront distributions by the associated domain names and invalidations.
'use strict';
const iterate = async function*(client) {
const params = {MaxItems: '100'};
for (;;) {
const data = await client.listDistributions(params).promise(),
list = data.DistributionList,
items = list.Items;
if (items) {
yield items;
}
if (!list.IsTruncated) break;
params.Marker = list.NextMarker;
}
};
exports.iterate = iterate;
const iterateByItem = async function*(client) {
for await (const items of iterate(client)) {
yield* items;
}
};
exports.iterateByItem = iterateByItem;
const getByDomain = async (client, fn) => {
if (typeof fn == 'string') {
// exact string
return getByDomain(client, alias => alias === fn);
}
if (typeof fn == 'object' && fn && typeof fn.test == 'function') {
// pattern
return getByDomain(client, alias => fn.test(alias));
}
if (typeof fn != 'function') {
console.error('the argument should be either a string/regular expression object/function');
return;
}
for await (const item of iterateByItem(client)) {
for (const alias of item.Aliases.Items) {
if (fn(alias)) {
return item;
}
}
}
};
exports.getByDomain = getByDomain;
'use strict';
const AWS = require('./getAWS');
const {getByDomain} = require('./distributions');
const client = new AWS.CloudFront();
if (process.argv.length < 2) {
console.log('Use: node invalidate.js --domain domainName [--wait]');
process.exit(1);
}
let domain;
const domainIndex = process.argv.indexOf('--domain');
if (domainIndex > 0 && domainIndex + 1 < process.argv.length) {
domain = process.argv[domainIndex + 1];
}
if (!domain) {
console.log('--domain should be specified. Run the utility without parameters for help.');
process.exit(1);
}
const main = async (client, name, wait) => {
const distribution = await getByDomain(client, name);
if (!distribution) {
throw Error('There is no distribution for: ' + name);
}
console.log('Invalidating', distribution.Id, name);
const data = await client
.createInvalidation({
DistributionId: distribution.Id,
InvalidationBatch: {
CallerReference: Date.now().toString(),
Paths: {
Quantity: 1,
Items: ['/*']
}
}
})
.promise();
console.log('Invalidation:', data.Invalidation.Id);
if (wait) {
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
console.log(`Checking ${name}...`);
client
.getInvalidation({DistributionId: distribution.Id, Id: data.Invalidation.Id})
.promise()
.then(data => {
if (data.Status === 'Completed') {
console.log(`...${name} is completed.`);
clearInterval(interval);
resolve(data);
}
})
.catch(error => {
clearInterval(interval);
reject(error);
});
}, 5000);
});
}
};
main(
client,
domain,
process.argv.some(arg => arg === '--wait')
).then(
() => console.log('Done.'),
error => console.error('ERROR:', error)
);
'use strict';
const AWS = require('./getAWS');
const {iterateByItem} = require('./distributions');
const client = new AWS.CloudFront();
const main = async client => {
let total = 0;
for await (const item of iterateByItem(client)) {
item.Aliases.Items.length && console.log(item.Id + '\t' + item.Aliases.Items.join('\t'));
++total;
}
console.log('Total:', total);
};
main(client);
@uhop
Copy link
Author

uhop commented Nov 18, 2019

The command-line utilities use getAWS.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment