如何使用节点开发工具包选择 aws lambda 函数名称?



在CLi我可以做

aws lambda list-functions

并获取所有功能详细信息

我也可以做

aws lambda list-functions --query 'Functions[*].[FunctionName]' --output text

并获取仅包含函数名称的简单列表。

如何使用开发工具包在 lambda 中执行此操作?

我试过了

exports.handler = function (event) {
const AWS = require('aws-sdk');
const lambda = new AWS.Lambda({ apiVersion: '2015-03-31' });
var lambs = lambda.listFunctions(); 
console.log(lambs);
};

并且我有 AWS lambda 完全访问权限角色

但我得到下面的输出

e,
s3DisableBodySigning: true,
computeChecksums: true,
convertResponseTypes: true,
correctClockSkew: false,
customUserAgent: null,
dynamoDbCrc32: true,
systemClockOffset: 0,
signatureVersion: 'v4',
signatureCache: true,
retryDelayOptions: {},
useAccelerateEndpoint: false,
clientSideMonitoring: false,
endpointDiscoveryEnabled: false,
endpointCacheSize: 1000,
hostPrefixEnabled: true,
stsRegionalEndpoints: null
},
isGlobalEndpoint: false,
endpoint: Endpoint {
protocol: 'https:',
host: 'lambda.us-east-2.amazonaws.com',
port: 443,
hostname: 'lambda.us-east-2.amazonaws.com',
pathname: '/',
path: '/',
href: 'https://lambda.us-east-2.amazonaws.com/'
},
_events: { apiCallAttempt: [Array], apiCall: [Array] },
MONITOR_EVENTS_BUBBLE: [Function: EVENTS_BUBBLE],
CALL_EVENTS_BUBBLE: [Function: CALL_EVENTS_BUBBLE],
_clientId: 2
},
operation: 'listFunctions',
params: {},
httpRequest: HttpRequest {
method: 'POST',
path: '/',
headers: {
'User-Agent': 'aws-sdk-nodejs/2.536.0 linux/v12.13.0 exec-env/AWS_Lambda_nodejs12.x'
},
body: '',
endpoint: Endpoint {
protocol: 'https:',
host: 'lambda.us-east-2.amazonaws.com',
port: 443,
hostname: 'lambda.us-east-2.amazonaws.com',
pathname: '/',
path: '/',
href: 'https://lambda.us-east-2.amazonaws.com/',
constructor: [Function]
},
region: 'us-east-2',
_userAgent: 'aws-sdk-nodejs/2.536.0 linux/v12.13.0 exec-env/AWS_Lambda_nodejs12.x'
},
startTime: 2019-12-04T20:30:18.812Z,
response: Response {
request: [Circular],
data: null,
error: null,
retryCount: 0,
redirectCount: 0,
httpResponse: HttpResponse {
statusCode: undefined,
headers: {},
body: undefined,
streaming: false,
stream: null
},
maxRetries: 3,
maxRedirects: 10
},
_asm: AcceptorStateMachine {
currentState: 'validate',
states: {
validate: [Object],
build: [Object],
afterBuild: [Object],
sign: [Object],
retry: [Object],
afterRetry: [Object],
send: [Object],
validateResponse: [Object],
extractError: [Object],
extractData: [Object],
restart: [Object],
success: [Object],
error: [Object],
complete: [Object]
}
},
_haltHandlersOnError: false,
_events: {
validate: [
[Function],
[Function],
[Function: VALIDATE_REGION],
[Function: BUILD_IDEMPOTENCY_TOKENS],
[Function: VALIDATE_PARAMETERS]
],
afterBuild: [
[Function],
[Function: SET_CONTENT_LENGTH],
[Function: SET_HTTP_HOST]
],
restart: [ [Function: RESTART] ],
sign: [ [Function], [Function], [Function] ],
validateResponse: [ [Function: VALIDATE_RESPONSE], [Function] ],
send: [ [Function] ],
httpHeaders: [ [Function: HTTP_HEADERS] ],
httpData: [ [Function: HTTP_DATA] ],
httpDone: [ [Function: HTTP_DONE] ],
retry: [
[Function: FINALIZE_ERROR],
[Function: INVALIDATE_CREDENTIALS],
[Function: EXPIRED_SIGNATURE],
[Function: CLOCK_SKEWED],
[Function: REDIRECT],
[Function: RETRY_CHECK],
[Function: API_CALL_ATTEMPT_RETRY]
],
afterRetry: [ [Function] ],
build: [ [Function: buildRequest] ],
extractData: [ [Function: extractData], [Function: extractRequestId] ],
extractError: [ [Function: extractError], [Function: extractRequestId] ],
httpError: [ [Function: ENOTFOUND_ERROR] ],
success: [ [Function: API_CALL_ATTEMPT] ],
complete: [ [Function: API_CALL] ]
},
emit: [Function: emit],
API_CALL_ATTEMPT: [Function: API_CALL_ATTEMPT],
API_CALL_ATTEMPT_RETRY: [Function: API_CALL_ATTEMPT_RETRY],
API_CALL: [Function: API_CALL]
}END RequestId: dc9caa5c-42b1-47e9-8136-80c3fbdddbc5
REPORT RequestId: dc9caa5c-42b1-47e9-8136-80c3fbdddbc5  Duration: 45.81 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 86 MB  

AWS开发工具包调用返回 AWS。请求对象,而不是对实际 API 调用的响应,后者通常是异步到达的。

您需要添加如下所示的回调处理程序:

lambda.listFunctions((err, data) => {
if (err) {
console.err(err);
} else {
data.Functions.forEach(func => console.log(func.FunctionName));
}
});

或者简单地使用 async/await,就像这样(注意封闭函数必须async(:

const AWS = require('aws-sdk');
const lambda = new AWS.Lambda();
exports.handler = async (event) => {
const funcs = await lambda.listFunctions().promise();
funcs.Functions.forEach(func => console.log(func.FunctionName));
}

返回给您的数据/函数将是一个包含函数数组的 JavaScript 对象。有关详细信息,请参阅 SDK 参考。

理想情况下,使用 async/await 表单。它更简单,更不容易出错,而且更现代。

最新更新