【问题标题】:How to call a secured route using AWS Lambda?如何使用 AWS Lambda 调用安全路由?
【发布时间】:2017-10-23 21:10:14
【问题描述】:

这是我的 AWS Lambda 函数的“代码”部分。当我在我们的暂存环境(非 SSL)上使用它时,它工作正常。

我尝试使用来自thisstackoverflow 帖子的答案,但我收到了一个 npm 错误Cannot find package ssl-root-cas,我不完全确定我是否能够安装节点包。

'use strict';

const https = require('https');

var rootCas = require('ssl-root-cas').create();

https.globalAgent.options.ca = rootCas;

/**
 * Pass the data to send as `event.data`, and the request options as
 * `event.options`. For more information see the HTTPS module documentation
 * at https://nodejs.org/api/https.html.
 *
 * Will succeed with the response body.
 */
exports.handler = (event, context, callback) => {

    var url = 'https://admin.domain.com/api/schedule/checkForPublishedScreeners?authentication_token=mytoken';
    https.get(url, (res) => {

        let body = '';
        console.log('Status:', res.statusCode);
        console.log('Headers:', JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', (chunk) => body += chunk);
        res.on('end', () => {
            console.log('Successfully processed HTTPS response');
            // If we know it's JSON, parse it
            if (res.headers['content-type'] === 'application/json') {
                body = JSON.parse(body);
            }
            callback(null, body);
        });

    }).on('error', (e) => {
        console.error(e);
    });
};

【问题讨论】:

    标签: node.js amazon-web-services ssl lambda


    【解决方案1】:

    您需要在 package.json 中包含 ssl-root-cas,并在发布 lambda 代码时包含它。

    npm init -y
    npm install --save ssl-root-cas
    

    将 node_modules 与您的 lambda 代码一起压缩,然后将其发布到 Lambda

    【讨论】:

    • 我实际上并没有提交 package.json,例如我从不为 require('https') 行包含任何内容。也许我在 Lambda 实现上遗漏了一些东西?
    • https 模块内置在 NodeJS Lambda 运行时中。 ssl-root-cas 模块不是。
    • 您需要在压缩和上传的 node_modules 中包含 ssl-root-cas。
    猜你喜欢
    • 2017-05-24
    • 2017-10-30
    • 2017-10-01
    • 2021-06-11
    • 2021-11-09
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    相关资源
    最近更新 更多