簡體   English   中英

Node.js HTTPS 調用在 AWS Lambda 中不起作用

[英]Node.js HTTPS call not working in AWS Lambda

我正在嘗試創建一個 AWS Lambda function ,它將按計划調用 DELETE。

我正在使用 Node.js。 在我的本地計算機上僅從 Node.js 運行時,請求工作正常。

這是代碼:

const https = require('https');

var options = {
    host: 'MY_HOST',
    path: '/v1/api/orders',
    port: 80,
    method: 'DELETE'
};

console.info('Do the DELETE call');

var reqDelete = https.request(options, function(res) {

    res.on('data', function(d) {
        console.info('DELETE result:\n');
        console.log(d.toString('utf8'));
        console.info('\nCall completed');
    });

});

 reqDelete.on('error', function(e) {
    console.error(e);
});


reqDelete.end(); 

我的 output 是這樣的:

Do the DELETE call
DELETE result:

{"message":"Cleanup triggered"}

Call completed

正如我所料。 However when I run from inside an AWS Lambda function I get the result of null and the Log output for the Lambda function is this.

START RequestId: fb2a1969-94e8-4c11-b43e-14ff6a4cc426 Version: $LATEST
2020-06-16T01:42:06.875Z    fb2a1969-94e8-4c11-b43e-14ff6a4cc426    INFO    Do the DELETE call
END RequestId: fb2a1969-94e8-4c11-b43e-14ff6a4cc426
REPORT RequestId: fb2a1969-94e8-4c11-b43e-14ff6a4cc426  Duration: 483.49 ms Billed Duration: 500 ms  
Memory Size: 128 MB Max Memory Used: 67 MB  Init Duration: 125.35 ms

請注意,它會打印出“執行 DELETE 調用”,因此我知道它正在訪問我的代碼,但沒有打印出其他任何內容。

Lambda的主體是這樣的:

exports.handler = async (event) => {
    // The exact code from above with the actual host name.
};

為什么我的 API 調用沒有從 Lambda function 在我的本地計算機上執行?

您正在使用異步 Lambda function 處理程序,但不等待 HTTP 請求。 這會導致 function 在https.request()調用完成之前完成執行。

如果您想使用回調而不是承諾,則為 function 定義一個非異步處理程序:

exports.handler = (event) => {
    // The exact code from above with the actual host name.
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM