1

我有一个 AWS Lambda 函数可以触发对 Google API 的 https 请求。我希望该函数是可等待的,以便它不会立即结束,而只是在从 Google API 获得响应之后。是的,我知道我为执行付费,但这不会经常调用,所以没关系。

问题是 http 请求似乎没有正确触发。回调永远不会执行。

我通过在 Promise 中使用 setTimeout 确保异步/等待按预期工作。所以问题出在 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;
 }
4

1 回答 1

1

问题是 http 请求似乎没有正确触发。回调永远不会执行。

我相信这部分问题与(a)可能没有实际发送 https 请求和(b)没有使用正确的回调签名的某种组合有关https.request。有关这两者的详细信息,请参阅https://nodejs.org/api/https.html#https_https_request_options_callback上的文档。

使用 node-fetch 包

以下示例适用于我使用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 抱怨说,它类似于“无法序列化本机函数”或类似的东西。有问题的部分是 node-fetch 依赖于 Symbol.iterator

如评论中所述,可能导致这种情况的一些条件记录在https://pulumi.io/reference/serializing-functions.html中。但是,我看不出有什么明确的理由说明这段代码会遇到这些限制。可能有关于如何在上面共享的片段的上下文之外使用它的细节,这导致了这一点。

于 2019-01-21T21:16:52.160 回答