我已经在Nodejs中为Lambda编写了以下代码,并获得了一个输出,该输出显示了所有区域中按节点和非节点运行时过滤的lambda函数(如预期的那样)。
但是,不知何故,代码看起来很笨重,有很多函数调用到不同的区域。可以缩短代码或者以某种方式减少执行时间吗?
var AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
var boolValue = true;
console.log("Only regions with Lambda Functions are being shown");
var callBackCount;
callBackCount = 0;
response = {
Lambda: []
};
usWest_1();
usWest_2();
usEast_1();
euWest_1();
euCentral_1();
apSouthEast_1();
apSouthEast_2();
apNorthEast_1();
apNorthEast_2();
// functions for Clients of Each Region
function usWest_1() {
var lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region: 'us-west-1'
});
var region = "us-west-1";
///// function called getFunctions//////
getFunctions(lambda, region);
}
function usWest_2() {
var lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region: 'us-west-2'
});
var region = "us-west-2";
///// function called getFunctions//////
getFunctions(lambda, region);
}
function usEast_1() {
var lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region: 'us-east-1'
});
var region = "us-east-1";
///// function called getFunctions//////
getFunctions(lambda, region);
}
function euWest_1() {
var lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region: 'eu-west-1'
});
var region = "eu-west-1";
///// function called getFunctions//////
getFunctions(lambda, region);
}
function euCentral_1() {
var lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region: 'eu-central-1'
});
var region = "eu-central-1";
///// function called getFunctions//////
getFunctions(lambda, region);
}
function apSouthEast_1() {
var lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region: 'ap-southeast-1'
});
var region = "ap-southeast-1";
///// function called getFunctions//////
getFunctions(lambda, region);
}
function apSouthEast_2() {
var lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region: 'ap-southeast-2'
});
var region = "ap-southeast-2";
///// function called getFunctions//////
getFunctions(lambda, region);
}
function apNorthEast_1() {
var lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region: 'ap-northeast-1'
});
var region = "ap-northeast-1";
///// function called getFunctions//////
getFunctions(lambda, region);
}
function apNorthEast_2() {
var lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region: 'ap-northeast-2'
});
var region = "ap-northeast-2";
///// function called getFunctions//////
getFunctions(lambda, region);
}
function getFunctions(lambda, region) {
var regionName = region;
var callbackData = {};
var params = {};
lambda.listFunctions(params, function(err, data) {
if (err) {
console.log(err, err.stack);
} // an error occurred
else {
callbackData = JSON.stringify(data);
} // successful response
getNodeFunctions(callbackData, regionName);
});
}
function getNodeFunctions(callbackData, regionName) {
var allFunctions = {
region: "",
nodeFunctions: [],
noneNodeFunctions: []
};
var paramData = (JSON.parse(callbackData));
if (paramData.Functions[0] === undefined) {
boolValue = false; // if no function in this region
viewData(allFunctions, boolValue, regionName);
} else {
boolValue = true;
paramData.Functions.forEach(function(functs) {
if (functs.Runtime.startsWith("node")) {
var names = {
"FunctionName": '',
"Runtime": '',
"FunctionArn": '',
"LastModified": ''
};
names.FunctionName = (functs.FunctionName);
names.Runtime = (functs.Runtime);
names.FunctionArn = (functs.FunctionArn);
names.LastModified = (functs.LastModified);
allFunctions.nodeFunctions.push(names);
} else {
var noneNode = {
"FunctionName": '',
"Runtime": '',
"FunctionArn": '',
"LastModified": ''
};
noneNode.FunctionName = (functs.FunctionName);
noneNode.Runtime = (functs.Runtime);
noneNode.FunctionArn = (functs.FunctionArn);
noneNode.LastModified = (functs.LastModified);
allFunctions.noneNodeFunctions.push(noneNode);
}
});
}
viewData(allFunctions, boolValue, regionName);
}
function viewData(allFunctions, boolValue, regionName) {
callBackCount++;
var testVal = boolValue;
var thisRegion = regionName;
var viewAllFunctions = allFunctions;
viewAllFunctions.region = thisRegion;
if (testVal === true) {
response.Lambda.push(viewAllFunctions);
} else {
// regions without lambda function
}
if (callBackCount == 9 && response.Lambda[0] === undefined) {
//console.log("weird");
} else if (callBackCount == 9 && response.Lambda[0] !== undefined) {
console.log(JSON.stringify(response));
callback(null, response);
console.log(JSON.stringify(event));
} else {
//console.log("something else");
}
}
};
更新1
根据John的建议,我将区域添加到数组中。它现在看起来更干净。但它仍然包含几个可以消除的循环。例如:要返回回调,检查条件是否所有区域都被执行,如果回调保持在循环外,它将返回null响应,因为异步Node将首先执行它。 Promise可用于克服这种情况,但仍然不确定如何在循环嵌套时使用promises并且执行时间是一个问题。此外,除了承诺之外,还有其他方法可以在不增加执行成本的情况下完成吗?
var AWS = require('aws-sdk');
exports.handler = (event, context, callback) =>
{
var boolValue = true;
console.log("Only regions with Lambda Functions are being shown");
var callBackCount;
callBackCount = 0;
response = {
Lambda: []
};
var regionNames = ['us-west-1', 'us-west-2', 'us-east-1', 'eu-west-1', 'eu-central-1', 'ap-southeast-1',
'ap-southeast-2', 'ap-northeast-1', 'ap-northeast-2'
];
regionNames.forEach(function(region)
{
getFunctions(region);
});
function getFunctions(region)
{
var regionName = region;
var info = {
apiVersion: '2015-03-31',
region: ''
};
info.region = regionName;
var lambda = new AWS.Lambda(info);
var callbackData = {};
var params = {};
lambda.listFunctions(params, function(err, data)
{
if (err)
{
console.log(err, err.stack);
} // an error occurred
else
{
callbackData = JSON.stringify(data);
} // successful response
getNodeFunctions(callbackData, regionName);
});
}
function getNodeFunctions(callbackData, regionName)
{
var allFunctions = {
region: "",
nodeFunctions: [],
noneNodeFunctions: []
};
var paramData = (JSON.parse(callbackData));
if (paramData.Functions[0] === undefined)
{
boolValue = false;
viewData(allFunctions, boolValue, regionName);
}
else
{
boolValue = true;
paramData.Functions.forEach(function(functs)
{
if (functs.Runtime.startsWith("node"))
{
var names = {
"FunctionName": '',
"Runtime": '',
"FunctionArn": '',
"LastModified": ''
};
names.FunctionName = (functs.FunctionName);
names.Runtime = (functs.Runtime);
names.FunctionArn = (functs.FunctionArn);
names.LastModified = (functs.LastModified);
allFunctions.nodeFunctions.push(names);
}
else
{
var noneNode = {
"FunctionName": '',
"Runtime": '',
"FunctionArn": '',
"LastModified": ''
};
noneNode.FunctionName = (functs.FunctionName);
noneNode.Runtime = (functs.Runtime);
noneNode.FunctionArn = (functs.FunctionArn);
noneNode.LastModified = (functs.LastModified);
allFunctions.noneNodeFunctions.push(noneNode);
}
});
}
viewData(allFunctions, boolValue, regionName);
}
function viewData(allFunctions, boolValue, regionName)
{
callBackCount++;
var testVal = boolValue;
var thisRegion = regionName;
var viewAllFunctions = allFunctions;
viewAllFunctions.region = thisRegion;
if (testVal === true)
{
response.Lambda.push(viewAllFunctions);
}
else
{
// regions without lambda function
}
if (callBackCount == 9 && response.Lambda[0] === undefined)
{
//console.log("weird");
}
else if (callBackCount == 9 && response.Lambda[0] !== undefined)
{
console.log(JSON.stringify(response));
callback(null, response);
console.log(JSON.stringify(event));
}
else
{
//console.log("something else");
}
}
};