Function Filter

Using the function filter, for complex filtering

This workflow is used to check to if the Instance is idle, and then terminate them:

Given below is a function filter that is being used for filtering out EC2 Instances that are idle:

function(obj){let isCpuData = false;
let cpuUtilization = obj.addOns &&
obj.addOns.MetricData &&
obj.addOns.MetricData.data &&
obj.addOns.MetricData.data.filter(x => x.Label === "CPUUtilization")[0];
if(cpuUtilization) {
isCpuData = cpuUtilization.Values.length !== 0;
cpuUtilization = cpuUtilization.Values.reduce((A,e) => A+e,0)/(cpuUtilization.Values.length)
}
let isNetworkInData = false;
let networkIn = obj.addOns &&
obj.addOns.MetricData &&
obj.addOns.MetricData.data &&
obj.addOns.MetricData.data.filter(x => x.Label === "NetworkIn")[0];
if(networkIn) {
isNetworkInData = networkIn.Values.length !== 0;
networkIn = networkIn.Values.reduce((A,e) => A+e,0)/(networkIn.Values.length)
}
let isNetworkOutData = false;
let networkOut = obj.addOns &&
obj.addOns.MetricData &&
obj.addOns.MetricData.data &&
obj.addOns.MetricData.data.filter(x => x.Label === "NetworkOut")[0];
if(networkOut) {
isNetworkOutData = networkOut.Values.length !== 0;
networkOut = networkOut.Values.reduce((A,e) => A+e,0)/(networkOut.Values.length)
}
if(!isCpuData || !isNetworkInData || ! isNetworkOutData) return false;
return cpuUtilization <= 2 && (networkIn + networkOut)/(1024*1024) <= 5;}

  • The function filter is working on the monitoring data of CPU Utilisation, Network In or Network Out data

  • Based on this, it will compare the data points with the threshold defined, pass through the Instances being used with the metric data below the threshold value