【问题标题】:AWS Lamda function to make rest api call to api deployed in EC2AWS Lambda函数对部署在EC2中的api进行rest api调用
【发布时间】:2018-08-12 18:13:58
【问题描述】:

我想使用 java 编写一个 AWS Lamda 函数 .. 使 rest api 调用部署在 EC2 url 中的 api 应该是可配置的

【问题讨论】:

  • 那么到目前为止你做了什么?这里有什么问题?

标签: java amazon-web-services aws-lambda


【解决方案1】:

您可以将AWS API Gateway 视为您的 EC2 中的 API 代理。 Calling from Lambda to API Gateway 很简单。

【讨论】:

    【解决方案2】:
    const https = require("https");
    
    const handler = async (event) => {
        const data = JSON.stringify({
            todo: "Order pizza",
        });
    
        const options = {
            hostname: "yourwebsite.com",
            port: 443,
            path: "/todos",
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "Content-Length": data.length,
            },
        };
    
        const response = await doRequest(options, data);
        console.log("OTP response", JSON.stringify(response));
    
        return event;
    };
    
    /**
     * Do a request with options provided.
     *
     * @param {Object} options
     * @param {Object} data
     * @return {Promise} a promise of request
     */
    function doRequest(options, data) {
        return new Promise((resolve, reject) => {
            const req = https.request(options, (res) => {
                res.setEncoding("utf8");
                let responseBody = "";
    
                res.on("data", (chunk) => {
                    responseBody += chunk;
                });
    
                res.on("end", () => {
                    resolve(JSON.parse(responseBody));
                });
            });
    
            req.on("error", (err) => {
                reject(err);
            });
    
            req.write(data);
            req.end();
        });
    }
    
    module.exports = {
        handler,
    };
    

    【讨论】:

      猜你喜欢
      • 2023-02-21
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      • 2020-11-21
      • 2015-10-05
      • 2018-07-28
      相关资源
      最近更新 更多