計劃的 Lambda 函數無法進行第 3 方 API 調用 (Scheduled Lambda function not able to make 3rd party API calls)


問題描述

計劃的 Lambda 函數無法進行第 3 方 API 調用 (Scheduled Lambda function not able to make 3rd party API calls)

它什麼都不做,我在 CloudWatch 中沒有收到任何錯誤。我在調用之前和之後放置了 console.logs(),它記錄了之前的,但之後沒有。</p>


參考解法

方法 1:

You should always put async code inside try ... catch block. Also forEach won't work with promise you will need to use for loop. Try this:

export async function eventFunction(event) {
  try {

    for (let record of event.Records) {
      if (record.eventName === 'INSERT') {    
        await sharedFunction(param1)
      }
    }
  }
  catch (err) {
    console.log(err);
    return err;
  }
}

Shared function:

const sharedFunction = async (param1) => {
  try {
    const apiUrl = 'xxxxxx';
    return await axios.get(apiUrl, {
        headers: {
            'x‑api‑key': xxxx
        }
    });

  }
  catch (err) {
    return err;   
  } 
}

(by Tom MatonA.Khan)

參考文件

  1. Scheduled Lambda function not able to make 3rd party API calls (CC BY‑SA 2.5/3.0/4.0)

#serverless #serverless-framework #aws-lambda






相關問題

計劃的 Lambda 函數無法進行第 3 方 API 調用 (Scheduled Lambda function not able to make 3rd party API calls)

如何創建具有公共讀取訪問權限的存儲桶? (How to create a bucket with Public Read Access?)

從代理後面登錄的無服務器框架? (Serverless Framework Login From Behind a Proxy?)

使用 AWS Lambda 對 Hasura 身份驗證掛鉤的空響應 (Empty response on Hasura auth hook using AWS Lambda)

從另一個 SAM 本地函數調用 AWS SAM 本地函數 (Invoke AWS SAM local function from another SAM local function)

package.json 文件在錯誤的文件夾中創建 (package.json file created in the wrong folder)

混合 Terraform 和無服務器框架 (Mixing Terraform and Serverless Framework)

將基於本地的每週商店時間轉換為 GMT 每週時間 (convert local based weekly store timings to GMT weekly timing)

從 AWS Lambda 函數創建 CloudFormation 堆棧,傳遞 API Gateway 參數 (Create CloudFormation stack from AWS Lambda function, passing API Gateway parameters)

減少我的 Express 應用程序的代碼大小 (Reduce code size of my Express application)

AWS Lambda 函數全局變量 (AWS Lambda function global variables)

我可以備份無服務器 SQL 池數據庫嗎? (Can I backup Serverless SQL pool Database?)







留言討論