简体   繁体   中英

How to select aws lambda function names using node SDK?

At the CLi I can do

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?

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

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.

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 ):

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. See the SDK reference for specifics.

Ideally, use the async/await form. It's simpler, less prone to error, and more modern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM