You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Localstack中通过API Gateway转发请求至公共HTTP服务的问题

问题:遗留系统集成AWS API Gateway的Localstack原型开发及Lambda请求异常

背景与需求

  • 需将遗留系统集成到AWS API Gateway,先基于Localstack制作概念验证原型
  • 正在快速学习AWS相关原理,使用不熟悉的Node.js编写Lambda函数

核心疑问

  • 不确定应选择API Gateway触发Lambda转发请求至遗留系统再返回响应,还是直接使用HTTP集成方案
  • 无法找到Localstack相关的实现示例,搜索结果仅为AWS控制台操作文档

当前问题与代码

已基于Localstack Docker容器和Serverless插件搭建基础示例(API Gateway触发Lambda返回字符串),但尝试让Lambda向测试URL(如Google)发送GET请求时,https.get()内部的console.log无输出,仅返回默认提示消息,请求未触发。

当前Lambda代码:

'use strict';
const https = require('https');

module.exports.hello = async (event) => {
    console.log("in lambda function");
    let testURL = "https://www.google.com/";
    console.log(testURL);

    let testResponse = "";
    
    https.get(testURL, (resp) => {
        console.log("In http.get");
    let data = '';

        // A chunk of data has been received.
    resp.on('data', (chunk) => {
            console.log(("get chunk"));
            data += chunk;
    });

    // The whole response has been received. Print out the result.
    resp.on('end', () => {
            console.log("response end");
            console.log(JSON.parse(data).explanation);
            testResponse = JSON.stringify(data);
    });

    }).on("error", (err) => {
        console.log("Error: " + err.message);
        testResponse = "Error: " + err.message;
    });

    // This is to make sure something correctly formatted gets returned otherwise API Gateway
    // complains about missing values. If this returns it also implies the http.get() didn't trigger
    if(testResponse == ""){
        testResponse = {
            statusCode: 200,
            body: JSON.stringify(
                {
                    message: "Didn't go through https.get?",
                    input: event,
                },
                null,
                2
            ),
    };
    }

    console.log("about to return");
    return testResponse;
}

内容的提问来源于stack exchange,提问作者Boosh_87

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 17:39:24