【问题标题】:How to select aws lambda function names using node SDK?如何使用节点 SDK 选择 aws lambda 函数名称?
【发布时间】:2019-12-04 20:36:17
【问题描述】:

在 CLi 我可以做到

aws lambda list-functions

并获取所有功能的详细信息

我也可以

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

并获得一个仅包含函数名称的简单列表。

如何使用 SDK 在 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  

【问题讨论】:

    标签: amazon-web-services aws-lambda


    【解决方案1】:

    AWS 开发工具包调用返回一个 AWS.Request 对象,而不是对实际 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 reference

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

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 2021-12-09
      • 2018-07-17
      • 2017-09-22
      • 1970-01-01
      相关资源
      最近更新 更多