[英]How to select aws lambda function names using node SDK?
At the CLi I can do在 CLi 我可以做到
aws lambda list-functions
and get all the functions detail并获取所有功能的详细信息
Also I can do我也可以
aws lambda list-functions --query 'Functions[*].[FunctionName]' --output text
and get a simple list of just the function names.并获得仅包含函数名称的简单列表。
How can I do that in a lambda using the SDK?如何使用 SDK 在 lambda 中做到这一点?
I tried我试过
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);
};
and I have aws lambda full access role我有 aws lambda 完全访问角色
But i get the output below但我得到下面的输出
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 SDK calls return an AWS.Request object, not the response to the actual API call, which typically arrives asynchronously. AWS 开发工具包调用返回一个AWS.Request对象,而不是对实际 API 调用的响应,后者通常是异步到达的。
You need to add a callback handler like so:您需要添加一个回调处理程序,如下所示:
lambda.listFunctions((err, data) => {
if (err) {
console.err(err);
} else {
data.Functions.forEach(func => console.log(func.FunctionName));
}
});
Or simply use async/await, like so (note that the enclosing function must be async
):或者简单地使用 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));
}
The data/funcs returned to you will be a JavaScript object including an array of functions.返回给您的数据/函数将是一个包含函数数组的 JavaScript 对象。 See the SDK reference for specifics.
有关详细信息,请参阅SDK 参考。
Ideally, use the async/await form.理想情况下,使用 async/await 形式。 It's simpler, less prone to error, and more modern.
它更简单,更不容易出错,而且更现代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.