NodeJS 从AWS Lambda访问Google云计算引擎API

jqjz2hbq  于 2023-06-29  发布在  Node.js
关注(0)|答案(1)|浏览(131)

我试图从谷歌云计算和计算引擎API获取数据。我在AWS lambda中使用Google的nodejs SKD。函数在dev中运行完成并获得所需的结果(使用AWS toolkit for vscode)。测试λ,我得到了
“errorMessage”:“请求ID:...错误:运行时退出,出现错误:signal:killed“我读到的是与过度使用内存有关。此外,当我尝试记录API调用的结果时,我什么也没有收到。
服务帐户中的“我的应用程序默认凭据”文件如下所示:

{
  "type": "service_account",
  "project_id": "",
  "private_key_id": "",
  "private_key": "",
  "client_email": "",
  "client_id": "",
  "auth_uri": "",
  "token_uri": "",
  "auth_provider_x509_cert_url": "",
  "client_x509_cert_url": "",
  "universe_domain": "googleapis.com"
}

它的路径在lambdas env中。
我的lambda部分代码是:

const AWS = require('aws-sdk');
const { CloudCatalogClient } = require('@google-cloud/billing').v1;
const { MachineTypesClient } = require('@google-cloud/compute').v1;

const FROM_NANOS = 1000000000.0;
const FROM_GIBY = 1024;

 
async function getSkuList() {
  const billingClient = new CloudCatalogClient();

  const computeEngineServiceId = await getComputeEngineServiceId (billingClient);
  const billingRequest = {
    parent: computeEngineServiceId,
  };
  
  // https://cloud.google.com/nodejs/docs/reference/billing/latest/billing/v1.cloudcatalogclient
  const skuList = await billingClient.listSkusAsync(billingRequest); 
  console.dir("skuList: ", skuList) // shows only text in lambdas logs

  // billingClient.close();
  return [ skuList, billingClient ];
}

// Get Compute-Engine service id
async function getComputeEngineServiceId (billingClient) {
  let computeEngine = null;
  let pageToken = null;
  
  do {
    const serviceList = await billingClient.listServicesAsync({ pageToken });
    pageToken = serviceList.nextPageToken;
      
    for await (const service of serviceList) {
      if (service.displayName === "Compute Engine") {
        computeEngine = service.name;
        break;
      }
    }
  } while (!computeEngine && pageToken)

  if (!computeEngine) {
    throw new Error("Compute engine service not found");
  }

  return computeEngine;  
}

// Get instances list for zone us-east1-a
async function getInstancesTypes() {  
  const instancesClient = new MachineTypesClient();
  const instancesRequest = {
    project: process.env.projectId,
    zone: "us-east1-a",
  };
  
  const res = [];

  const instances = await instancesClient.listAsync(instancesRequest);

  console.dir("instances: ", instances) // shows only text in lambdas logs

  for await (const type of instances) {
    const instance = {
      instanceType: type.name,
      memorySize: type.memoryMb,
      cpuCount: type.guestCpus,
    }
    
    res.push(instance);
  }

  instancesClient.close();
  return res;
}

// LAMBDA HANDLER
exports.handler = async function(event, context, callback) {
  try {
    ... 
   
    const [skuList, billingClient ] = await getSkuList();
    const instancesTypes = await getInstancesTypes();
    
    for await (const sku of skuList) {
      ...
    }

    billingClient.close()
  
    
      
    const resultV1 = {
      timestamp: Date.now(),
      cloudProvider: 'GCP',
      ...
    };
    
    console.log(" : ", JSON.stringify(resultV1));

  

    callback(null, "Success");
  } catch (error) {
    console.error(error.message);
    throw error;
  }
};

你知道我该怎么做才能让事情顺利进行吗?谢谢!

k2fxgqgv

k2fxgqgv1#

错误消息Runtime exited with error: signal: killed通常在AWS Lambda函数超过其分配的资源或执行时间限制时发生。
Google Cloud Compute Engine API可能需要大量资源或时间才能完成某些操作,而AWS Lambda有一些可能导致此错误的限制。
AWS Lambda允许您配置分配给函数的内存量。请尝试增加分配的内存,以便为函数执行提供更多资源。更高的内存分配也增加了该函数可用的CPU能力。如果您的函数执行CPU密集型任务,请考虑同时调整超时配置。对于需要更长时间才能完成的任务,可能需要更长的超时时间。
如果问题仍然存在,最好联系AWS支持以获得进一步帮助。

相关问题