我有一个 AWS Lambda 函数,它会触发对 Google API 的 https 请求。我希望该函数是可等待的,这样它就不会立即结束,而是在收到 Google API 的响应后才结束。 是的,我知道我为执行付费,但这不会经常被调用,所以没关系。
问题是 http 请求似乎没有正确触发。回调永远不会执行。
我通过在 Promise 中使用 setTimeout 确保 async/await 按预期工作。所以问题出在 https.request 的某个地方。
另请注意,我使用 Pulumi 部署到 AWS,因此其中可能存在一些隐藏问题。我只是不知道在哪里。
<小时/>相关代码:
调用 Google API 的 AWS Lambda
import config from '../../config';
import { IUserInfo } from '../../interfaces';
const https = require('https');
function sendHttpsRequest(options: any): Promise<any> {
console.log(`sending request to ${options.host}`);
console.log(`Options are ${JSON.stringify(options)}`);
return new Promise(function (resolve, reject) {
console.log(` request to ${options.host} has been sent A`);
let body = new Array<Buffer>();
const request = https.request(options, function (res: any) {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
if (res.statusCode != 200) {
reject(res.statusCode);
}
res.on('data', (data: any) => {
console.log(`body length is ${body.length}`);
console.log('data arrived', data);
body.push(data);
console.log('pushed to array');
console.log(data.toString());
});
});
request.on('end', () => {
console.error('Request ended');
// at this point, `body` has the entire request body stored in it as a string
let result = Buffer.concat(body).toString();
resolve(result);
});
request.on('error', async (err: Error) => {
console.error('Errooooorrrr', err.stack);
console.error('Errooooorrrr request failed');
reject(err);
});
request.end();
console.log(` request to ${options.host} has been sent B`);
});
}
/**
* AWS Lambda to create new Google account in TopMonks domain
*/
export default async function googleLambdaImplementation(userInfo: IUserInfo) {
const payload = JSON.stringify({
"primaryEmail": userInfo.topmonksEmail,
"name": {
"givenName": userInfo.firstName,
"familyName": userInfo.lastName
},
"password": config.defaultPassword,
"changePasswordAtNextLogin": true
});
const resultResponse: Response = {
statusCode: 200,
body: 'Default response. This should not come back to users'
}
console.log('Calling google api via post request');
try {
const options = {
host: 'www.googleapis.com',
path: '/admin/directory/v1/users',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': payload.length.toString()
},
form: payload
}
const responseFromGoogle = await sendHttpsRequest(options);
console.log('responseFromGoogle', JSON.stringify(responseFromGoogle));
}
catch (err) {
console.log('Calling google api failed with error', err);
resultResponse.statusCode = 503;
resultResponse.body = `Error creating new Google Account for ${userInfo.topmonksEmail}.`;
return resultResponse;
}
console.log('request to google sent');
return resultResponse;
}
最佳答案
The problem is that the http request does not seem to fire correctly. The callback is never executed.
我认为问题的这一部分与 (a) 可能没有实际发送 https 请求和 (b) 没有为 https.request
使用正确的回调签名有关。请参阅文档 https://nodejs.org/api/https.html#https_https_request_options_callback有关这两个方面的详细信息。
Use node-fetch package
以下示例适用于我使用node-fetch
:
import * as aws from "@pulumi/aws";
import fetch from "node-fetch";
const api = new aws.apigateway.x.API("api", {
routes: [{
method: "GET", path: "/", eventHandler: async (ev) => {
const resp = await fetch("https://www.google.com");
const body = await resp.text();
return {
statusCode: resp.status,
body: body,
}
},
}],
})
export const url = api.url;
Pulumi complains, it something like "Can not serialize native function" or something like that. The problematic part is that node-fetch relies on Symbol.iterator
正如评论中所指出的,可能导致这种情况的一些条件记录在 https://pulumi.io/reference/serializing-functions.html 中。 。但是,我没有看到任何明确的原因说明为什么此代码会遇到任何这些限制。可能有关于如何在上面共享的代码片段上下文之外使用它的详细信息,从而导致这种情况。
关于node.js - 在 AWS Lambda/Pulumi 中等待 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54287228/