Skip to content

Instantly share code, notes, and snippets.

@nathanpeck
Created September 3, 2019 19:05
Show Gist options
  • Save nathanpeck/dbd1ed6467167d11de2ec9353b75ba2d to your computer and use it in GitHub Desktop.
Save nathanpeck/dbd1ed6467167d11de2ec9353b75ba2d to your computer and use it in GitHub Desktop.
import AWS = require('aws-sdk');
const ecs_sdk = new AWS.ECS();
function chunk(array: Array<any>, size: number) {
const chunked_arr = [];
for (let i = 0; i < array.length; i++) {
const last = chunked_arr[chunked_arr.length - 1];
if (!last || last.length === size) {
chunked_arr.push([array[i]]);
} else {
last.push(array[i]);
}
}
return chunked_arr;
}
export interface ServiceDesiredCount {
[details: string] : number | undefined;
}
let serviceDesiredCounts: ServiceDesiredCount;
serviceDesiredCounts = {};
async function fetchDesiredCounts() {
var nextToken = null;
let clusterArns: Array<string>;
clusterArns = [];
// Enumerate all clusters
do {
let resp = await ecs_sdk.listClusters().promise();
if (resp.clusterArns) {
clusterArns = clusterArns.concat(resp.clusterArns);
}
nextToken = resp.nextToken;
} while(nextToken)
// Enumerate all services
for (let cluster of clusterArns) {
do {
let resp = await ecs_sdk.listServices({ cluster }).promise();
if (resp.serviceArns) {
// Fetch desired count of all services in all clusters
let chunked: Array<Array<string>>
chunked = chunk(resp.serviceArns, 10);
for (let chunk of chunked) {
let resp = await ecs_sdk.describeServices({ cluster: cluster, services: chunk }).promise();
if (resp.services) {
for (let serviceDetail of resp.services) {
serviceDesiredCounts[cluster + '|' + serviceDetail.serviceName] = serviceDetail.desiredCount;
}
}
}
}
nextToken = resp.nextToken;
} while(nextToken)
}
console.log(serviceDesiredCounts);
}
function desiredCountForService(cluster: string, serviceName: string) {
return serviceDesiredCounts[cluster + '|' + serviceName];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment