如何使用AWS Lambda调用安全路由?

时间:2017-05-23 18:52:10

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

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

我尝试使用来自this stackoverflow帖子的答案,但我收到了一个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);
    });
};

1 个答案:

答案 0 :(得分:2)

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

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

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