問題描述
計劃的 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;
}
}