You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lambda中AWS CloudWatch Logs查询超时问题求助

使用AWS JavaScript SDK v3在Lambda中查询CloudWatch日志组

相关实现代码

export const realTimeQueryString = (route_key: string, account: string) => `fields @timestamp, @message | 
    filter route_key = "ANY /${route_key}/{proxy+}" and user != "-"  and account = "${account}" | 
    stats count(*) by account, user, path`;

export const getFirstDayOfMonthTimestamp = (): number => {
  const date = new Date();
  const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
  return Math.floor(firstDayOfMonth.getTime() / 1000);
};


const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

export const startQuery = async (queryString: string) => {
  logger.info(`query initiated for ${queryString}`);
  const params: StartQueryCommandInput = {
    logGroupName: 'api_gw_v2',
    queryString: queryString,
    startTime: getFirstDayOfMonthTimestamp(),
    endTime: Math.floor(new Date().getTime() / 1000),
    limit: 10000,
  };
  logger.info(`init query: ${JSON.stringify(params)}`);
  const command = new StartQueryCommand(params);
  const response = await client.send(command);
  return response.queryId;
}


export const stopQuery = async (queryId: string) => {
  logger.info(`stopping query: ${JSON.stringify(queryId)}`);
  const command = new StopQueryCommand({ queryId: queryId });
  await client.send(command);
}

export const getQueryResults = async (queryId: string) => {
  logger.info(`Processing results for query: ${queryId}`);
  const command = new GetQueryResultsCommand({ queryId: queryId });
  const result = await client.send(command);
  
  while (result['status'] === 'Running') {
    logger.info(`current result: ${JSON.stringify(result)}`);
    logger.info(`waiting for query results to complete...`);
    await sleep(5000);
  }
  return result;
}

Lambda处理函数中的调用方式

const apiQueryId = await startQuery(realTimeQueryString('api',  event["account"]));
const apiQueryResults = await getQueryResults(apiQueryId);

Lambda日志记录信息

startQuery函数输出的查询参数日志:

{
    "logGroupName": "*****

内容的提问来源于stack exchange,提问作者Øystein Seel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 23:44:58