首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法通过node.js从lambda获取Amazon日志

无法通过node.js从lambda获取Amazon日志
EN

Stack Overflow用户
提问于 2020-05-27 00:09:48
回答 1查看 3.8K关注 0票数 2

我希望使用AWS.CloudWatchLogs.describeLogStreams获取cloudwatch日志,但返回的响应是httpstatuscode = 'undefined‘,而不是数据。

我该如何处理这个问题?

我执行以下代码来进行调试。

代码语言:javascript
复制
const aws = require('aws-sdk')

exports.handler = async (event, context) => {
  const cloudwatchlogs = new aws.CloudWatchLogs();
  const params = {
    logGroupName: '/aws/lambda/test'
  }
  var tmp = await cloudwatchlogs.describeLogStreams(params);
  console.log(tmp);
};

得到下面的日志。

代码语言:javascript
复制
2020-05-26T22:48:25.869Z    51891acf-302d-4d71-8f3b-b98cab65192d    INFO    Request {
  domain: null,
  service: Service {
    config: Config {
      credentials: [EnvironmentCredentials],
      credentialProvider: [CredentialProviderChain],
      region: 'ap-northeast-1',
      logger: null,
      apiVersions: {},
      apiVersion: null,
      endpoint: 'logs.ap-northeast-1.amazonaws.com',
      httpOptions: [Object],
      maxRetries: undefined,
      maxRedirects: 10,
      paramValidation: true,
      sslEnabled: true,
      s3ForcePathStyle: false,
      s3BucketEndpoint: false,
      s3DisableBodySigning: true,
      s3UsEast1RegionalEndpoint: 'legacy',
      s3UseArnRegion: undefined,
      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: 'legacy'
    },
    isGlobalEndpoint: false,
    endpoint: Endpoint {
      protocol: 'https:',
      host: 'logs.ap-northeast-1.amazonaws.com',
      port: 443,
      hostname: 'logs.ap-northeast-1.amazonaws.com',
      pathname: '/',
      path: '/',
      href: 'https://logs.ap-northeast-1.amazonaws.com/'
    },
    _events: { apiCallAttempt: [Array], apiCall: [Array] },
    MONITOR_EVENTS_BUBBLE: [Function: EVENTS_BUBBLE],
    CALL_EVENTS_BUBBLE: [Function: CALL_EVENTS_BUBBLE],
    _clientId: 1
  },
  operation: 'describeLogGroups',
  params: {},
  httpRequest: HttpRequest {
    method: 'POST',
    path: '/',
    headers: {
      'User-Agent': 'aws-sdk-nodejs/2.682.0 linux/v12.16.3 exec-env/AWS_Lambda_nodejs12.x'
    },
    body: '',
    endpoint: Endpoint {
      protocol: 'https:',
      host: 'logs.ap-northeast-1.amazonaws.com',
      port: 443,
      hostname: 'logs.ap-northeast-1.amazonaws.com',
      pathname: '/',
      path: '/',
      href: 'https://logs.ap-northeast-1.amazonaws.com/',
      constructor: [Function]
    },
    region: 'ap-northeast-1',
    _userAgent: 'aws-sdk-nodejs/2.682.0 linux/v12.16.3 exec-env/AWS_Lambda_nodejs12.x'
  },
  startTime: 2020-05-26T22:48:25.866Z,
  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]
}

补编:

  • 我的lambda函数有CloudWatchFullAccess策略
  • 我可以在cloudwatch控制台中看到此函数的cloudwatch日志。
  • 我的lambda函数运行库是Node.js 12.x
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-27 01:37:25

我认为这是因为你的函数在得到任何结果之前就完成了。克服这一问题的一种方法是为异步处理程序使用模式,如aws博士所示。

代码语言:javascript
复制
const aws = require('aws-sdk')

exports.handler = async (event, context) => {
  const promise = new Promise(function(resolve, reject) {
    const cloudwatchlogs = new aws.CloudWatchLogs();
    const params = {
      logGroupName: '/aws/lambda/test'
    }
    cloudwatchlogs.describeLogStreams(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });
  })
  return promise;

};

上面的代码工作。我用我自己的lambda函数测试了它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62033425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档